目录
一.zxing jar包地址
二.封装工具类文章来源:https://www.toymoban.com/news/detail-458523.html
一.zxing jar包地址
链接: https://pan.baidu.com/s/1toPsCFS1yV6ZifXC1hwwkw?pwd=y12a 提取码: y12a 复制这段内容后打开百度网盘手机App,操作更方便哦
--来自百度网盘超级会员v3的分享
文章来源地址https://www.toymoban.com/news/detail-458523.html
二.封装工具类
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; } }
到了这里,关于Android生成二维码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!