码迷,mamicode.com
首页 > 移动开发 > 详细

android使用qrcode_swetake.jar生成二维码

时间:2015-06-11 20:51:17      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

只有生成二维码的代码

优点是占用方法数比较少 以防65535

 

public class QRcodeUtil {

	static Paint paint;
	static {
		paint = new Paint(); // 设置一个笔刷大小是3的黄色的画笔
		paint.setColor(Color.BLACK);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(3);
	}
	private static final int PIC_SIZE = 300;
	private static final int RECT_SIZE = 6;
	/**
	 * 生成二维码(QRCode)图片
	 * 
	 * @param content
	 * @param 
	 */
	public Bitmap encoderQRCode(String content) {
		Canvas g;
		try {
			Qrcode qrcodeHandler = new Qrcode();
			qrcodeHandler.setQrcodeErrorCorrect(‘M‘);
			qrcodeHandler.setQrcodeEncodeMode(‘B‘);
			qrcodeHandler.setQrcodeVersion(8);

			byte[] contentBytes = content.getBytes("utf-8");
			Bitmap bufferBitMap = Bitmap.createBitmap(PIC_SIZE, PIC_SIZE, Bitmap.Config.ARGB_8888);
			g = new Canvas(bufferBitMap);
			g.drawColor(Color.WHITE);
			// 设置偏移量 不设置可能导致解析出错
			int pixoff = 2;
			// 输出内容 > 二维码
			if (contentBytes.length > 0 && contentBytes.length < 512 ) {
				boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
				for (int i = 0; i < codeOut.length; i++) {
					for (int j = 0; j < codeOut.length; j++) {
						if (codeOut[j][i]) {
							float left = j * RECT_SIZE + pixoff;
							float top = i * RECT_SIZE + pixoff;
							float right = (j + 1) * RECT_SIZE + pixoff;
							float bottom = (i + 1) * RECT_SIZE + pixoff;
							g.drawRect(left, top, right, bottom, paint);
						}
					}
				}
			} else {
				Log.e("QRCodeEncoderHandler", "QRCode content bytes length = " + contentBytes.length
						+ " not in [ 0,120 ]. ");
			}
			return bufferBitMap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

  

android使用qrcode_swetake.jar生成二维码

标签:

原文地址:http://www.cnblogs.com/waterbear/p/4569855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!