Java使用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也支持,只是方法被标注为已过期了。

二、实例代码

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

 

enum QRcodeType {
    QR_CODE,二维码
    CODE_128,1)">条形码
    other;
}

 

猜你在找的Java相关文章