android 使用zxing生成中间嵌套logo的二维码

来源:互联网 发布:手机拍照滤镜软件 编辑:程序博客网 时间:2024/06/06 13:16

android 使用zxing生成中间嵌套logo的二维码

标签: android 二维码 嵌套logo的二
 3545人阅读 评论(1) 收藏 举报
 分类:

效果图:


zxing请自行下载

代码:

[java] view plain copy
  1. /** 
  2.      * 生成二维码Bitmap 
  3.      * 
  4.      * @param content   文本内容 
  5.      * @param logoBm    二维码中心的Logo图标(可以为null) 
  6.      * @return 合成后的bitmap 
  7.      */   
  8.     public static Bitmap createQRImage(Context context,String data, Bitmap logoBm) {   
  9.          
  10.         try {   
  11.               
  12.             if (data == null || "".equals(data)) {   
  13.                 return null;   
  14.             }   
  15.               
  16.             int widthPix = ((Activity) context).getWindowManager().getDefaultDisplay()  
  17.                     .getWidth();  
  18.             widthPix = widthPix / 5 * 3;  
  19.             int heightPix = widthPix;  
  20.      
  21.             //配置参数   
  22.             Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();   
  23.             hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
  24.             //容错级别   
  25.             hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);   
  26.             //设置空白边距的宽度   
  27.             hints.put(EncodeHintType.MARGIN, 3); //default is 4   
  28.      
  29.             // 图像数据转换,使用了矩阵转换   
  30.             BitMatrix bitMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);   
  31.             int[] pixels = new int[widthPix * heightPix];   
  32.             // 下面这里按照二维码的算法,逐个生成二维码的图片,   
  33.             // 两个for循环是图片横列扫描的结果   
  34.             for (int y = 0; y < heightPix; y++) {   
  35.                 for (int x = 0; x < widthPix; x++) {   
  36.                     if (bitMatrix.get(x, y)) {   
  37.                         pixels[y * widthPix + x] = 0xff000000;   
  38.                     } else {   
  39.                         pixels[y * widthPix + x] = 0xffffffff;   
  40.                     }   
  41.                 }   
  42.             }   
  43.      
  44.             // 生成二维码图片的格式,使用ARGB_8888   
  45.             Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);   
  46.             bitmap.setPixels(pixels, 0, widthPix, 00, widthPix, heightPix);   
  47.      
  48.             if (logoBm != null) {   
  49.                 bitmap = addLogo(bitmap, logoBm);   
  50.             }   
  51.      
  52.             return bitmap;  
  53.             //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!   
  54.             //return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));   
  55.         } catch (Exception e) {   
  56.             e.printStackTrace();   
  57.         }   
  58.      
  59.         return null;   
  60.     }   
  61.      
  62.     /** 
  63.      * 在二维码中间添加Logo图案 
  64.      */   
  65.     private static Bitmap addLogo(Bitmap src, Bitmap logo) {   
  66.         if (src == null) {   
  67.             return null;   
  68.         }   
  69.      
  70.         if (logo == null) {   
  71.             return src;   
  72.         }   
  73.      
  74.         //获取图片的宽高   
  75.         int srcWidth = src.getWidth();   
  76.         int srcHeight = src.getHeight();   
  77.         int logoWidth = logo.getWidth();   
  78.         int logoHeight = logo.getHeight();   
  79.      
  80.         if (srcWidth == 0 || srcHeight == 0) {   
  81.             return null;   
  82.         }   
  83.      
  84.         if (logoWidth == 0 || logoHeight == 0) {   
  85.             return src;   
  86.         }   
  87.      
  88.         //logo大小为二维码整体大小的1/5   
  89.         float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;   
  90.         Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);   
  91.         try {   
  92.             Canvas canvas = new Canvas(bitmap);   
  93.             canvas.drawBitmap(src, 00null);   
  94.             canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);   
  95.             canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2null);   
  96.      
  97.             canvas.save(Canvas.ALL_SAVE_FLAG);   
  98.             canvas.restore();   
  99.         } catch (Exception e) {   
  100.             bitmap = null;   
  101.             e.getStackTrace();   
  102.         }   
  103.      
  104.         return bitmap;   
  105.     }   
原创粉丝点击