Zxing二维码解析——图文转换
2024-10-19 22:28:33
一:文字转化为二维码图片。
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
最新文章
- MapReduce Shuffle过程
- (视频) 《快速创建网站》3.4 网站改版3分钟搞定 - WordPress主题安装和备份
- 使用 jsoup 解析HTML
- java ClassLoader与动态扩展
- OC和JS之间的交互
- C++中栈区 堆区 常量区
- js 三元运算符以及|| 和 &;&; 测试
- 第二部分面向对像基础第五章Strng类中方法的使用
- 面向对象S.O.L.I.D原则
- Open source and free log analysis and log management tools.
- Windows Azure Web Role 的 IIS 重置
- 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
- 双击GridView查看详情
- Linux下搭建ntp时间同步服务器
- 学习笔记——命令模式Command
- 《深入理解java虚拟机》读书笔记1--java内存区域
- Jenkins配置报告与邮件插件
- SQL Server关于predicate、density、selectivity、cardinality名词浅析
- 【转】shell脚本中如何传入参数
- 【bfs】抓住那头牛
热门文章
- 【转】MySQL5.5的my.cnf 参数详解
- 【java】java自带的java.util.logging.Logger日志功能
- iis7文件夹 首页设置
- JDK开发WebService
- 【Java】String和Date、Timestamp之间的转换
- zookeeper安装和使用
- java-selenium(一)元素定位
- 2017.7.1 ftp文件服务器安装与配置(已验证可使用)
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 24.0怎么办
- poj 2506 Tiling(java解法)