ZXing生成二维码
生活随笔
收集整理的這篇文章主要介紹了
ZXing生成二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<!-- https://mvnrepository.com/artifact/com.google.zxing/core --> ? ?<dependency> ? ? ?<groupId>com.google.zxing</groupId> ? ? ?<artifactId>core</artifactId> ? ? ?<version>3.3.2</version> ? ?</dependency> ? ? ? ?<!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> ? ?<dependency> ? ? ?<groupId>com.google.zxing</groupId> ? ? ?<artifactId>javase</artifactId> ? ? ?<version>3.3.2</version> ? ?</dependency>
具體事務代碼
package cn.silica.Utils; ? import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; ? import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; ? import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK; import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE; ? /** * 二維碼生成及解析工具 */ public class QRCodeUtil { ? ?/** ? ? * 生成無中間log的二維碼 ? ? * @param contexts 二位中內容 ? ? * @param qrWidth 二維碼寬度 ? ? * @param qrHeight 二維碼長度 ? ? * @param outputStream 輸出流 ? ? */ ? ?public static void createNoLogQRCode(String contexts, int qrWidth, int qrHeight ,OutputStream outputStream){ ? ? ? ?Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType, Object>(); ? ? ? ?//設置二維碼校錯級別 LMQH 7%,15%,25%,30%表示二維碼被圖形被污染時可自動復原的比例? ? ? ? ?//同時糾錯級別越高,糾錯信息占用的空間就越多,能存儲的有用信息就越少 ? ? ? ?hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); ? ? ? ?//設置編碼格式 ? ? ? ?hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); ? ? ? ?//設置二維碼邊界大小1,2,3,4 默認為4,最大 ? ? ? ?hints.put(EncodeHintType.MARGIN,1); ? ? ? ?try { ? ? ? ? ? ?//MultiFormatWriter 二維碼格式化輸出 ? ? ? ? ? ?//MultiFormatWriter ? ? ? ? ? ?BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contexts, BarcodeFormat.QR_CODE,qrWidth,qrHeight,hints); ? ? ? ? ? ?//二維碼轉換為圖像 ? ? ? ? ? ?MatrixToImageWriter.writeToStream(bitMatrix,"png",outputStream); ? ? ? } catch (Exception e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? } ? } ? ? ?/** ? ? * 生成帶logo的二維碼 ? ? * @param contexts 二維碼中的內容 ? ? * @param qrWidth 二維碼的寬度 ? ? * @param qrHeight 二維碼的高度 ? ? * @param outputStream 二維碼輸出地 ? ? * @param imgWidth 二維碼中logo的寬度 ? ? * @param imgHeight 二維碼中logo的高度 ? ? * @param imgPath 二維碼中logo的原地址 ? ? * @throws IOException ? ? */ ? ?public static void createLogQRCode(String contexts, int qrWidth, int qrHeight ,OutputStream outputStream,int imgWidth,int imgHeight,String imgPath) throws IOException { ? ? ? ?Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType, Object>(); ? ? ? ?//設置二維碼校錯級別 LMQH 7%,15%,25%,30%表示二維碼被圖形被污染時可自動復原的比例? ? ? ? ?//同時糾錯級別越高,糾錯信息占用的空間就越多,能存儲的有用信息就越少 ? ? ? ?hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); ? ? ? ?//設置編碼格式 ? ? ? ?hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); ? ? ? ?//設置二維碼邊界大小1,2,3,4 默認為4,最大 ? ? ? ?hints.put(EncodeHintType.MARGIN,1); ? ? ? ?try { ? ? ? ? ? ?//MultiFormatWriter 二維碼格式化輸出 ? ? ? ? ? ?BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contexts, BarcodeFormat.QR_CODE,qrWidth,qrHeight,hints); ? ? ? ? ? ?//獲取二維碼高度 ? ? ? ? ? ?qrWidth = bitMatrix.getWidth(); ? ? ? ? ? ?qrHeight = bitMatrix.getHeight(); ? ? ? ? ? ? ?//創建緩沖流 ? ? ? ? ? ?BufferedImage bufferedImage = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_RGB); ? ? ? ? ? ? ?//將二維碼放入緩沖流 ? ? ? ? ? ?for (int i = 0; i < qrWidth; i++) { ? ? ? ? ? ? ? ?for (int j = 0; j < qrHeight; j++) { ? ? ? ? ? ? ? ? ? ?// 循環將二維碼內容寫入圖片 ? ? ? ? ? ? ? ? ? ?bufferedImage.setRGB(i, j, bitMatrix.get(i, j) ? BLACK : WHITE); ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? ? ? ?//寫入logo圖像 ? ? ? ? ? ?File logoFile = new File(imgPath); ? ? ? ? ? ?Graphics2D gs2 = bufferedImage.createGraphics();//該對象可以繪制BufferedImage對象 ? ? ? ? ? ?BufferedImage logoImg = ImageIO.read(logoFile);//將圖片寫入到BufferedImage對象中 ? ? ? ? ? ?//如果logo長寬超過二維碼長寬的20%則設定其長寬為二維碼的20%,如果小于20%則以logo實際長寬為準 ? ? ? ? ? ?int logoWidth = logoImg.getWidth()>bufferedImage.getWidth()*2/10 ? bufferedImage.getWidth()*2/10 : logoImg.getWidth(); ? ? ? ? ? ?int logoHeight = logoImg.getHeight()>bufferedImage.getHeight()*2/10 ? bufferedImage.getHeight()*2/10 : logoImg.getHeight(); ? ? ? ? ? ? ?//x,y為logo相對于二維碼圖片的位置 ? ? ? ? ? ?int x = (qrWidth - logoWidth) / 2; ? ? ? ? ? ?int y = (qrHeight - logoHeight) / 2; ? ? ? ? ? ? ?//開始繪制logo圖片 ? ? ? ? ? ?gs2.drawImage(logoImg,x,y,logoWidth,logoHeight,null); ? ? ? ? ? ?//生成一個可設置圓角的矩形 ? ? ? ? ? ?gs2.drawRoundRect(x,y,logoWidth,logoHeight,0,0); ? ? ? ? ? ?//邊框寬度 ? ? ? ? ? ?gs2.setStroke(new BasicStroke(3)); ? ? ? ? ? ?gs2.setColor(Color.green);//設置背景顏色 ? ? ? ? ? ?//生成特定矩形的輪廓 ? ? ? ? ? ?gs2.drawRect(x,y,logoWidth,logoHeight); ? ? ? ? ? ?gs2.dispose();//處理圖像內容并釋放資源,此后gs2將不能再被調用 ? ? ? ? ? ?logoImg.flush(); ? ? ? ? ? ?bufferedImage.flush(); ? ? ? ? ? ?//將二維碼返回到頁面 ? ? ? ? ? ?ImageIO.write(bufferedImage,"png",outputStream); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? } ? ? } } ?參考:
https://blog.csdn.net/rongku/article/details/51872156
總結
以上是生活随笔為你收集整理的ZXing生成二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: String String.value
- 下一篇: 深入理解Tomcat和Jetty源码之第