這篇文章主要介紹“Java怎么生成二維碼圖片”,在日常操作中,相信很多人在Java怎么生成二維碼圖片問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java怎么生成二維碼圖片”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、云浮ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的云浮網(wǎng)站制作公司
ZXing
是一個開源 Java 類庫用于解析多種格式的 1D/2D 條形碼。目標(biāo)是能夠?qū)R編碼、Data Matrix、UPC的1D條形碼進(jìn)行解碼。其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</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;
Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
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);
HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
return result.getText();
}
到此,關(guān)于“Java怎么生成二維碼圖片”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站名稱:Java怎么生成二維碼圖片
本文URL:http://aaarwkj.com/article32/pdphsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、用戶體驗、網(wǎng)站改版、Google、軟件開發(fā)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)