简体   繁体   中英

How to generate a custom QR code in Android programmatically using ZXing Library?

I'm using Journeyapp's ZXing Android Embedded library for my android app and I can generate a simple QR code using the following piece of code

 private void init() {

        ImageView qrImageView = (ImageView) findViewById(R.id.qr_image_view);

        qrImageView.setImageBitmap(generateQRBitMap("a"));

    }

    private Bitmap generateQRBitMap(final String content) {

        Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();

        hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);

        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        try {
            BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 512, 512, hints);

            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();

            Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {

                    bmp.setPixel(x , y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
                }
            }

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

        return null;
    }

However, I want to be able to generate something as cool as the one given below在此处输入图像描述

Now I know that I may have to write a custom encoder for that, but I really don't know where to begin. The BitMatrix class always creates a square QR code, but is there anything that I can use to create the odd shapes?

I found this library QRGen using ZXing and very easy to use. Whatever, to design at your desire, you may add another image behind of this QR code's Image View.

Sample Code for Generating QR Code

Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);

Try creating two QRCodes. One should be random, call it A. One should contain data, B. Enlarge A, and warp it (blur will do). Create a white vector that is transparent for the shape of the image, and white round the edge. Overlay this image onto QRCode A, and then overlay B on top.

Hope you work out some code from this, P

PS If you do, make it a library!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM