5

I use zxing library to generate QRcode. When I generate QR it work fine but it have a white space on left and right side of QR. How to remove this white space (to make a bitmap width=height)

Here is my code to generate QR

    private static final int QR_SIZE = 480;

  public static Bitmap generateQR(String content){
      Bitmap returnBitmap= null;
      try {
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix matrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, QR_SIZE, QR_SIZE, hintMap);
        //int width = matrix.getWidth();
        int width = matrix.getHeight();
        int height = matrix.getHeight();
        int[] pixels = new int[width*height];
        for(int y=0; y<height; y++) {
            for(int x=0; x<width; x++) {
                int grey = matrix.get(x, y) ? 0x00 : 0xff;
                pixels[y*width+x] = 0xff000000 | (0x00010101*grey);
            }
        }    
        returnBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        returnBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        //returnBitmap.set
    } catch (Exception e) {
        Log.d(LOGTAG, e.toString());
    }
    return returnBitmap;
  }
Vazedias
  • 145
  • 1
  • 2
  • 9
  • Take a Look at the Following Posts http://stackoverflow.com/questions/10142748/reduce-border-width-on-qr-codes-generated-by-zxing https://groups.google.com/forum/?fromgroups=#!topic/zxing/54untGQOjRk – Usman Kurd Dec 24 '12 at 08:37
  • Thanks Usman Kurd, but I can't find any answer in link. T^T – Vazedias Dec 24 '12 at 09:14
  • It's my fault. I didn't see background color property of ImageView. I set it from white to transparent, however Thanks again for your attention. – Vazedias Dec 25 '12 at 04:36

6 Answers6

9

Too late for the answer, but can be used in future.

White space can be removed by using below code sample

Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>();
hintMap.put(EncodeHintType.MARGIN, new Integer(1));
BitMatrix matrix = new MultiFormatWriter().encode(
        new String(qrCodeData.getBytes(charset), charset),
        BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
Harsh
  • 282
  • 1
  • 3
  • 11
  • Additionally, you can make it transparent by passing MatrixToImageConfig to `com.google.zxing.client.j2se.MatrixToImageWriter#writeToStream(com.google.zxing.common.BitMatrix, java.lang.String, java.io.OutputStream, com.google.zxing.client.j2se.MatrixToImageConfig)` – cghislai Oct 30 '19 at 15:37
  • Yes It working thank you for saving our time – Mohamed Imran Jan 03 '23 at 13:29
5

Don't remove the white space, it is required by the spec.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
4

For completeness, this is a solution written in Kotlin (its based on the above answers) :

fun generateQrImage(width: Int): Bitmap? {
    val code = "some code here" // Whatever you need to encode in the QR code
    val multiFormatWriter = MultiFormatWriter();
    try {
        val hintMap = mapOf(EncodeHintType.MARGIN to 0)
        val bitMatrix = multiFormatWriter.encode(code, BarcodeFormat.QR_CODE, width, width, hintMap)
        val barcodeEncoder = BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (e: Exception) {
        Log.error("Could not generate QR image", e)
        return null
    }
}
HixField
  • 3,538
  • 1
  • 28
  • 54
0

Am might be too late, though my suggestion might help in future who would have this issue.

Yes, you do not remove the white space, some readers will scan it with just a little border, it will be harder. And some won't read at all.

In my case i want to show the QR code in Ui, so i just made my layout as white background. and its reading QR code as well.

Raju
  • 1,183
  • 3
  • 11
  • 19
0

Anyone interested can use the below code. After applying this code, I tried to scan the QRcode, it is got scanned without any problems.

 try {

        /**
         * Allow the zxing engine use the default argument for the margin variable
         */
        int MARGIN_AUTOMATIC = -1;

        /**
         * Set no margin to be added to the QR code by the zxing engine
         */
        int MARGIN_NONE = 0;
        int marginSize = MARGIN_NONE;

        Map<EncodeHintType, Object> hints = null;
        if (marginSize != MARGIN_AUTOMATIC) {
            hints = new EnumMap<>(EncodeHintType.class);
            // We want to generate with a custom margin size
            hints.put(EncodeHintType.MARGIN, marginSize);
        }

        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = writer.encode(text, BarcodeFormat.QR_CODE, imageWidth, imageHeight, hints);

        final int width = result.getWidth();
        final 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) ? barcodeColor : barcodeBackgroundColor;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;

    } catch (WriterException e) {
        e.printStackTrace();
    }

Source this page

Rakesh Yadav
  • 1,966
  • 2
  • 21
  • 35
-1

Try this......:)

private void generateQRCode(String data) throws IOException {
            com.google.zxing.Writer writer = new QRCodeWriter();
            String finaldata = Uri.encode(data, "UTF-8");
            try {
                BitMatrix bm = writer.encode(finaldata, BarcodeFormat.QR_CODE, 400,
                        400);
                mBitmap = Bitmap.createBitmap(400, 400, Config.ARGB_8888);
                for (int i = 0; i < 400; i++) {
                    for (int j = 0; j < 400; j++) {
                        mBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK
                                : Color.WHITE);
                    }
                }
            } catch (WriterException e) {
                e.printStackTrace();
            }

            if (mBitmap != null) {
                                  img_qrcode.setImageBitmap(mBitmap);
                                }
}
Melbourne Lopes
  • 4,817
  • 2
  • 34
  • 36