如何使用 ZXing 库用Java生成和读取二维码
QRCode缩写为Quic Response Code ,我们现在对 QRCodes 非常熟悉。它用于经过身份验证的快速在线支付。二维码使用四种标准化的编码模式(数字、字母数字、字节/二进制和汉字)来有效地存储数据;也可以使用扩展。
QRCode是黑白方块的排列,可以使用各种 QRCode 扫描仪读取,并且在今天很方便,因为每个智能手机都有 QRcode 扫描仪应用程序。
用于生成二维码的库(ZXing)
ZXing(“斑马线”)是Java中用于二维码处理的流行 API。它的库有多个组件,我们将在我们的Java示例中使用“核心”来创建 QR 码。
如何生成二维码?
以下代码是创建 QR 码图像的示例。
- 从这里下载 ZXING 库。
- 在maven 文件中添加 ZXING 依赖项。
XML
com.google.zxing
core
3.3.0
com.google.zxing
javase
3.3.0
Java
// Java code to generate QR code
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class MyQr {
// Function to create the QR code
public static void createQR(String data, String path,
String charset, Map hashMap,
int height, int width)
throws WriterException, IOException
{
BitMatrix matrix = new MultiFormatWriter().encode(
new String(data.getBytes(charset), charset),
BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToFile(
matrix,
path.substring(path.lastIndexOf('.') + 1),
new File(path));
}
// Driver code
public static void main(String[] args)
throws WriterException, IOException,
NotFoundException
{
// The data that the QR code will contain
String data = "www.geeksforgeeks.org";
// The path where the image will get saved
String path = "demo.png";
// Encoding charset
String charset = "UTF-8";
Map hashMap
= new HashMap();
hashMap.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.L);
// Create the QR code and save
// in the specified folder
// as a jpg file
createQR(data, path, charset, hashMap, 200, 200);
System.out.println("QR Code Generated!!! ");
}
}
Java
// Java code to read the QR code
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCode {
// Function to read the QR file
public static String readQR(String path, String charset,
Map hashMap)
throws FileNotFoundException, IOException,
NotFoundException
{
BinaryBitmap binaryBitmap
= new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(
new FileInputStream(path)))));
Result result
= new MultiFormatReader().decode(binaryBitmap);
return result.getText();
}
// Driver code
public static void main(String[] args)
throws WriterException, IOException,
NotFoundException
{
// Path where the QR code is saved
String path = "F:\user\QRCodes";
// Encoding charset
String charset = "UTF-8";
Map hashMap
= new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.L);
System.out.println(
"QRCode output: "
+ readQRCode(filePath, charset, hintMap));
}
}
- 编写代码生成二维码,并将其保存为原生文件夹中的jpg 文件。
Java
// Java code to generate QR code
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class MyQr {
// Function to create the QR code
public static void createQR(String data, String path,
String charset, Map hashMap,
int height, int width)
throws WriterException, IOException
{
BitMatrix matrix = new MultiFormatWriter().encode(
new String(data.getBytes(charset), charset),
BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToFile(
matrix,
path.substring(path.lastIndexOf('.') + 1),
new File(path));
}
// Driver code
public static void main(String[] args)
throws WriterException, IOException,
NotFoundException
{
// The data that the QR code will contain
String data = "www.geeksforgeeks.org";
// The path where the image will get saved
String path = "demo.png";
// Encoding charset
String charset = "UTF-8";
Map hashMap
= new HashMap();
hashMap.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.L);
// Create the QR code and save
// in the specified folder
// as a jpg file
createQR(data, path, charset, hashMap, 200, 200);
System.out.println("QR Code Generated!!! ");
}
}
输出文件名为demo.jpg
扫描此二维码后,您将被重定向到geeksforgeeks主页。
如何生成二维码?
生成二维码后,我们还可以使用ZXing 库读取二维码图像文件。下面是执行此操作的代码。
Java
// Java code to read the QR code
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCode {
// Function to read the QR file
public static String readQR(String path, String charset,
Map hashMap)
throws FileNotFoundException, IOException,
NotFoundException
{
BinaryBitmap binaryBitmap
= new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(
new FileInputStream(path)))));
Result result
= new MultiFormatReader().decode(binaryBitmap);
return result.getText();
}
// Driver code
public static void main(String[] args)
throws WriterException, IOException,
NotFoundException
{
// Path where the QR code is saved
String path = "F:\user\QRCodes";
// Encoding charset
String charset = "UTF-8";
Map hashMap
= new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.L);
System.out.println(
"QRCode output: "
+ readQRCode(filePath, charset, hintMap));
}
}
输出:
QRCode output: www.geeksforgeeks.org