package cn.qrcode; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.Binarizer; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.Result; 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; /** * 二维码生成和解析 */ public class QRTest { public static void main(String[] args) { //create(); parse(); } private static void create() { try { String content = "120605181003;http://www.cnblogs.com/jtmjx"; String path = "src"; MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); // 用于设置QR二维码参数 Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // QR_CODE: //这里是二维码 // CODE_128: //这个可以生成 // PDF_417: //这个可以生成 // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints); File file1 = new File(path, "dd.jpg"); MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1); System.out.println("图片完成!"); } catch (Exception e) { e.printStackTrace(); } } /** * */ public static void parse() { try { MultiFormatReader formatReader = new MultiFormatReader(); String filePath = "src/a.jpg"; File file = new File(filePath); BufferedImage image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); Result result = formatReader.decode(binaryBitmap, hints); System.out.println("result = " + result.toString()); System.out.println("resultFormat = " + result.getBarcodeFormat()); System.out.println("resultText = " + result.getText()); } catch (Exception e) { e.printStackTrace(); } } }
最近下载更多
xuexizhuanyong23 LV16
2022年3月23日
yangctz LV24
2021年9月29日
yiangeee LV9
2021年1月14日
abcd11112 LV6
2020年8月12日
hanweinan6 LV12
2020年6月29日
huasir2018 LV14
2020年5月9日
霸道小盆友 LV3
2020年4月30日
sonofy123 LV9
2020年2月14日
kong.yee LV40
2020年2月1日
15838634741 LV18
2019年12月4日
最近浏览更多
crosa_Don LV18
2022年9月22日
liys1234 LV9
2022年4月22日
xuexizhuanyong23 LV16
2022年3月23日
newhaijun LV15
2022年3月2日
Q375892799 LV9
2022年2月25日
yangctz LV24
2021年9月29日
whfuai LV14
2021年6月16日
yiangeee LV9
2021年1月14日
MrReady LV14
2020年12月27日
1126055836 LV13
2020年11月6日