因为是非安卓的环境,所以解析的话是不用去调相册或是拍照的。一般是直接用图片文件去解析,所以如果要在电脑上操作是非常简单的。
这里使用的工具是ZXING,在我的Eclipse上使用需要有两个包分别如下。

由于ZXING可以生产和解析非常多种的一维和二维码,这里只介绍二维码(QR_CODE)
生成二维码
public static void main(String[] args) throws WriterException, FileNotFoundException, IOException, NotFoundException, ChecksumException, FormatException {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode("https://www.baidu.com", BarcodeFormat.QR_CODE, 150, 150);
MatrixToImageWriter.writeToStream(matrix, "png", new FileOutputStream("holyQR.png"));
}
 如此便生成了,为了验证,可以拿出手机扫扫自己生成的码。
解析二维码
有Writer就有reader,所以读取的方法也和上述类似。
public static void ReaderQRCode() throws FileNotFoundException, IOException, NotFoundException, ChecksumException, FormatException {
QRCodeReader reader = new QRCodeReader();
BufferedImage read = ImageIO.read(new FileInputStream(new File("holyQR.png")));
LuminanceSource bufferedImageLuminanceSource = new BufferedImageLuminanceSource(read);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(bufferedImageLuminanceSource));
Result decode = reader.decode(binaryBitmap);
System.out.println(decode.getText());
}

|