一:文字转化为二维码图片。

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二维码解析——图文转换的更多相关文章

  1. ZXing 二维码解析生成工具类

    原文:http://www.open-open.com/code/view/1455848023292 import com.google.zxing.*; import com.google.zxi ...

  2. Atitit zxing二维码qr码识别解析

    Atitit zxing二维码qr码识别解析 1.1. qr码识别解析 by zxing1 1.2. 解码lib:qrcode.jar  2 1.3. atitit.二维码生成总结java zxing ...

  3. Zxing二维码的集成使用

    在github网站搜索Zxing 详见:https://github.com/yipianfengye/android-zxingLibrary 在module的build.gradle中执行comp ...

  4. Android项目实战(二十八):Zxing二维码实现及优化

    前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中我们也许只会用到二维码的扫描和生成两个功能,所以不必下载完整的ja ...

  5. (转载)Android项目实战(二十八):Zxing二维码实现及优化

    Android项目实战(二十八):Zxing二维码实现及优化   前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中 ...

  6. 二维码解析(编译zxing-cpp)

    二维码解析使用的类库是zxing(官网 https://github.com/zxing/zxing). 这个类库是谷歌的,原来有c++版本,后来的更新去掉了,zxing介绍了目前基于zxing的其他 ...

  7. 程序猿媛 九:Adroid zxing 二维码3.1集成(源码无删减)

    Adroid zxing 二维码3.1集成 声明:博文为原创,文章内容为,效果展示,思路阐述,及代码片段. 转载请保留原文出处“http://my.oschina.net/gluoyer/blog”, ...

  8. 谷歌zxing 二维码生成工具

    一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...

  9. 二维码解析:使用 JavaScript 库reqrcode.js解析二维码

    上次使用QRCode.js可以来生成二维码,但是我没有找到有文档说明可以对存在的二维码进行扫描解析其中的内容. 幸亏查找到了可行的解决方案,而且很好使哦!就是reqrcode.js 地址:https: ...

随机推荐

  1. [Bug] 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”

    This is very easy to do. Open your build definition and go to the "Process" page. Then und ...

  2. 【转】C++调用Matlab的.m文件

    原文地址     Matlab是一个强大的数学计算/仿真工 具,其内置了很多实用的现成的函数,而且我们经常也自己定义很多m函数.但在很多情况下,我们不得不使用VC编程.那么,如何在VC中利用matla ...

  3. 去除自定义Toolbar中左边距

    问题 自定义Toolbar之后,发现左侧不能完全填充,总是留一点空白,如下图: 原因 查看Wiget.AppCompat.Toolbar的parent(Toolbar默认的style),如下: < ...

  4. JAVA常见算法题(十二)

    package com.xiaowu.demo; /** * 完全平方即用一个整数乘以自己例如1*1,2*2,3*3等,依此类推.若一个数能表示成某个整数的平方的形式,则称这个数为完全平方数. * 完 ...

  5. Struts2实现登录权限访问控制

    目录: Ⅰ 条件 Ⅱ 目的 Ⅲ 分析 Ⅳ 实现 Ⅴ 具体代码实现 ------------------------------------------------------------------- ...

  6. Java 数字签名原理及产生

    数字签名与数字证书 关于数字签名的介绍可以参考以上这篇. 这里稍微说一下不对称加密的方式: 用公钥加密的内容只能用私钥解密,用私钥加密的内容只能用公钥解密. 这样比对称加密更安全 Java 中数字签名 ...

  7. 前端存储之Web Sql Database

    前言 在上一篇前端存储之indexedDB中说到,我们项目组要搞一个前后端分离的项目,要求在前端实现存储,我们首先找到了indexedDB,而我们研究了一段时间的indexedDB后,发现它并不是很适 ...

  8. 转: 由socket的accept说开去

    from: http://ticktick.blog.51cto.com/823160/779866 今天与同学争执一个话题:由于socket的accept函数在有客户端连接的时候产生了新的socke ...

  9. [Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction

    State is a lazy datatype and as such we can combine many simple transitions into one very complex on ...

  10. hibernate学习系列-----(3)Session 缓存和持久化生命周期以及Session 基本操作

    Session缓存原理 为了能够在控制台更好的看到我们的hibernate干了些什么,可以在hibernate.cfg.xml文件中写入如下配置: <!-- print all generate ...