Question

I'm currently generating a QR code in my Android app but the customer complained that we need to generate codes with High error correction.

The code I'm currently using is the following, but the problem is that I don't know how to actually generate the QR image with the certain error connection:

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);
        }
        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) ? BLACK : WHITE;
            }
        }

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

Thanks a lot for the help in advance!

Was it helpful?

Solution 2

I just added the line hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); to my method in order to generate a High correction level, and taking a look at this, I can see the generated one has a high correction level.

OTHER TIPS

If you need online qrcode generator use Google Chart Api.

https://chart.googleapis.com//chart?chs="+ width+"x"+ width+"&cht=qr&chl=yourqrcodetext"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top