该代码基于ZXing项目,这个网上能下载得到。
定义的控件以及属性:
public static final int SCAN_CODE = 1;
private ImageView iv;
private EditText et;
private Button qr_btn,add_logo;
private Bitmap logo,bitmap,bmp; //logo图标
private static final int IMAGE_HALFWIDTH = 40;//宽度值,影响中间图片大小
private Uri outputFileUri;
获取图标代码
//获取logo图片资源
add_logo = (Button) findViewById(R.id.add_logo);
add_logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getPicture();
}
});
其中getPicture()方法在上章获取图片方式中有给出。http://blog.csdn.net/u012138137/article/details/52063066 也就是把监听方法改成getPicture()的无返回值方法。再不然就直接把里面的内容全都复制到这个点击监听事件中就好了。
/**
*不带logo的二维码
* @param url 需要生成的地址
*/
public Bitmap createQRImage(String url)
{
try
{
//判断URL合法性
if (url == null || "".equals(url) || url.length() < 1)
{
return null;
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//图像数据转换,使用了矩阵转换
BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);
int[] pixels = new int[300 * 300];
//下面这里按照二维码的算法,逐个生成二维码的图片,
//两个for循环是图片横列扫描的结果
for (int y = 0; y < 300; y++)
{
for (int x = 0; x < 300; x++)
{
if (bitMatrix.get(x, y))
{
pixels[y * 300 + x] = 0xff0000ff;
}
else
{
pixels[y * 300 + x] = 0xffffff00;
}
}
}
//生成二维码图片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, 300, 0, 0, 300, 300);
//显示到一个ImageView上面
return bitmap;
}
catch (WriterException e)
{
e.printStackTrace();
}
return null;
}
/**
* 生成二维码,带Logo
* @param string 二维码中包含的文本信息
* @param mBitmap logo图片
* @param format 编码格式
* @return Bitmap 位图
* @throws WriterException
*/
public Bitmap createCode(String string,Bitmap mBitmap)
throws WriterException {
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();
m.setScale(sx, sy);//设置缩放信息
//将logo图片按martix设置的信息缩放
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false);
MultiFormatWriter writer = new MultiFormatWriter();
Hashtable hst = new Hashtable();
hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置字符编码
BitMatrix matrix = writer.encode(string, BarcodeFormat.QR_CODE, 400, 400, hst);//生成二维码矩阵信息
int width = matrix.getWidth();//矩阵高度
int height = matrix.getHeight();//矩阵宽度
int halfW = width / 2;
int halfH = height / 2;
int[] pixels = new int[width * height];//定义数组长度为矩阵高度*矩阵宽度,用于记录矩阵中像素信息
for (int y = 0; y < height; y++) {//从行开始迭代矩阵
for (int x = 0; x < width; x++) {//迭代列
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
&& y > halfH - IMAGE_HALFWIDTH
&& y < halfH + IMAGE_HALFWIDTH) {//该位置用于存放图片信息
//记录图片每个像素信息
pixels[y * width + x] = mBitmap.getPixel(x - halfW
+ IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); } else {
if (matrix.get(x, y)) {//如果有黑块点,记录信息
pixels[y * width + x] = 0xff000000;//记录黑块信息
}
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
在MainActivity中使用:
qr_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String result = et.getText().toString().trim();
if(TextUtils.isEmpty(result)||"".equals(result)||result.length()<=0){
Toast.makeText(MainActivity.this,"生成地址不能为空",Toast.LENGTH_SHORT).show();
return;
}else {
if(bmp!=null){
//带图片
bitmap = createCode(result,bmp);
iv.setImageBitmap(bitmap);
}else{
//不带图片
bitmap = createQRImage(result);
iv.setImageBitmap(bitmap);
}
}
} catch (WriterException e) {
e.printStackTrace();
}
}
});