二维码 编码原理简介


 

public static void encode(String content, String format, String filePath) {
try {
Hashtable hints = new Hashtable();//
设置编码类型
hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_ENCODING);
//
编码
                          BitMatrix bitMatrix = new QRCodeWriter().encode(content,
BarcodeFormat.QR_CODE, DEFAULT_IMAGE_WIDTH,
DEFAULT_IMAGE_HEIGHT,hints);
//
输出到文件,也可以输出到流
File file = new File(filePath);
MatrixToImageWriter.writeToFile(bitMatrix, format, file);

} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e1) {
e1.printStackTrace();
}
}

解码:  

BufferedImage image = ImageIO.read(file);//读取文件
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
source));
 //
解码
Result result = new MultiFormatReader().decode(bitmap);
String resultStr = result.getText();
System.out.println(resultStr);