Pep*_*tal 10 android qr-code bitmap zxing
我在我的应用程序ZXing库中使用它来生成QR码.我想生成适合屏幕宽度的QR码(可能是一些小填充).
如果我将屏幕宽度设置为QR码的宽度尺寸,我会得到更小的QR码.看截图(它是320x240分辨率).我想要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);
Run Code Online (Sandbox Code Playgroud)
生成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, 0, width, 0, 0, width, height);
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
Pep*_*tal 13
我发现了一个问题!
这一行:
String encoding = guessAppropriateEncoding(contentsToEncode);
Run Code Online (Sandbox Code Playgroud)
返回null
所以它没有设定
EncodeHintType.MARGIN.
Run Code Online (Sandbox Code Playgroud)
删除该文件,它应该没问题.
//if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
//hints.put(EncodeHintType.CHARACTER_SET, encoding);
hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */
//}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4381 次 |
| 最近记录: |