Android生成二维码

古人已用三冬足,年少今开万卷余。这篇文章主要讲述Android生成二维码相关的知识,希望能为你提供帮助。
一个生成二维码的函数
【Android生成二维码】 
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;
}

    推荐阅读