二维码

来源:互联网 发布:淘宝黑号怎么洗白 编辑:程序博客网 时间:2024/04/30 10:46

1、生成二维码

/** * @Title: createQRImage * @param url *            可以是链接地址,也可以是文字 * @param QR_WIDTH *            二维码宽度 * @param QR_HEIGHT *            二维码高度 * @return Bitmap 返回一个bitmap,可以自行保存到本地,也可以设置显示在页面 */public static Bitmap createQRImage(String url, int QR_WIDTH, int QR_HEIGHT) {Bitmap bitmap = null;try {// 判断URL合法性if (url == null || "".equals(url) || url.length() < 1) {return bitmap;}Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 图像数据转换,使用了矩阵转换BitMatrix bitMatrix = new QRCodeWriter().encode(url,BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);int[] pixels = new int[QR_WIDTH * QR_HEIGHT];// 下面这里按照二维码的算法,逐个生成二维码的图片,// 两个for循环是图片横列扫描的结果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;}}}// 生成二维码图片的格式,使用ARGB_8888bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);} catch (WriterException e) {e.printStackTrace();return null;}return bitmap;}


参考文章:http://blog.csdn.net/u012885461/article/details/49617729


2、扫描二维码详细完整源码下载

http://download.csdn.net/detail/xinnian25/9788221



0 0