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

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

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

1
2
3
4
5
6
7
8
9
10
<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;
    
    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;
}

 

posted @   逃离沙漠  阅读(2045)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示