码迷,mamicode.com
首页 > 移动开发 > 详细

Android生成二维码

时间:2016-09-26 12:49:57      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

一个生成二维码的函数

 

public static Bitmap createQRCode(String str) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, 300, 300);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}else{
pixels[y * width + x] = 0xffffffff; //-1 相当于0xffffffff 白色
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}

Android生成二维码

标签:

原文地址:http://www.cnblogs.com/xxzjyf/p/5908261.html

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