/**
* 二维码生成 依赖2个jar包: zxing-core, zxing-javase
*/
@Test
public void testZxcode() {
String contents = "https://www.yf2017.top"; // 可以是链接、文字或其他
contents = "无论你是胖是瘦,你都无法否认夏天真的很热。";
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 字符编码
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, 300, 300, hints);
} catch (WriterException e) {
e.printStackTrace();
}
File file = new File("D://qrcodeImage.png");
try {
MatrixToImageWriter.writeToFile(matrix, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 二维码内容读取
*/
@Test
public void testZxcode2() {
File file = new File("D://qrcodeImage.png");
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = null;
try {
result = new MultiFormatReader().decode(bitmap, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
System.out.println(result.toString());
}
java二维码生成与读取
- 本文链接: https://caigenli.github.io/2014/07/29/java二维码生成与读取/
- 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!