导入zxing
compile 'com.google.zxing:core:3.2.1'
生成Bitmap二维码
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix matrix = multiFormatWriter.encode("test", BarcodeFormat.QR_CODE, 200, 200);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;//黑色和白色
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}