使用java生成、解析二維碼

一、導入jar包(在工程中可以導入依賴)

在一個普通的java項目中,我們需要導入相關的jar包來生成和解析二維碼,但是在一個maven工程等等的web項目中,我們可以通過帶入相關依賴來實現。
jar包鏈接: https://pan.baidu.com/s/1Vk6QnyFWQuMxm85RsMOrAg
提取碼:9nbk
如果添加依賴的話,可以導入下面的Zxing依賴:

	<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>

下面我以導包爲例分別來演示生成、解析二維碼。

二、生成二維碼

代碼:

package com.qrcode;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class CreatQRCode {
	public static void main(String[] args) throws IOException {
		
		//定義二維碼的相關參數
		int size = 300;//尺寸
		String content = "第一個二維碼";//二維碼的內容
		
		//二維碼要生成的數據參數
		Hashtable<EncodeHintType, Object> hits = new Hashtable<EncodeHintType, Object>();
		//容錯級別是最高級別
		hits.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);
		//內容的字符集
		hits.put(EncodeHintType.CHARACTER_SET,"UTF-8");
		//二維碼的內容
		hits.put(EncodeHintType.MARGIN, 1);
		
		try {
			//格式輸出流
			MultiFormatWriter out = new MultiFormatWriter();
			//創建二維碼矩陣
			BitMatrix bitMatrix = out.encode(content, BarcodeFormat.QR_CODE, size, size,hits);
		
			//創建一個圖像緩衝區,用來存放二維碼的圖像數據
			BufferedImage bufferQR = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
			
			//將bitMatrix的數據寫到緩衝區圖像中
			for(int i = 0; i < size ; i ++) {
				for(int j = 0; j < size ; j++) {
					
					//從bitMatrix取出每一個像素點的顏色  true爲1  false爲0
					int rgb = bitMatrix.get(i, j)?0xFF000000:0xFFFFFFFF;
					
					//設置bufferQR對應的位置顏色和bitMatrix一致
					bufferQR.setRGB(i, j, rgb);
				}
			}
			
			
			//添加logo,就是二維碼中間的那個圖片
			File logo = new File("本地圖片的地址");
			Image image = ImageIO.read(logo);
			Image src = image.getScaledInstance(60, 60, Image.SCALE_SMOOTH);
			//在緩衝區圖像上繪製logo
			Graphics2D g = bufferQR.createGraphics();
			int x = (size-src.getWidth(null))/2;
			int y = (size-src.getHeight(null))/2;
			g.drawImage(src, x, y,null);
			
			//圓角矩形
			Shape shape = new RoundRectangle2D.Float(x,y,60,60,6,6);
			g.setStroke(new BasicStroke(1f));
			g.setColor(Color.gray);
			g.draw(shape);
			g.dispose();
			
			//生成圖像位置,我這是生成到本地E盤,文件名爲QR.png
			File file = new File("E:\\QR.png");
			
			//將內存中的圖像寫到文件
			ImageIO.write(bufferQR,"png",file);//對應上面的文件類型
		} catch (WriterException e) {
			e.printStackTrace();
		}
	}
}

生成的二維碼:
在這裏插入圖片描述
因爲是採用的google的相關jar包,所以用微信掃的時候需要跳轉查看。

三、解析二維碼

代碼演示:

package com.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQRCode {

	public static void main(String[] args) throws Exception {
		
		try {
			//得到圖片並存入文件
			File file = new File("E:\\QR.png");
			//將圖片文件轉換成內存緩衝區圖像
			BufferedImage buffer = ImageIO.read(file);

			//創建數據源對象
			BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(buffer); 
		
			//創建一個二值化器對象
			HybridBinarizer binarizer = new HybridBinarizer(source);
			
			//使用二值化器得到二進制圖像
			BinaryBitmap bitmap = new BinaryBitmap(binarizer);
			
			//定義解析標準參數
			Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
			hints.put(DecodeHintType.CHARACTER_SET,"UTF-8");
			
			//開始解析二進制圖像
			MultiFormatReader reader = new MultiFormatReader();
			
			//開始解析,獲得解析的結果
			Result rs = reader.decode(bitmap,hints);
			
			//提取數據
			String msg = rs.getText();
			System.out.println(msg);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

運行結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章