普通二维码和带图片二维码生成
2016-02-18 14:51
260 查看
参考:/article/1915038.html
依赖于Google开源框架zxing
下载core.jar http://maven.outofmemory.cn/com.google.zxing/core/3.1.0/ href="http://maven.outofmemory.cn/com.google.zxing/core/3.1.0/" target=_blank>
普通二维码
生成带图片的二维码
图片像素不能太高,若要大些,相应的二维码尺寸也要增大,否则无法识别,不能被扫描出来
依赖于Google开源框架zxing
下载core.jar http://maven.outofmemory.cn/com.google.zxing/core/3.1.0/ href="http://maven.outofmemory.cn/com.google.zxing/core/3.1.0/" target=_blank>
普通二维码
<span style="white-space:pre"> </span>/* * url:扫描文本信息 width:生成二维码宽 height:高 */ public Bitmap createQRImage(String url, final int width, final int height) { 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, width, height, hints); int[] pixels = new int[width * height]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (bitMatrix.get(x, y)) { pixels[y * width + x] = 0xff000000; } else { pixels[y * width + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
生成带图片的二维码
图片像素不能太高,若要大些,相应的二维码尺寸也要增大,否则无法识别,不能被扫描出来
private int IMAGE_HALFWIDTH = 10;// 中间图片的宽度 /* * string:文本信息 * mBitmap:要插入的图片 */ public Bitmap createCode(String string, Bitmap mBitmap, BarcodeFormat format) throws WriterException { Matrix m = new Matrix(); float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth(); float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight(); m.setScale(sx, sy);// 设置缩放信息 // 将logo图片按martix设置的信息缩放 mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false); MultiFormatWriter writer = new MultiFormatWriter(); Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>(); hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 设置字符编码 BitMatrix matrix = writer.encode(string, format, 400, 400, hst);// 生成二维码矩阵信息 int width = matrix.getWidth();// 矩阵高度 int height = matrix.getHeight();// 矩阵宽度 int halfW = width / 2; int halfH = height / 2; int[] pixels = new int[width * height];// 定义数组长度为矩阵高度*矩阵宽度,用于记录矩阵中像素信息 for (int y = 0; y < height; y++) {// 从行开始迭代矩阵 for (int x = 0; x < width; x++) {// 迭代列 if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {// 该位置用于存放图片信息 // 记录图片每个像素信息 pixels[y * width + x] = mBitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); } else { if (matrix.get(x, y)) {// 如果有黑块点,记录信息 pixels[y * width + x] = 0xff000000;// 记录黑块信息 } } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
相关文章推荐
- 面向切面编程(AOP)的理解
- Unity3d 中的 A*寻路
- 《笨办法学Python》 第39课手记
- 使用Python的PIL模块来进行图片对比
- 商业级别Fortify白盒神器介绍与使用分析
- Android安全攻防战,反编译与混淆技术完全解析(上)
- LeetCode 169. Majority Element
- 返回脚本所在目录
- 用Entity Framework往数据库插数据时,出现异常,怎么查看异常的详细信息呢?
- 单例模式
- windows下使用svn命令行
- 2016太原网络营销师开讲百度竞价(SEM)优化从哪些角度考虑?
- 网络路由基础认识
- 关于融合和"超融合"你不可不知的5件事
- UIDatePicker
- How to pass an empty string to Value Prompt
- 框架风格
- 渡轮问题Ship
- 灰色预测模型 c# 算法实现
- JMS【三】--ActiveMQ简单的HelloWorld实例