读书人

二维码生成的png图片没有白色背景如何

发布时间: 2012-04-04 16:38:51 作者: rapoo

二维码生成的png图片没有白色背景怎么加上?
/**
* 生成二维码图片方法
* */
public Bitmap Create2DCode(String str) throws WriterException {
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 320,320);
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;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
我用上面的方法生成的二维码图片没有白色背景,所以我保存完图片文件后,在预览时由于手机的背景是黑色的,就看不见二维码了,我的问题是如何在生成二维码PNG图片时就加上一个白色背景。



[解决办法]

Java code
 if(matrix.get(x, y)){     pixels[y * width + x] = 0xff000000;     } 

读书人网 >Java相关

热点推荐