Android QR 位图需要帮助来删除边距

Toe*_*fen 2 java android pixel bitmap

我已将字符串编码为 QR 位图。图片就变成了这样:

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;

}
Run Code Online (Sandbox Code Playgroud)

Evg*_*sky 7

您应该使用提示参数来设置自定义边距。

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);
Run Code Online (Sandbox Code Playgroud)