码迷,mamicode.com
首页 > 编程语言 > 详细

二维码生成器和解析器-java

时间:2014-04-30 02:21:35      阅读:601      评论:0      收藏:0      [点我收藏+]

标签:com   http   blog   style   class   div   img   code   java   size   javascript   

 

  1.工具zxing2.1----下载地址:http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.1.zip&can=2&q=

    所有版本下载地址:http://code.google.com/p/zxing/downloads/list

  2.需要的jar包,zxing的core.jar和zxing的javase.jar这两个jar包在zxing2.1版本中地址是

    zxing/zxingorg/web/WEB-INF/lib里能找得到

  代码

bubuko.com,布布扣
 1     private static final int BLACK = 0xff000000;
 2     private static final int WHITE = 0xFFFFFFFF;
 3     public static void main(String[] args) throws Exception {
 4         File file = new File("f://test.png");
 5         TestImg test = new TestImg();
 6         test.encode("duwenlei", file,BarcodeFormat.QR_CODE, 200, 200, null);
 7         test.decode(file);
 8     }
 9 
10     public void encode(String contents, File file, BarcodeFormat format,
11             int width, int height, Map<EncodeHintType, ?> hints) throws Exception {
12         BitMatrix bitMatrix =  new MultiFormatWriter().encode(contents, format, width, height);
13         writeToFile(bitMatrix, "png", file);
14     }
15     /**
16      * 生成二维码图片<br>
17      * 
18      * @param matrix
19      * @param format
20      *            图片格式
21      * @param file
22      *            生成二维码图片位置
23      * @throws IOException
24      */
25     public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
26         BufferedImage image = toBufferedImage(matrix);
27         ImageIO.write(image, format, file);
28     }
29 
30     /**
31      * 生成二维码内容<br>
32      * 
33      * @param matrix
34      * @return
35      */
36     public static BufferedImage toBufferedImage(BitMatrix matrix) {
37         int width = matrix.getWidth();
38         int height = matrix.getHeight();
39         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
40         for (int x = 0; x < width; x++) {
41             for (int y = 0; y < height; y++) {
42                 image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
43             }
44         }
45         return image;
46     }
47 
48     /**
49      * 解析QRCode二维码
50      */
51     @SuppressWarnings("unchecked")
52     public void decode(File file) {
53         try {
54             BufferedImage image;
55             try {
56                 image = ImageIO.read(file);
57                 if (image == null) {
58                     System.out.println("Could not decode image");
59                 }
60                 LuminanceSource source = new BufferedImageLuminanceSource(image);
61                 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
62                 Result result;
63                 @SuppressWarnings("rawtypes")
64                 Hashtable hints = new Hashtable();
65                 //解码设置编码方式为:utf-8
66                 hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
67                 result = new MultiFormatReader().decode(bitmap, hints);
68                 String resultStr = result.getText();
69                 System.out.println("解析后内容:" + resultStr);
70             } catch (IOException ioe) {
71                 System.out.println(ioe.toString());
72             } catch (ReaderException re) {
73                 System.out.println(re.toString());
74             }
75         } catch (Exception ex) {
76             System.out.println(ex.toString());
77         }
78     }
bubuko.com,布布扣

 

 

 

 

 

 

 


摘自:http://www.cnblogs.com/hongten/archive/2012/10/26/java_qrcode.html

二维码生成器和解析器-java,布布扣,bubuko.com

二维码生成器和解析器-java

标签:com   http   blog   style   class   div   img   code   java   size   javascript   

原文地址:http://www.cnblogs.com/duwenlei/p/3695896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!