java 生成条形码 或二维码

package com.sks.db.common;


import java.io.File;
import java.util.HashMap;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.sks.db.unit.CustomConfigCache;
import com.sks.db.unit.MatrixToImageWriter;


public class ImageBitmatrixUtils {

/**

* @方法名:BitmatrixUtils 
* @参数 @param bh  编号 比如订货单ID 或者订货单编号
* @参数 @param bhlx 编号类型 订货单未
* @参数 @param tmlx 1为一维码 2为二位码
* @参数 @return  
* @返回类型 String
*/
public static String  BitmatrixUtils(String bh , String bhlx , String tmlx) throws Exception{

String path ="" ;
String[] a  = new String[2] ;
int width = 130;  
        int height = 70; 
        String format = "gif";  
        StringBuffer text =new StringBuffer()  ;
        text.append(bhlx) ;
        text.append(bh) ;//类型和id拼接   
        HashMap<EncodeHintType, String> hints = new HashMap<>();  
        //内容所使用编码  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
        BitMatrix bitMatrix = null  ;
        if("1".equals(tmlx)){
         bitMatrix = new MultiFormatWriter().encode(text.toString(),  BarcodeFormat.CODE_128, width, height, hints);  
        
        }else{
        width = 300;
        height = 300 ;
        bitMatrix = new MultiFormatWriter().encode(text.toString(),  BarcodeFormat.QR_CODE, width, height, hints); 
       
        }//生成二维码  
        path = CustomConfigCache.configMap.get("ImageUrl") ;//配置文件读取地址
       //path = "F:\\SKS\\db_project\\WebRoot\\uploadfile";
        String qrCodePath = path+"\\"+text.toString()+".gif";//图片保存地址
        File outputFile = new File(qrCodePath);  
        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); 
        a[0] = path ;
        a[1] = text.toString() ;
return qrCodePath ; 
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值