Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove white space on side QR code (using zxing)

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;
  }
like image 282
Vazedias Avatar asked Dec 12 '22 19:12

Vazedias


2 Answers

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);
like image 181
Harsh Avatar answered Dec 27 '22 09:12

Harsh


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

like image 30
Sean Owen Avatar answered Dec 27 '22 09:12

Sean Owen