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; 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; }
如果我的文章对你有帮助,欢迎微信支付打赏。