我已经将一个字符串编码成一个 QR 位图。图片变成这样:
我需要更改什么才能使 QR 周围没有空格?我试图阅读有关 MultiFormatWriter() 和 setPixels() 的文档,但找不到错误的地方。 这是代码:
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, 500, 500, null);
} catch (IllegalArgumentException iae) {
return null;
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int [w * h];
for (int i = 0; i < h; i++) {
int offset = i * w;
for (int j = 0; j < w; j++) {
pixels[offset + j] = result.get(i, j) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, 500, 0, 0, w, h);
return bitmap;
}
最佳答案
您应该使用 hints 参数来设置自定义边距。
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.MARGIN, marginSize);
BitMatrix result = new MultiFormatWriter().encode(contentsToEncode, BarcodeFormat.QR_CODE, imageWidth, imageHeight, hints);
关于java - Android QR 位图需要帮助去除边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768886/