Android 生成二维码

ops/2025/3/19 13:12:01/

一、生成二维码工具类封装

 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); // 设置边距为0BitMatrix 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对象来缩放LogoMatrix 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;// 在二维码上绘制Logocanvas.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); // 设置边距为0BitMatrix 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对象来缩放LogoMatrix 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;// 在二维码上绘制Logocanvas.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;}

到此结束


http://www.ppmy.cn/ops/19935.html

相关文章

【机器学习】机器学习学习笔记 - 无监督学习 - k-means/均值漂移聚类/凝聚层次聚类/近邻传播聚类 - 05

pdf在线免费转word文档 https://orcc.online/pdf 不限次数、免费不需要注册。 无监督学习 (聚类) 聚类是一种无监督学习方法&#xff0c;是将数据划分为若干个簇&#xff0c;使得簇内的点尽可能相似&#xff0c;簇间尽可能不相似。 k-means 聚类 k-means 聚类算法是一种迭…

css中新型的边框设置属性border-block

border-block 是 CSS 中的一个属性&#xff0c;主要用于在样式表中一次性设置元素的逻辑块向边框的属性值。这个属性是简写属性&#xff0c;可以同时设置 border-block-width、border-block-style 和 border-block-color。其中&#xff0c;border-block-start 用于设置元素的开…

Python项目开发实战:动物分拣器的实现

注意:本文的下载教程,与以下文章的思路有相同点,也有不同点,最终目标只是让读者从多维度去熟练掌握本知识点。 下载教程:Python项目开发实战_动物分拣器的实现_编程案例解析实例详解课程教程.pdf 1、步骤 一、项目背景与目标 在生物研究、动物园管理以及动物保护等领域中…

SpringCloud系列(11)--将微服务注册进Eureka集群

前言&#xff1a;在上一章节中我们介绍并成功搭建了Eureka集群&#xff0c;本章节则介绍如何把微服务注册进Eureka集群&#xff0c;使服务达到高可用的目的 Eureka架构原理图 1、分别修改consumer-order80模块和provider-payment8001模块的application.yml文件&#xff0c;使这…

Java、Android面试高频系列文章合集

本人今年参加了很多面试&#xff0c;也有幸拿到了一些大厂的offer&#xff0c;整理了众多面试资料&#xff0c;后续还会分享众多面试资料。 整理成了面试系列&#xff0c;由于时间有限&#xff0c;每天整理一点&#xff0c;后续会陆续分享出来&#xff0c;感兴趣的朋友可关注收…

ESBMC代码阅读笔记

文档描述 本文档为对ESBMC模型检测工具进行代码阅读随手写的文档&#xff0c;该文档针对工具ESBMC version 7.5.0 64-bit x86_64 linux 的BMC框架进行代码阅读&#xff0c;主要关注其BMC算法框架&#xff0c;数据结构以及如何从BMC得到的中间数据结构进行SMT编码的过程。本文档…

elment-table实现行滚动效果

通过获取dom &#xff0c;来控制表格滚动 <template><div class"scroll_table"><div style"display: inline-block; width: 100%"><el-table:data"tableData"ref"table"borderheight"100%"highlight-c…

OpenCV直方图计算

返回:OpenCV系列文章目录&#xff08;持续更新中......&#xff09; 上一篇&#xff1a;OpenCV实现直方图均衡 下一篇 :OpenCV系列文章目录&#xff08;持续更新中......&#xff09; 在本教程中&#xff0c;您将学习如何&#xff1a; 使用 OpenCV 函数 cv::split 将图像划分…