Android生成二维码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于Android生成二维码的主要内容,如果未能解决你的问题,请参考以下文章

android 生成的二维码图片如何保存到sdcard?

玩转Android之二维码生成与识别

Android生成二维码

Android之扫描二维码和根据输入信息生成名片二维码

android中使用Zxing库生成二维码二维码和二维码四的区别

Android Studio 扫描识别二维码(包含闪光灯和本地二维码)生成二维码生成带logo的二维码