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

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

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

05-16 00:18