Android生成二维码

目录

一.zxing jar包地址

二.封装工具类


一.zxing jar包地址

链接: https://pan.baidu.com/s/1toPsCFS1yV6ZifXC1hwwkw?pwd=y12a 提取码: y12a 复制这段内容后打开百度网盘手机App,操作更方便哦 
--来自百度网盘超级会员v3的分享
 

二.封装工具类

public class QRCodeUtils {

    /**
     * 生成二维码
     *
     * @param url 需要生成二维码的网址
     * @param size 需要生成二维码的大小
     * @return bitmap
     */
    public static Bitmap createQRCode(String url, int size) {
        Bitmap bitmap = null;
        try {
            HashMap 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;
    }

    /**
     * 生成带logo的二维码
     *
     * @param url 需要生成二维码的网址
     * @param size 需要生成二维码的大小
     * @param logo logo
     * @return bitmap
     */
    public static Bitmap createQRCodeWithLogo(String url, int size, Bitmap logo) {
        Bitmap bitmap = null;
        try {
            HashMap hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, 0);
            /*
             * 设置容错级别,默认为ErrorCorrectionLevel.L
             * 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了
             */
            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();//矩阵高度
            int height = bitMatrix.getHeight();//矩阵宽度
            int halfWidth = width / 2;
            int halfHeight = height / 2;

            //将logo图片缩放成二维码的5分之一大小
            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) {
                        //该位置用于存放图片信息
                        //记录图片每个像素信息
                        //如果log为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;
    }
}

你可能感兴趣的:(android,二维码,zxing)