您的位置:首页 > 移动开发 > Android开发

Android小技巧/二维码生成Zxing

2013-06-25 14:54 330 查看
先上效果图:



主要实现了渐变与中间加LOGO

二维码的容错性比较强 所以即使ps上去也没有什么关系

步骤 生成渐变二维码 -> 加logo

关键代码:

生成渐变二维码

int WHITE = 0xFFFFFFFF;
int BLACK = 0x78541400;
int pixTemp = BLACK;
for (int y = 0; y < height; y++) {
int offset = y * width;
pixTemp += 100;	//实现渐变效果
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? pixTemp : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);


载入logo水印:

if (background == null) {
return null;
}
forground = getRoundedCornerBitmap(forground);
int bgWidth = background.getWidth();
int bgHeight = background.getHeight();
int forWidth = forground.getWidth();
int forHeight = forground.getHeight();

Bitmap newBitmap = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888);
Canvas cv = new Canvas(newBitmap);
cv.drawBitmap(background, 0, 0, null);
cv.drawBitmap(forground, (bgWidth - forWidth) / 2, (bgHeight - forHeight) / 2, null);// 在src的右下角画入水印
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newBitmap;


增加二维码的容错率设置为H:35%

//	提高纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);


TODO : 事实上这和PS进去一张图片没差别 都是依赖于QRcode的容错性,增加一些噪点去保证准确性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: