Android generates QR code

Table of contents

1. zxing jar package address

2. Packaging tools


1. zxing jar package address

Link: https://pan.baidu.com/s/1toPsCFS1yV6ZifXC1hwwkw?pwd=y12a Extraction code: y12a After copying this content, open the Baidu Netdisk mobile app, the operation is more convenient 
--- Sharing from Baidu Netdisk Super Member v3
 

2. Packaging tools

public class QRCodeUtils { 

    /** 
     * Generate QR code 
     * 
     * @param url URL that needs to generate QR code 
     * @param size Size of QR code that needs to be generated 
     * @return bitmap 
     */ 
    public static Bitmap createQRCode(String url, int size) { 
        Bitmap bitmap = null; 
        try { 
            HashMap<EncodeHintType, Object> hints = new HashMap<>(); 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
            hints.put(EncodeHintType.MARGIN, 0) ; 
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, 
                    BarcodeFormat.QR_CODE, size, size, hints);  
            bitMatrix = deleteWhite(bitMatrix);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();

            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * height + x] = Color.BLACK;
                    } else {
                        pixels[y * height + x] = Color.WHITE;
                    }
                }
            }
            bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 
        } catch (Throwable e) { 
            /*

        }
        return bitmap;
    } 

    /** 
     * Generate a QR code with logo 
     * 
     * @param url URL to generate QR code 
     * @param size Size of QR code to generate 
     * @param logo logo 
     * @return bitmap 
     */ 
    public static Bitmap createQRCodeWithLogo (String url, int size, Bitmap logo) { 
        Bitmap bitmap = null; 
        try { 
            HashMap<EncodeHintType, Object> hints = new HashMap<>(); 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
            hints. put(EncodeHintType.MARGIN, 0); 
             * Set the error tolerance level, the default is ErrorCorrectionLevel.L 
             * Because the logo is added in the middle, it is recommended that you adjust the error tolerance level to H, otherwise it may not be recognized 
             */ 
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); 
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, size, size, hints); 
            bitMatrix = deleteWhite( bitMatrix); 

            int width = bitMatrix.getWidth();//Matrix height 
            int height = bitMatrix.getHeight();//Matrix width 
            int halfWidth = width / 2; 
            int halfHeight = height / 2; 

            //Scale the logo image to One fifth of the size of the QR code 
            int halfLogoWidth = 0; 
            int halfLogoHeight = 0; 
            if (logo != null) {
                logo = Bitmap.createScaledBitmap(logo, width / 5, height / 5, false);  
                halfLogoWidth = logo.getWidth() / 2;
                halfLogoHeight = logo.getHeight() / 2; 
            } 

            int[] pixels = new int[width * height]; 
            for (int y = 0; y < height; y++) { 
                for (int x = 0; x < width; x++) { 
                    if (logo != null && x > halfWidth - halfLogoWidth && x < halfWidth + halfLogoWidth 
                            && y > halfHeight - halfLogoHeight && y < halfHeight + halfLogoHeight) { 
                        //This location is used to store image information 
                        //Record each pixel information of the image 
                        //If the log is null, 
                        pixels[y * height + x] = logo.getPixel(x - halfWidth 
                                + halfLogoWidth, y - halfHeight + halfLogoHeight); 
                    } else {
                        if (bitMatrix.get(x, y)) {
                            pixels[y * height + x] = Color.BLACK;
                        } else {
                            pixels[y * height + x] = Color.WHITE;
                        }
                    }
                }
            }
            bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    private static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        if (rec[0] == 0 && rec[1] == 0) {
            return matrix;
        }

        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;
        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++) {
            for (int j = 0; j < resHeight; j++) {
                if (matrix.get(i + rec[0], j + rec[1]))
                    resMatrix.set(i, j);
            }
        }
        return resMatrix;
    }
}

Guess you like

Origin blog.csdn.net/s_nshine/article/details/130850449