【ZXing】Android使用zxing生成二维码图片

时间:2022-11-17 00:25:57

【ZXing】Android使用zxing生成二维码图片


GitHub开源地址:https://github.com/zxing/zxing【ZXing】Android使用zxing生成二维码图片

ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android。

Android平台二维码生成并添加Logo图像的方法如下:

注:本例中使用的是core-2.3-SNAPSHOT.jar和javase-2.3-SNAPSHOT.jar两个Jar包,下载地址:

http://download.csdn.net/detail/wenbitianxiafeng/9073437

package com.feng.androidtest;

import java.util.Hashtable;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;

import com.feng.test.R;
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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class MainActivity extends Activity {

private ImageView mTestIv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTestIv = (ImageView) findViewById(R.id.iv_test);
mTestIv.setImageBitmap(generateQRCode("测试内容!"));
}

/**
* 图像数据转换
* @param matrix
* @return
*/
private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
int w = matrix.getWidth();
int h = matrix.getHeight();
int[] rawData = new int[w * h];
//按照二维码的算法,采用矩阵转换,逐个生成二维码的图片
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int color = Color.WHITE;
if (matrix.get(i, j)) {
color = Color.BLACK;
}
rawData[i + (j * w)] = color;
}
}

Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
return bitmap;
}

/**
* 生成二维码
* @param content
* @return Bitmap对象
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private Bitmap generateQRCode(String content) {
try {
//配置参数
Hashtable hints = new Hashtable();
//设置编码格式,防止汉字乱码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置容错级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置空白边距的宽度,默认为4
hints.put(EncodeHintType.MARGIN, 2);

QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 500, 500, hints);
return addLogo(bitMatrix2Bitmap(matrix), BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}

/**
* 在二维码图片中添加Logo图片
* @param src
* @param logo
* @return
*/
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (src == null) {
return null;
}

if (logo == null) {
return src;
}

//获取图片的宽高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();

if (srcWidth == 0 || srcHeight == 0) {
return null;
}

if (logoWidth == 0 || logoHeight == 0) {
return src;
}

//logo大小为二维码整体大小的1/5
float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}

return bitmap;
}

}

拓展:

Zxing条形码扫描:http://blog.csdn.net/xiaanming/article/details/10163203