一、关于ZXing
1、ZXing是谷歌开源的支持二维码、条形码 等图形的生成类库;支持生成、和解码功能。 Github主页 、 API文档 、 介绍文档 。
二、依赖 pom.xml
1、javase.jar
-
- <dependency>
- <groupId>com.google.zxinggroupId>
- <artifactId>javaseartifactId>
- <version>3.3.0version>
- dependency>
三、生成二维码、条形码 和解码
1、生成二维码
- /**
- * description: 生成二维码
- * @param content 二维码内容
- * @param width 宽
- * @param height 高
- * @param targetPath 生成二维码位置
- * @param imgType 图片类型-- jpg/png/gif 等
- * @throws Exception
- * @return void
- * @version v1.0
- * @author w
- * @date 2020年8月27日 上午10:41:37
- */
- public static void genQRCode(String content ,String targetPath ,String imgType , int width ,int height)throws Exception{
- Map
hints = new HashMap<>(); - //内容编码格式
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- // 指定纠错等级
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
- //设置二维码边的空度,非负数
- hints.put(EncodeHintType.MARGIN, 1);
- BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
- MatrixToImageWriter.writeToPath(bitMatrix, imgType, new File(targetPath).toPath());// 输出原图片
- }
2、解析二维码
- /**
- * description: 解析二维码
- * @param imgPath
- * @return
- * @throws Exception
- * @return String
- * @version v1.0
- * @author w
- * @date 2020年8月27日 上午11:22:42
- */
- @SuppressWarnings("unchecked")
- public static String readQRCode(String imgPath) throws Exception {
- File file = new File(imgPath);
- if(!file.exists() && !file.isFile()) {
- throw new RuntimeException("file not found ,"+ imgPath);
- }
- MultiFormatReader formatReader = new MultiFormatReader();
- //读取指定的二维码文件
- BufferedImage bufferedImage =ImageIO.read(file);
- BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
- //定义二维码参数
- Map hints= new HashMap<>();
- hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
- Result result = formatReader.decode(binaryBitmap , hints);
- return result.getText();
- }
3、生成条形码
- /**
- * description: 生成条形码
- * @param contents 条形码内容
- * @param width 条形码宽
- * @param height 条形码高
- * @param imgPath 图片存储路径
- * @return void
- * @version v1.0
- * @author w
- * @date 2020年8月27日 上午11:30:12
- */
- public static void genBarCode(String contents, int width, int height, String imgPath) {
- int codeWidth = 3 + // start guard
- (7 * 6) + // left bars
- 5 + // middle guard
- (7 * 6) + // right bars
- 3; // end guard
- codeWidth = Math.max(codeWidth, width);
- try {
- BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
- BarcodeFormat.EAN_13, codeWidth, height, null);
-
- MatrixToImageWriter
- .writeToFile(bitMatrix, "png", new File(imgPath));
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
4、解析条形码
- /**
- * description: 解析条形码
- * @param imgPath
- * @return String
- * @version v1.0
- * @author w
- * @date 2020年8月27日 上午11:30:27
- */
- public static String decode(String imgPath) {
- BufferedImage image = null;
- Result result = null;
- try {
- image = ImageIO.read(new File(imgPath));
- if (image == null) {
- System.out.println("the decode image may be not exit.");
- }
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
-
- result = new MultiFormatReader().decode(bitmap, null);
- return result.getText();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
四、web页面返回二维码
1、二维码转换为 Buffer
- /**
- * description: 生成二维码的 Buffer 二进制文件
- * @param content
- * @param imgType
- * @param width
- * @param height
- * @throws Exception
- * @return BufferedImage
- * @version v1.0
- * @author w
- * @date 2020年9月7日 下午5:33:03
- */
- public static BufferedImage genQRCodeToBuffer(String content ,String imgType , int width ,int height)throws Exception{
- Map
hints = new HashMap<>(); - //内容编码格式
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- // 指定纠错等级
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
- //设置二维码边的空度,非负数
- hints.put(EncodeHintType.MARGIN, 1);
- BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
- BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
- return bufferedImage;
- }
2、SpringMVC 设置返回 图片
- @RequestMapping(value = {"/gen"})
- public void gen(String name ,HttpServletRequest request , HttpServletResponse response) throws Exception {
- System.out.println(" name :" + name);
- if(null == name || name == "") {
- response.setContentType("application/json;charset=UTF-8");
- response.setCharacterEncoding("UTF-8");
- response.getWriter().print(" name can't be blank ,名字不能为空");
- return;
- }
-
- response.setHeader("Pragma", "no-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- response.setContentType("image/jpeg");
- BufferedImage genQRCodeToBuffer = ZXingUtils.genQRCodeToBuffer(name);
- ImageIO.write(genQRCodeToBuffer , "jpeg" ,response.getOutputStream());
- }
五、总结
1、本示例是简单记录了使用 zxing生成二维码、条形码编码和解码的过程;若有更高的需求,如:批量生成、解析等,二维码带Logo 等,可根据本示例代码进行修改定制,也可以私信哈 ^_^ !
前端生成二维码: 使用jquery qrcode生成二维码图片分享到朋友圈打印二维码_HaHa_Sir的博客-CSDN博客