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 ; 
}


}

猜你喜欢

转载自blog.csdn.net/hujiujun/article/details/77127476