ZxingUtil二维码 封装
来源:互联网 发布:lrc歌词编辑软件 编辑:程序博客网 时间:2024/05/22 19:19
import java.util.HashMap;import java.util.Hashtable;import java.util.Map;import android.graphics.Bitmap;import android.graphics.Canvas;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class ZxingUtil { private static int QR_WIDTH = 156; private static int QR_HEIGHT = 156; public static Bitmap createQRImage(String url, Bitmap logo) { try { // 判断URL合法性 if (url == null || "".equals(url) || url.length() < 1) { return null; } Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < QR_HEIGHT; y++) { for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { pixels[y * QR_WIDTH + x] = 0xff000000; } else { pixels[y * QR_WIDTH + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); return addLogo(bitmap, logo); } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 在二维码中间添加Logo图案 */ private static Bitmap addLogo(Bitmap src, Bitmap logo) { if (src == null) { return null; } if (logo == null) { return src; } // 获取图片的宽高 int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); int logoWidth = logo.getWidth(); int logoHeight = logo.getHeight(); if (srcWidth == 0 || srcHeight == 0) { return null; } if (logoWidth == 0 || logoHeight == 0) { return src; } // logo大小为二维码整体大小的1/5 float scaleFactor = srcWidth * 1.0f / 5 / logoWidth; Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888); try { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(src, 0, 0, null); canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2); canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); } catch (Exception e) { bitmap = null; e.getStackTrace(); } return bitmap; } /** * 生成二维码Bitmap * * @param content * 内容 * @param widthPix * 图片宽度 * @param heightPix * 图片高度 * @param logoBm * 二维码中心的Logo图标(可以为null) * @param filePath * 用于存储二维码图片的文件路径 * @return 生成二维码及保存文件是否成功 */ public static Bitmap createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm, String filePath) { try { if (content == null || "".equals(content)) { return null; } // 配置参数 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置空白边距的宽度 hints.put(EncodeHintType.MARGIN, 2); // default is 4 // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); int[] pixels = new int[widthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < widthPix; x++) { if (bitMatrix.get(x, y)) { pixels[y * widthPix + x] = 0xff000000; } else { pixels[y * widthPix + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix); if (logoBm != null) { bitmap = addLogo(bitmap, logoBm); } // 必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大! // boolean result = bitmap != null // && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new // FileOutputStream(filePath)); // if (result) return bitmap; // else // return null; } catch (Exception e) { e.printStackTrace(); } return null; }}
0 0
- ZxingUtil二维码 封装
- 二维码封装库
- react封装二维码
- 二维码简单封装
- 二维码的简单封装(swift)
- jquery生成二维码插件-封装
- 二维码的扫描封装OC
- QT 生成二维码接口封装
- android生成二维码的封装包
- QRCode--根据系统API, 封装二维码扫描
- 快速封装一个二维码在线生成接口
- 微信支付生成二维码方法封装
- 支付宝支付二维码收银台封装
- android二维码扫描及生成封装工具类
- 快速集成二维码扫描库-zxing的源码封装
- Android开发 封装生成二维码的方法,随用随调
- 二维码的生成 识别 扫描 封装(Swift)
- 简单的二维码扫描封装,要在真机上试才不会崩溃
- 面试题12:二进制中1的个数
- woe和iv的含义
- Step-by-Step Guide to Setting Up an R-Hadoop System
- 64位Win7安装Oracle12C临时位置权限错误解决方案
- 【LEETCODE】204-Count Primes
- ZxingUtil二维码 封装
- SQL server学习笔记之数据库启动登陆,创建和维护
- UILabel
- MFC Ribbon界面各部分的名字
- 构建兼容浏览器的Angularjs web应用
- 每隔五秒执行一次 共十次 模拟骰子的投掷
- 关于ARM交叉编译工具出现:assertion fail elf32-arm.c:12387 的错误解决办法。
- Openstack Kilo后台命令练习
- iframe 自适应高度