您的位置:首页 > 其它

ZXING生成二维码以及解析

2016-08-11 17:23 190 查看
public String qrCreate() {
try {
String str = "https://www.baidu.com/";// 二维码内容
String path = "D:\\hwy.png";
BitMatrix byteMatrix;
byteMatrix = new MultiFormatWriter().encode(
// 这里防止有乱码
new String(str.getBytes(), "iso-8859-1"),
BarcodeFormat.QR_CODE, 200, 200);
File file = new File(path);

MatrixToImageWriter.writeToFile(byteMatrix, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

@Override
public String parseQR() {
try {
String imgPath = "D:\\hwy.png";
File file = new File(imgPath);
BufferedImage image;
try {
image = ImageIO.read(file);
if (image == null) {
System.out.println("Could not decode image");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
source));
Result result;
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
System.out.println(resultStr);

} catch (IOException ioe) {
System.out.println(ioe.toString());
} catch (ReaderException re) {
System.out.println(re.toString());
}

} catch (Exception ex) {

}
return "";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: