我在我的应用程序ZXing库中使用它来生成QR码.我想生成适合屏幕宽度的QR码(可能是一些小填充).
如果我将屏幕宽度设置为QR码的宽度尺寸,我会得到更小的QR码.看截图(它是320×240分辨率).我想要QR码适合黑色区域.为什么QR码红色这么小?
如何将其拉伸到黑色区域?
我的代码:
display display = getwindowManager().getDefaultdisplay(); Point size = new Point(); display.getSize(size); int width = size.x; Bitmap bm = encodeAsBitmap(mGeneratedURL,BarcodeFormat.QR_CODE,width,width); qrcodeImage.setimageBitmap(bm);
生成QR码:
private Bitmap encodeAsBitmap(String contents,BarcodeFormat format,int img_width,int img_height) throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map<EncodeHintType,Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
//hints.put(EncodeHintType.CHaraCTER_SET,encoding);
hints.put(EncodeHintType.MARGIN,0); /* default = 4 */
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode,format,img_width,img_height,hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.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] = result.get(x,y) ? RED : Color.BLACK;
}
}
Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels,height);
return bitmap;
}
解决方法
我发现了一个问题!
这一行:
String encoding = guessAppropriateEncoding(contentsToEncode);
返回null
所以它没有设定
EncodeHintType.MARGIN.
删除该文件,它应该没问题.
//if (encoding != null) {
hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
//hints.put(EncodeHintType.CHaraCTER_SET,encoding);
hints.put(EncodeHintType.MARGIN,0); /* default = 4 */
//}