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

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

最新文章

  1. MapReduce Shuffle过程
  2. (视频) 《快速创建网站》3.4 网站改版3分钟搞定 - WordPress主题安装和备份
  3. 使用 jsoup 解析HTML
  4. java ClassLoader与动态扩展
  5. OC和JS之间的交互
  6. C++中栈区 堆区 常量区
  7. js 三元运算符以及|| 和 &amp;&amp; 测试
  8. 第二部分面向对像基础第五章Strng类中方法的使用
  9. 面向对象S.O.L.I.D原则
  10. Open source and free log analysis and log management tools.
  11. Windows Azure Web Role 的 IIS 重置
  12. 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
  13. 双击GridView查看详情
  14. Linux下搭建ntp时间同步服务器
  15. 学习笔记——命令模式Command
  16. 《深入理解java虚拟机》读书笔记1--java内存区域
  17. Jenkins配置报告与邮件插件
  18. SQL Server关于predicate、density、selectivity、cardinality名词浅析
  19. 【转】shell脚本中如何传入参数
  20. 【bfs】抓住那头牛

热门文章

  1. 【转】MySQL5.5的my.cnf 参数详解
  2. 【java】java自带的java.util.logging.Logger日志功能
  3. iis7文件夹 首页设置
  4. JDK开发WebService
  5. 【Java】String和Date、Timestamp之间的转换
  6. zookeeper安装和使用
  7. java-selenium(一)元素定位
  8. 2017.7.1 ftp文件服务器安装与配置(已验证可使用)
  9. 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 24.0怎么办
  10. poj 2506 Tiling(java解法)