Java 生成二維碼實戰(zhàn)
點擊上方“碼農(nóng)突圍”,馬上關(guān)注
這里是碼農(nóng)充電第一站,回復(fù)“666”,獲取一份專屬大禮包
真愛,請設(shè)置“星標(biāo)”或點個“在看”
ZXing?是一個開源 Java 類庫用于解析多種格式的 1D/2D 條形碼。目標(biāo)是能夠?qū)R編碼、Data Matrix、UPC的1D條形碼進行解碼。其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。<dependency>
??<groupId>com.google.zxinggroupId>
??<artifactId>coreartifactId>
??<version>3.3.0version>
dependency>
<dependency>
??<groupId>com.google.zxinggroupId>
??<artifactId>javaseartifactId>
??<version>3.3.0version>
dependency>1. com.google.zxing.MultiFormatWriter?根據(jù)內(nèi)容以及圖像編碼參數(shù)生成圖像2D矩陣。2. com.google.zxing.client.j2se.MatrixToImageWriter?根據(jù)圖像矩陣生成圖片文件或圖片緩存?BufferedImage?。
public?void?encode(String?content,?String?filepath) throws WriterException, IOException {
??int width =?100;
??int height =?100;
??MapObject> encodeHints =?new?HashMapObject>();
??encodeHints.put(EncodeHintType.CHARACTER_SET,?"UTF-8");
??BitMatrix bitMatrix =?new?MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, encodeHints);
??Path path = FileSystems.getDefault().getPath(filepath);
??MatrixToImageWriter.writeToPath(bitMatrix,?"png", path);
} 解析二維碼圖片
javax.imageio.ImageIO?讀取圖片文件,并存為一個?java.awt.image.BufferedImage對象。java.awt.image.BufferedImage?轉(zhuǎn)換為 ZXing 能識別的com.google.zxing.BinaryBitmap?對象。3. com.google.zxing.MultiFormatReader?根據(jù)圖像解碼參數(shù)來解析com.google.zxing.BinaryBitmap?。public?String?decode(String?filepath) throws IOException, NotFoundException {
??BufferedImage bufferedImage = ImageIO.read(new?FileInputStream(filepath));
??LuminanceSource source =?new?BufferedImageLuminanceSource(bufferedImage);
??Binarizer binarizer =?new?HybridBinarizer(source);
??BinaryBitmap bitmap =?new?BinaryBitmap(binarizer);
??HashMapObject> decodeHints =?new?HashMapObject>();
??decodeHints.put(DecodeHintType.CHARACTER_SET,?"UTF-8");
??Result result =?new?MultiFormatReader().decode(bitmap, decodeHints);
??return?result.getText();
} (完)
碼農(nóng)突圍資料鏈接
1、臥槽!字節(jié)跳動《算法中文手冊》火了,完整版 PDF 開放下載!
2、計算機基礎(chǔ)知識總結(jié)與操作系統(tǒng) PDF 下載
3、艾瑪,終于來了!《LeetCode Java版題解》.PDF
4、Github 10K+,《LeetCode刷題C/C++版答案》出爐.PDF歡迎添加魚哥個人微信:smartfish2020,進粉絲群或圍觀朋友圈
評論
圖片
表情

