现在的位置: 首页 > 综合 > 正文

Android小技巧/二维码生成Zxing

2018年02月04日 ⁄ 综合 ⁄ 共 1037字 ⁄ 字号 评论关闭

先上效果图:

主要实现了渐变与中间加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的容错性,增加一些噪点去保证准确性。

抱歉!评论已关闭.