ZXing插件生成二维码和条形码的技巧

Java使用ZXing生成二维码条形码

一、增加zxing 的maven依赖,或者下载Zxingjar包

本实例使用的是 zxing3.2.0的版本 

<dependency>

    <groupId>com.google.zxing</groupId>

    <artifactId>core</artifactId>

    <version>3.2.0</version>

</dependency>

<dependency>

    <groupId>com.google.zxing</groupId>

    <artifactId>javase</artifactId>

    <version>3.2.0</version>

</dependency>

  

maven依赖:

 

下载地址 http://pan.baidu.com/s/1gdH7PzP

说明:本实例使用的3.2.0版本已经使用的java7的nio. 旧版本的java io也支持,只是方法被标注为已过期了。

二、实例代码

copy

/**
 * 二维码&条形码操作工具
 * <br/>
 * 使用示例:<code>QRcodes.me().doMethod(O o1,O o2);</code>
 * <br/>
 * 
 * @author  
 * @date 2015年6月1日 下午5:00:55
 */
public class QRcodes {

    private static QRcodes me=null;
    
    public static final String default_stuffix="png";
    
    public static final String default_charset="UTF-8";
    
    private QRcodes(){}
    
    /**
     * 单例
     * @return
     */
    public static QRcodes me(){
        if(me==null){
            me=new QRcodes();
        }
        return me;
    }
    
    
    /**
     * 生成二维码到指定文件路径
     * @param codeData
     *                     二维码内容
     * @param filePath
     *                     文件,格式为/fatherpath/childpath/filename.stuffix
     * @param charset
     *                     编码默认为uft-8
     * @param correctionLevel
     *                     错误修正级别1,2,3,4
     * @param height
     *                     高度
     * @param width
     *                     宽度
     * @return
     */
    public  boolean createQRCode2File(QRcodeType codeType,String codeData, String filePath,String charset, int correctionLevel, int height, int width) {
        try {
            Path path=Paths.get(filePath);
            String suffix=filePath.substring(filePath.lastIndexOf('.') + 1);
            if(suffix==null||"".equals(suffix)){
                suffix=default_stuffix;
            }
            if(charset==null||"".equals(charset)){
                charset=default_charset;
            }
            Map<EncodeHintType, Object> hintMap =  createHintMap(correctionLevel);
            BarcodeFormat type=getBarcodeFormat(codeType);
            BitMatrix matrix =new MultiFormatWriter().encode(new String(codeData.getBytes(charset), charset),type, width, height, hintMap);;
            MatrixToImageWriter.writeToPath(matrix, suffix, path);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /**
     * 输出二维码到输出流
     * 
     * @param codeData
     *                     二维码内容
     * @param height
     *                     高度
     * @param width
     *                     宽度
     * @param charset
     *                     编码格式,默认utf-8
     * @param suffix
     *                     生成的文件后缀名
     * @param correctionLevel
     *                     错误修正级别1,2,3,4,级别越高越好识别,特别是在二维码中加入了logo的时候
     * @param stream
     *                     输出流
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public  OutputStream createQRCode2Stream(QRcodeType codeType,String codeData,int height, int width,String charset,String suffix,int correctionLevel,OutputStream stream)throws WriterException, IOException {
        if(suffix==null||"".equals("")){
            suffix=default_stuffix;
        }
        Map<EncodeHintType, Object> hintMap = createHintMap(correctionLevel);
        BarcodeFormat type=getBarcodeFormat(codeType);
        BitMatrix matrix=new MultiFormatWriter().encode(new String(codeData.getBytes(charset), charset),type, width, height, hintMap);;
        MatrixToImageWriter.writeToStream(matrix, suffix, stream);
        return stream;
    }
    /**
     * 参数处理,错误修正级别
     * @param correctionLevel
     * @return
     */
    private  Map<EncodeHintType, Object> createHintMap(int correctionLevel){
        Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
        hintMap.put(EncodeHintType.MARGIN, 1);//空白填充
        if(correctionLevel==2){
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        }else if(correctionLevel==3){
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
        }else if(correctionLevel==4){
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        }else{
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);    
        }
        return hintMap;
    }    
    
    public BarcodeFormat getBarcodeFormat(QRcodeType codeType){
        if(QRcodeType.QR_CODE==codeType){
            return BarcodeFormat.QR_CODE;
        }else if(QRcodeType.CODE_128==codeType){
            return BarcodeFormat.CODE_128;
        }else{
            return BarcodeFormat.QR_CODE;
        }
    }
    
}

 

public enum QRcodeType {
    QR_CODE,//二维码
    CODE_128,//条形码
    other;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值