一、生成二维码工具类封装
1、二维码库
// 二维码 implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
2、工具类
/** * 二维码 * 处理工具 */ public class qrcodedealutils { /** * @param content 字符串内容 * @param size 位图宽&高(单位:px) * @param logo 二维码logo * @param logopercent 二维码logo的占比 [0,1] * @return */ public static bitmap createqrcodebitmaplogo(string content, int size, bitmap logo, float logopercent) { bitmap qrcodebitmap = null; bitmap bitmap; try { // 不带logo二维码 qrcodebitmap = generateqrcodewithoutmargin(content, size, size); // 带logo 二维码 bitmap = addlogotoqrcode(qrcodebitmap, logo, logopercent); } catch (writerexception e) { throw new runtimeexception(e); } return bitmap; } /** * 生成 * 无白色边框 * 二维码 */ public static bitmap generateqrcodewithoutmargin(string text, int width, int height) throws writerexception { qrcodewriter qrcodewriter = new qrcodewriter(); map<encodehinttype, object> hints = new hashmap<>(); hints.put(encodehinttype.margin, 0); // 设置边距为0 bitmatrix bitmatrix = qrcodewriter.encode(text, barcodeformat.qr_code, width, height, hints); 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 * width + x] = color.black; // 黑色像素 } else { pixels[y * width + x] = color.transparent; // 透明像素,去除白边 } } } bitmap bitmap = bitmap.createbitmap(width, height, bitmap.config.argb_8888); bitmap.setpixels(pixels, 0, width, 0, 0, width, height); return bitmap; } /** * 给二维码 * 添加logo */ public static bitmap addlogotoqrcode(bitmap srcbitmap, bitmap logobitmap, float logopercent) { // 计算logo相对于二维码的尺寸 int logowidth = math.round(srcbitmap.getwidth() * logopercent); int logoheight = math.round(srcbitmap.getheight() * logopercent); // 确保logo尺寸不会超过二维码尺寸 if (logowidth > srcbitmap.getwidth() || logoheight > srcbitmap.getheight()) { throw new illegalargumentexception("logo size is too large for the qr code."); } // 创建一个新的bitmap来保存带logo的二维码 bitmap resultbitmap = bitmap.createbitmap(srcbitmap.getwidth(), srcbitmap.getheight(), srcbitmap.getconfig()); canvas canvas = new canvas(resultbitmap); // 绘制原始二维码 canvas.drawbitmap(srcbitmap, 0, 0, null); // 创建一个matrix对象来缩放logo matrix matrix = new matrix(); matrix.postscale(logowidth / (float) logobitmap.getwidth(), logoheight / (float) logobitmap.getheight()); // 计算logo应该放置的位置(中心) int xoffset = (srcbitmap.getwidth() - logowidth) / 2; int yoffset = (srcbitmap.getheight() - logoheight) / 2; // 在二维码上绘制logo canvas.drawbitmap(logobitmap, xoffset, yoffset, null); return resultbitmap; } }
二、方法说明
1、不带logo
/** * 生成 * 无白色边框 * 二维码 */ public static bitmap generateqrcodewithoutmargin(string text, int width, int height) throws writerexception { qrcodewriter qrcodewriter = new qrcodewriter(); map<encodehinttype, object> hints = new hashmap<>(); hints.put(encodehinttype.margin, 0); // 设置边距为0 bitmatrix bitmatrix = qrcodewriter.encode(text, barcodeformat.qr_code, width, height, hints); 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 * width + x] = color.black; // 黑色像素 } else { pixels[y * width + x] = color.transparent; // 透明像素,去除白边 } } } bitmap bitmap = bitmap.createbitmap(width, height, bitmap.config.argb_8888); bitmap.setpixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
2、给二维码添加logo的方法
/** * 给二维码 * 添加logo */ public static bitmap addlogotoqrcode(bitmap srcbitmap, bitmap logobitmap, float logopercent) { // 计算logo相对于二维码的尺寸 int logowidth = math.round(srcbitmap.getwidth() * logopercent); int logoheight = math.round(srcbitmap.getheight() * logopercent); // 确保logo尺寸不会超过二维码尺寸 if (logowidth > srcbitmap.getwidth() || logoheight > srcbitmap.getheight()) { throw new illegalargumentexception("logo size is too large for the qr code."); } // 创建一个新的bitmap来保存带logo的二维码 bitmap resultbitmap = bitmap.createbitmap(srcbitmap.getwidth(), srcbitmap.getheight(), srcbitmap.getconfig()); canvas canvas = new canvas(resultbitmap); // 绘制原始二维码 canvas.drawbitmap(srcbitmap, 0, 0, null); // 创建一个matrix对象来缩放logo matrix matrix = new matrix(); matrix.postscale(logowidth / (float) logobitmap.getwidth(), logoheight / (float) logobitmap.getheight()); // 计算logo应该放置的位置(中心) int xoffset = (srcbitmap.getwidth() - logowidth) / 2; int yoffset = (srcbitmap.getheight() - logoheight) / 2; // 在二维码上绘制logo canvas.drawbitmap(logobitmap, xoffset, yoffset, null); return resultbitmap; }
3、调用方式
/** * @param content 字符串内容 * @param size 位图宽&高(单位:px) * @param logo 二维码logo * @param logopercent 二维码logo的占比 [0,1] * @return */ public static bitmap createqrcodebitmaplogo(string content, int size, bitmap logo, float logopercent) { bitmap qrcodebitmap = null; bitmap bitmap; try { // 不带logo二维码 qrcodebitmap = generateqrcodewithoutmargin(content, size, size); // 带logo 二维码 bitmap = addlogotoqrcode(qrcodebitmap, logo, logopercent); } catch (writerexception e) { throw new runtimeexception(e); } return bitmap; }
总结
到此这篇关于android生成二维码工具类封装及使用的文章就介绍到这了,更多相关android生成二维码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论