个性二维码生成

google官方的Zxing二维码开源包 提供二维码的扫描和生成2个功能:

这里仔细说下生成二维码的方法:

下面代码阐述了生成普通二维码和个性二维码的方法, 特别要注意的是生成个性化的二维码的时候 中间的个性化图标不要超过50*50 否则会出现扫描故障。

google zxing的jar包 下载地址: http://download.csdn.net/detail/songegao/7540539 


/**
* 产生二维码图片 方法名:createImage
*
* @param text
*/
private void createImage(String text) {
try {
// 需要引入core包
QRCodeWriter writer = new QRCodeWriter();
if (text == null || "".equals(text) || text.length() < 1) {
return;
}
// 把输入的文本转为二维码
BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
QR_WIDTH, QR_HEIGHT);

System.out.println("w:" + martix.getWidth() + "h:"
+ martix.getHeight());
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.createBitmap(QR_WIDTH, QR_HEIGHT,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);

// 自定义个性化二维码 中间放图标或者用户图像
// 1-缩放用户图像大小50*50二维码中间的自定义图像区 不能太大 ,个人亲测超过50扫描会有障碍。
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
Bitmap icon2 = scaleBitmap(icon, 50, 50);// 二维码中间的自定义图像区 不能太大
// ,个人亲测超过50扫描会有障碍。
// 2-将用户图像画在二维码中间, 千万别覆盖四个角的扫描线
Bitmap finalBitmap = mergeBitmap(bitmap, icon2);

// 设置给imageview展示
imge.setImageBitmap(finalBitmap);

// 及时回收不要的图片
if (icon != null & !icon.isRecycled()) {
icon2.recycle();
icon.recycle();
bitmap.recycle();
}

} catch (WriterException e) {
e.printStackTrace();
}
}

/**
* 按照固定长宽 缩放图片 方法名:scaleBitmap
*
* @param 需要缩放的图片
* @param newWidth
* 缩放后的宽度
* @param newHeight
* 缩放后的长度
* @return 新的图片
*/
public Bitmap scaleBitmap(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的缩放后的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}

/**
* 合并2张图 方法名:mergeBitmap
*
* @param src
* 背景图
* @param icon
* 需要画在中间的icon
* @return 合成后的图片
*/
public Bitmap mergeBitmap(Bitmap src, Bitmap icon) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = icon.getWidth();
int wh = icon.getHeight();
// create the new blank bitmap
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas cv = new Canvas(newb);
cv.drawBitmap(src, 0, 0, null);
cv.drawBitmap(icon, (w - ww) / 2, (h - wh) / 2, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newb;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值