码迷,mamicode.com
首页 > 其他好文 > 详细

利用zxing生成二维码

时间:2015-08-26 17:43:10      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

使用zxing类库可以很容易生成二维码QRCode,主要代码如下:

 private Bitmap createQRCode(String str,int width,int height)
 {
        Bitmap bmp=null;
        if(str.equals(""))return null;
        try
        {
             MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
             Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
             hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
             hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
             BitMatrix bitMatrix=null;
             try 
             {
                  bitMatrix = multiFormatWriter.encode(str, BarcodeFormat.QR_CODE, width, height,hints);
             } 
             catch (WriterException e) 
             {
                e.printStackTrace();
             }
             
             int[] pixels = new int[width * height];
             for (int y = 0; y < height; y++) 
             {
                 for (int x = 0; x < width; x++) 
                 {
                     if (bitMatrix.get(x, y)) 
                     {
                         pixels[y * width + x] = 0xff000000;
                     } 
                     else 
                     {
                         pixels[y * width + x] = 0xffffffff;
                     }
                 }
             }
             bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
             bmp.setPixels(pixels, 0, width, 0, 0, width, height);
           
        }
        catch(Exception e)
        {
            e.printStackTrace();
            bmp=null;
        }
        return bmp;
 }

 

利用zxing生成二维码

标签:

原文地址:http://www.cnblogs.com/rainboy2010/p/4760838.html

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