Java生成二維碼與解碼二維碼

下載QRCode.jar包
下載完成後在cmd命令行執行以下命令

mvn install:install-file -Dfile = QRCode的路徑 -DgroupId=QRCode -DartifactId=QRCode -Dversion=3.0 -Dpackaging=jar

注意: 需要配置maven環境變量
在pom.xml文件裏添加jar包

<dependency>
 <groupId>QRCode</groupId>
 <artifactId>QRCode</artifactId>
 <version>3.0</version>
</dependency> 

運行類

package test;

public class Test {

	public static void main(String[] args) throws Exception {
		
		// 生成後的二維碼存放的路徑
		String imgPath = "D://二維碼.png";
		// 二維碼的內容
		String content = "hello world";
		// 跳轉網頁
//		String content = "http://www.baidu.com";
		
		/**
		 * 生成二維碼
		 */
		QRCodeUtil qrCodeUtil = new QRCodeUtil();
		qrCodeUtil.encoderQRCode(content, imgPath, "png", 17);
	
		/**
		 * 解碼二維碼
		 */
		String imgContent = qrCodeUtil.decodeQrCode(imgPath);
		System.out.println("imgContent:" + imgContent);
		
	}
}

生成二維碼的方法

	public void encoderQRCode(String content, String imgPath, String imgType, int size) {
		File file = new File(imgPath);

		BufferedImage bufImg = qrCodeCommon(content, imgType, size);

		try {
			ImageIO.write(bufImg, imgType, file);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 生成一個二維碼的BufferedImage
	 * 
	 * @param content
	 *            內容
	 * @param imgType
	 *            類型
	 * @param size
	 *            二維碼邊長
	 * @return
	 */
	public BufferedImage qrCodeCommon(String content, String imgType, int size) {
		// BufferedImage.TYPE_INT_ARGB:RGB三基色
		BufferedImage bufImg = null;

		Qrcode qrcode = new Qrcode();
		// 設置二維碼的拍錯率
		qrcode.setQrcodeErrorCorrect('M');
		// 可存放的信息
		qrcode.setQrcodeEncodeMode('B');
		// 尺寸:取值範圍:1-40
		qrcode.setQrcodeVersion(size);
		byte[] bytes = null;
		try {
			bytes = content.getBytes("utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 將內容轉換成字節數組
		boolean[][] codeOut = qrcode.calQrcode(bytes);

		int imgSize = 67 + 12 * (size - 1);
		// BufferedImage:內存中的圖片
		bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB);
		// 創建一個畫板
		Graphics2D gs = bufImg.createGraphics();
		// 設置二維碼背景顏色:一般爲白色
		gs.setBackground(Color.WHITE);
		// 初始化,x和y爲0代表左上角,size一樣代表是個正方形
		gs.clearRect(0, 0, imgSize, imgSize);
		// 設置二維碼顏色:一般爲黑色
		gs.setColor(Color.BLACK);

		for (int i = 0; i < codeOut.length; i++) {
			for (int j = 0; j < codeOut.length; j++) {
				if (codeOut[i][j]) {
					gs.fillRect(j * 3, i * 3, 3, 3);
				}
			}
		}

		// 給二維碼中間增加logo
		try {
			// logo路徑
			File file = new File("D://jieZai.jpg");
			// 加載一個Image對象
			Image logo = ImageIO.read(file);
			int width = bufImg.getWidth();
			int height = bufImg.getHeight();

			// 在已生成的二維碼上畫logo
			gs.drawImage(logo, imgSize / 5 * 2, imgSize / 5 * 2, width / 5, height / 5, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		gs.dispose(); // 釋放空間
		bufImg.flush(); // 刷新

		return bufImg;
	}

解碼的方法

   /**
	 * 解碼
	 * 
	 * @param imgPath  圖片路徑
	 * @return
	 */
	public String decodeQrCode(String qrImg) {
		File file = new File(qrImg);
		Result result = null;
		if (!qrImg.isEmpty()) {
			try {
				// BufferedImage內存中的圖片
				BufferedImage image = ImageIO.read(file);

				BinaryBitmap binaryBitmap = new BinaryBitmap(
						new HybridBinarizer(new BufferedImageLuminanceSource(image)));

				// 定義二維碼的參數:
				HashMap<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
				// 定義字符集
				hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
				// 解密
				result = new MultiFormatReader().decode(binaryBitmap, hints);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result.getText();
	}

生成字符串內容的二維碼
在這裏插入圖片描述
控制檯
在這裏插入圖片描述
生成跳轉網頁的二維碼
在這裏插入圖片描述
控制檯
在這裏插入圖片描述

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