Zxing二维码解析——图文转换

一:文字转化为二维码图片。
package com.xhm.tool; import java.util.Hashtable; import android.graphics.Bitmap;
import android.text.TextUtils; 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; public class TextToBitmap {
// 要生成的文字
private String text;
// 生成图片的大小
private int QR_WIDTH, QR_HEIGHT; public TextToBitmap(String text, int QR_WIDTH, int QR_HEIGHT) {
this.text = text;
this.QR_HEIGHT = QR_HEIGHT;
this.QR_WIDTH = QR_WIDTH;
} public Bitmap getBitmap() {
try {
// 判断文字是否为空
if (TextUtils.isEmpty(text)) {
return null;
}
// 设置二维码属性,如编码格式,大小,颜色等
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
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;
}
}
}
// 建立一个bitmap图片,用来接受二维码的颜色。
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 bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
}
二:图片转换为文字:
package com.xhm.tool; import java.util.Hashtable; import android.graphics.Bitmap; import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader; public class BitmapToText {
// 要读取的二维码图片
private Bitmap bitmap; public BitmapToText(Bitmap bitmap) {
this.bitmap = bitmap;
} // 获得文字
public String getText() {
int mWidth, mHeight;
String value = null;
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
// 设置解析编码
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
mWidth = bitmap.getWidth();
mHeight = bitmap.getHeight();
int[] pixels = new int[mWidth * mHeight];
bitmap.getPixels(pixels, 0, mWidth, 0, 0, mWidth, mHeight);
RGBLuminanceSource source = new RGBLuminanceSource(mWidth, mHeight,
pixels);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader2 = new QRCodeReader();
Result result = null;
try {
result = reader2.decode(bitmap1, hints);
value = result.getText();
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ChecksumException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
}
三:调用代码
获得imageview中的图片
ImageView image = (ImageView) view[1];
// 缓存图片,方便后边调用
image.setDrawingCacheEnabled(true);
ImageView image = (ImageView) view[1];
// 获得缓存的图片
btt = new BitmapToText(image.getDrawingCache(true));
JAR包下载地址:http://download.csdn.net/detail/as294985925/5685213
源码下载地址:http://download.csdn.net/detail/as294985925/5685321
Zxing二维码解析——图文转换的更多相关文章
- ZXing 二维码解析生成工具类
原文:http://www.open-open.com/code/view/1455848023292 import com.google.zxing.*; import com.google.zxi ...
- Atitit zxing二维码qr码识别解析
Atitit zxing二维码qr码识别解析 1.1. qr码识别解析 by zxing1 1.2. 解码lib:qrcode.jar 2 1.3. atitit.二维码生成总结java zxing ...
- Zxing二维码的集成使用
在github网站搜索Zxing 详见:https://github.com/yipianfengye/android-zxingLibrary 在module的build.gradle中执行comp ...
- Android项目实战(二十八):Zxing二维码实现及优化
前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中我们也许只会用到二维码的扫描和生成两个功能,所以不必下载完整的ja ...
- (转载)Android项目实战(二十八):Zxing二维码实现及优化
Android项目实战(二十八):Zxing二维码实现及优化 前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中 ...
- 二维码解析(编译zxing-cpp)
二维码解析使用的类库是zxing(官网 https://github.com/zxing/zxing). 这个类库是谷歌的,原来有c++版本,后来的更新去掉了,zxing介绍了目前基于zxing的其他 ...
- 程序猿媛 九:Adroid zxing 二维码3.1集成(源码无删减)
Adroid zxing 二维码3.1集成 声明:博文为原创,文章内容为,效果展示,思路阐述,及代码片段. 转载请保留原文出处“http://my.oschina.net/gluoyer/blog”, ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...
- 二维码解析:使用 JavaScript 库reqrcode.js解析二维码
上次使用QRCode.js可以来生成二维码,但是我没有找到有文档说明可以对存在的二维码进行扫描解析其中的内容. 幸亏查找到了可行的解决方案,而且很好使哦!就是reqrcode.js 地址:https: ...
随机推荐
- SQL Server Profiler和数据库引擎优化顾问
原文:SQL Server Profiler和数据库引擎优化顾问 简介 说到Sql的[性能工具]真是强大,SQL Server Profiler的中文意思是SQL Server事件探查,这个到底 ...
- Linux内核分析 - 网络
http://blog.csdn.net/column/details/network-kernel-yoyo.html
- Volley缓存说明——一个请求两次回调
从上一篇文章Android 异步网络请求框架-Volley了解volley的一些出来过程,当然也包含网络请求和缓存处理的流程,但是在此需要单独做一些说明. 我在使用过程中忽略了一个事情,就是一个网络请 ...
- pure-ftp 服务配置篇
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为 "文传协议" 用于Internet上的控制文件的双向传输. FTP的主要作用,就是让 ...
- Makefile之文件搜索
Makefile之文件搜索 1.Makefile 文件中的"VPATH"变量 如果没有指明这个变量,make只会在当前目录下查找依赖文件和目标文件: 如果定义了这个变量,make会 ...
- 在CcentOS系统上将deb包转换为rpm包
deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/centos/fedora中安装,需要把deb格式的软件包转化成rpm格式. 需要用到的转换工具:alien_8 ...
- nginx静态资源配置
解决EE工程中静态文件显示问题 在工程中本地测试没有问题,发现使用nginx配置了路径的页面,会获取不到相应页面的静态文件问题 静态文件的路径类似为: http://localhost:8080/sa ...
- 【Hadoop】如何形象描述大数据生态?
作者:千岁大王链接:https://www.zhihu.com/question/27974418/answer/39845635来源:知乎著作权归作者所有,转载请联系作者获得授权. Google内部 ...
- linux系统预留内存和磁盘大小
默认情况下, Linux 会最多使用 40% 的可用内存作为文件系统缓存.当超过这个阈值后,文件系统会把将缓存中的内存全部写入磁盘, 导致后续的 IO 请求都是同步的. 将缓存写入磁盘时,有一个默认1 ...
- SharedPreferences具体解释(一)——基础知识
我们在开发软件的时候,常须要向用户提供软件參数设置功能,比如我们经常使用的微信,用户能够设置是否同意陌生人加入自己为好友.对于软件配置參数的保存,假设是在window下通常我们会採用ini文件进行保存 ...