免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1554 | 回复: 1

[Android] Android 生成透明的二维码 [复制链接]

论坛徽章:
0
发表于 2015-07-15 09:47 |显示全部楼层
生成透明的二维码,用木纹做背景,真有点入木三分的感觉.
上面的那张是不是比下面的那张好看多了?
代码:
  1. // 生成QR图
  2. private Bitmap createQRCodeBitmap(String text) {
  3.     int QR_WIDTH = 200, QR_HEIGHT = 200;
  4.     try {
  5.         // 需要引入core包
  6.         QRCodeWriter writer = new QRCodeWriter();

  7.         Log.i(TAG, "生成的文本:" + text);
  8.         if (text == null || "".equals(text) || text.length() < 1) {
  9.             return null;
  10.         }

  11.         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  12.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  13.         //比特矩阵
  14.         BitMatrix bitMatrix = new QRCodeWriter().encode(text,
  15.                 BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
  16.         int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
  17.         //比特矩阵转颜色数组
  18.         for (int y = 0; y < QR_HEIGHT; y++) {
  19.             for (int x = 0; x < QR_WIDTH; x++) {
  20.                 if (bitMatrix.get(x, y)) {
  21.                     pixels[y * QR_WIDTH + x] = 0xff000000;//黑点
  22.                 } else {
  23.                     pixels[y * QR_WIDTH + x] = 0x00ffffff;//透明点,白点为0xffffffff
  24.                 }

  25.             }
  26.         }
  27.          
  28.         //解析颜色数组,其他的java平台可以选择其他的API
  29.         Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
  30.                 Bitmap.Config.ARGB_8888);

  31.         bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
  32.         return bitmap;

  33.     } catch (WriterException e) {
  34.         e.printStackTrace();
  35.         return null;
  36.     }
  37. }
复制代码
1.png

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2015-07-15 09:55 |显示全部楼层
楼主真是高手啊。佩服佩服
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP