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<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;
    }

    /**
     * 生成带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<EncodeHintType, Object> 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s_nshine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值