/**********
* 生成二维码
*/
public static void main(String[] args) {
String content = "酒至颜自解,声和心亦宣。";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, 300, 100, hints);
} catch (WriterException e) {
e.printStackTrace();
}
File file = new File("D:/qrimage.png");
try {
MatrixToImageWriter.writeToFile(matrix, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/******
* 解析这个二维码
* @param args
*/
public static void main(String[] args) {
File file = new File("D:/qrimage.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<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
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 生成二维码
于 2012-09-11 09:14:23 首次发布