现在二维码越来越流行,到处可以见二维码,扫一扫就可以传输信息。
目前大多都是应用Google的开源码 zxing。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。
- CaptureActivity。这个是启动Activity 也就是扫描器。
- CaptureActivityHandler 解码处理类,负责调用另外的线程进行解码。
- DecodeThread 解码的线程。
- com.google.zxing.client.android.camera 包,摄像头控制包。
- ViewfinderView 自定义的View,就是我们看见的拍摄时中间的框框了。具体代码大家可以下载开源包看下。
目前仅讨论二维码的生成。引入QRCodeUtil 二维码工具类,代码如下:
import java.util.HashMap;
import java.util.Map;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 二维码工具
*
*/
public class QRCodeUtil {
private int qrcodeSize;
private int marginSize;
public QRCodeUtil(int qrcodeSize) {
super();
this.qrcodeSize = qrcodeSize;
marginSize = 0;
}
/**
* 用字符串生成二维码
* @param str
* (这里需要啰嗦几句,如果只要传入一个URL即可,传入一个合法的网址扫一下打开某网址即可。或者像现在一些移动APP的推广,
* 把APP下载地址转为二维码,只要扫一下就可以下载相应的APP。这个也是目前比较流行的APP的推广方式。
* 又或者拼接一个字串("mallId="+mallId+"&storeId="+storeId)这就是需要传递数据信息。)
*
* @author jyc
* @return
* @throws WriterException
*/
public Bitmap create2DCode(String str) throws WriterException {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, marginSize);
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, qrcodeSize, qrcodeSize,hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
//二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(matrix.get(x, y)){
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap whiteBg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(whiteBg);
canvas.drawColor(Color.WHITE);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
canvas.drawBitmap(bitmap, 0, 0, null);
bitmap.recycle();
return whiteBg;
}
/**
* 在二维码中间加上logo
*/
public Bitmap createQRCodeWithLogo(String codeStr, Bitmap logoBitmap) {
Bitmap qrcodeBitmap = null;
try {
qrcodeBitmap = create2DCode(codeStr);
// 头像图片的大小
int logoW = logoBitmap.getWidth();
int logoH = logoBitmap.getHeight();
// 设置头像要显示的位置,即居中显示
int left = (qrcodeBitmap.getWidth() - logoW) / 2;
int top = (qrcodeBitmap.getHeight() - logoH) / 2;
int right = left + logoW;
int bottom = top + logoH;
Rect rect1 = new Rect(left, top, right, bottom);
// 取得qr二维码图片上的画笔,即要在二维码图片上绘制我们的头像
Canvas canvas = new Canvas(qrcodeBitmap);
// 设置我们要绘制的范围大小,也就是头像的大小范围
Rect rect2 = new Rect(0, 0, logoW, logoH);
// 开始绘制
canvas.drawBitmap(logoBitmap, rect2, rect1, null);
} catch (Exception e) {
e.printStackTrace();
}
return qrcodeBitmap;
}
}