大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java二維碼生成器zxing的介紹

Java二維碼生成器zxing的介紹

更新時間:2022-10-24 10:10:25 來源:動力節(jié)點 瀏覽2029次

zxing是最簡單且最好用的。如果要生成二維碼圖片,那么我們只需要它的核心庫即可。只需將以下依賴項添加到您的 Maven 項目中。

<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.3.2</version>
</dependency>

如果你想通過命令行讀取二維碼圖片,那么我們需要使用它的JavaSE庫。您可以為其添加以下依賴項。

<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>javase</artifactId>
	<version>3.3.2</version>
</dependency>

這還將讓您知道從命令行運行所需的兩個額外依賴項,如下圖所示。我們必須將這些 jars 添加到類路徑中以運行客戶端應用程序以讀取 QR 碼圖像。我們將在本教程的后面部分看到這一點。

zxing 示例生成二維碼圖片

這是您可以使用 zxing API 創(chuàng)建二維碼圖像的程序。GenerateQRCode.java

package com.journaldev.qrcode.generator;
import java.awt.Color;
import java.awt.Graphics2D;
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.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class GenerateQRCode {
	public static void main(String[] args) throws WriterException, IOException {
		String qrCodeText = "https://www.journaldev.com";
		String filePath = "JD.png";
		int size = 125;
		String fileType = "png";
		File qrFile = new File(filePath);
		createQRImage(qrFile, qrCodeText, size, fileType);
		System.out.println("DONE");
	}
	private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType)
			throws WriterException, IOException {
		// Create the ByteMatrix for the QR-Code that encodes the given String
		Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
		hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
		// Make the BufferedImage that are to hold the QRCode
		int matrixWidth = byteMatrix.getWidth();
		BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
		image.createGraphics();
		Graphics2D graphics = (Graphics2D) image.getGraphics();
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, matrixWidth, matrixWidth);
		// Paint and save the image using the ByteMatrix
		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, j, 1, 1);
				}
			}
		}
		ImageIO.write(image, fileType, qrFile);
	}
}

zxing示例讀取二維碼

如果您沒有移動應用程序來測試它,請不要擔心。您可以通過命令行使用 zxing API 讀取二維碼。下面是讀取二維碼圖像文件的命令。請注意 zxing 依賴的類路徑中的附加 jar。

$java -cp $HOME/.m2/repository/com/google/zxing/javase/3.3.2/javase-3.3.2.jar:.:$HOME/.m2/repository/com/google/zxing/core/3.3.2/core-3.3.2.jar:$HOME/.m2/repository/com/beust/jcommander/1.72/jcommander-1.72.jar:$HOME/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.3.1/jai-imageio-core-1.3.1.jar com.google.zxing.client.j2se.CommandLineRunner JD.png

下圖顯示了此命令產生的輸出。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产免费三a在线 | 五月天激情视频 | 青青国产成人久久激情911 | 亚洲欧美一二三区 | 91资源在线视频 | 亚洲午夜日韩高清一区 | 一级毛片视频在线 | 全午夜免费一级毛片 | 人人爽天天碰天天躁夜夜躁 | 爱操成人网 | 亚洲欧美日韩高清一区二区一 | 精品国产成人综合久久小说 | 四虎影视在线看免费观看 | 免费播放美女一级毛片 | 九九黄色大片 | 级毛片久久久毛片精品毛片 | 嘿咻成人免费视频欧美激情 | 国产成人精品高清不卡在线 | 国产男女性特黄录像 | 亚洲成a人一区二区三区 | 天天做天天爱天天爽 | ova熟肉动漫在线 | 久久久久激情免费观看 | 亚洲成人黄色在线 | 国内久久久久高清影视 | 青青草免费视频在线播放 | 青青青国产深夜福利视频 | 性网站免费 | 亚洲不卡视频 | 岛国片欧美一级毛片 | 精品伊人久久久 | 久久亚洲精品成人综合 | 看特级大黄一片 | 欧美末成年videos在线观看 | 四虎永久免费地址ww417 | 99国产欧美久久精品 | 不卡一区二区在线 | 天天摸夜夜摸爽爽狠狠婷婷97 | 免费黄色毛片视频 | 日本一级毛片片免费观看 | 婷婷综合久久狠狠色99h |