日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZXing二维码和条形码

發(fā)布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZXing二维码和条形码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

工具類:

package com.wfz.zxing;import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.util.Hashtable; import java.util.Map;/*** Created by Liang on 5/3/2017.*/ public class ZxingUtils {/*** 生成二維碼** @param contents* @param width* @param height* @param imgPath*/public void encodeQRCode(String contents, int width, int height, String imgPath) {Map<EncodeHintType, Object> hints = new Hashtable<>();// 指定糾錯等級 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);// 指定編碼格式hints.put(EncodeHintType.CHARACTER_SET, "GBK");try {BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.QR_CODE, width, height, hints);MatrixToImageWriter.writeToStream(bitMatrix, "png",new FileOutputStream(imgPath));} catch (Exception e) {e.printStackTrace();}}/*** 解析二維碼** @param imgPath* @return*/public String decodeQRCode(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));Map<DecodeHintType, Object> hints = new Hashtable<>();hints.put(DecodeHintType.CHARACTER_SET, "GBK");result = new MultiFormatReader().decode(bitmap, hints);return result.getText();} catch (Exception e) {e.printStackTrace();}return null;}/*** 生成條形碼** @param contents* @param width* @param height* @param imgPath*/// int width = 105, height = 50; 長度很容易報錯:NotFoundExceptionpublic void encodeBarCode(String contents, int width, int height, String imgPath) {int codeWidth = 3 + // start guard(7 * 6) + // left bars5 + // middle guard(7 * 6) + // right bars3; // end guardcodeWidth = Math.max(codeWidth, width);try {BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.EAN_13, codeWidth, height, null);MatrixToImageWriter.writeToStream(bitMatrix, "png",new FileOutputStream(imgPath));} catch (Exception e) {e.printStackTrace();}}/*** 解析條形碼** @param imgPath* @return*/public String decodeBarCode(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)); /* Map<DecodeHintType, Object> hints = new Hashtable<>();hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);hints.put(DecodeHintType.CHARACTER_SET, "utf-8");result = new MultiFormatReader().decode(bitmap, hints);*/result = new MultiFormatReader().decode(bitmap, null);return result.getText();} catch (Exception e) {e.printStackTrace();}return null;}}

測試:特別注意 高寬 容易報錯:NotFoundException

public static void main(String[] args) throws Exception {String imgPath = "D:\\test.png";// 益達(dá)無糖口香糖的條形碼String contents = "6923450657713";int width = 105, height = 50;ZxingUtils handler = new ZxingUtils();handler.encodeBarCode(contents, width, height, imgPath);String barcode = handler.decodeBarCode(imgPath);System.out.println(barcode);handler.encodeQRCode("abc123中文@#\\", 200, 200, imgPath);String qrcode = handler.decodeQRCode(imgPath);System.out.println(qrcode);}

?jar下載地址:zxing.jar

轉(zhuǎn)載于:https://www.cnblogs.com/manusas/p/6801436.html

總結(jié)

以上是生活随笔為你收集整理的ZXing二维码和条形码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。