使用谷歌的開源包ZXingjava
maven引入以下兩個包便可微信
<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.0.0</version> </dependency>
一、工具類maven
package com.unicom.zxing; import com.google.zxing.*; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeReader; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Hashtable; /** * 二維碼生成和讀的工具類 * */ public class ZXingUtil { /** * 生成包含字符串信息的二維碼圖片 * @param outputStream 文件輸出流路徑 * @param content 二維碼攜帶信息 * @param qrCodeSize 二維碼圖片大小 * @param imageFormat 二維碼的格式 * @throws WriterException * @throws IOException */ public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{ //設置二維碼糾錯級別MAP Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矯錯級別 QRCodeWriter qrCodeWriter = new QRCodeWriter(); //建立比特矩陣(位矩陣)的QR碼編碼的字符串 BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap); // 使BufferedImage勾畫QRCode (matrixWidth 是行二維碼像素點) int matrixWidth = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, matrixWidth, matrixWidth); // 使用比特矩陣畫並保存圖像 graphics.setColor(Color.BLACK); for (int i = 0; i < matrixWidth; i++){ for (int j = 0; j < matrixWidth; j++){ if (byteMatrix.get(i, j)){ graphics.fillRect(i-100, j-100, 1, 1); } } } return ImageIO.write(image, imageFormat, outputStream); } /** * 讀二維碼並輸出攜帶的信息 */ public static void readQrCode(InputStream inputStream) throws IOException{ //從輸入流中獲取字符串信息 BufferedImage image = ImageIO.read(inputStream); //將圖像轉換爲二進制位圖源 LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Result result = null ; try { result = reader.decode(bitmap); } catch (ReaderException e) { e.printStackTrace(); } System.out.println(result.getText()); } /** * 測試代碼 * @throws WriterException */ public static void main(String[] args) throws IOException, WriterException { //生成二維碼到E盤 createQrCode(new FileOutputStream(new File("E:\\test.jpg")),"https://www.baidu.com/",900,"JPEG"); //下面是讀取二維碼內容 readQrCode(new FileInputStream(new File("E:\\test.jpg"))); } }
直接運行Main方法,便可在E盤生成二維碼工具
微信掃一掃,便可跳轉到百度網頁測試