因为项目需要,所以在网上搜了下,发现其实要生成二维码,网上是有很多在线工具的,而且很方便,各式各样的二维码都可以生成,但是,要是想在APP中动态的生成二维码呢,嗯哼,看到大家都是使用一个叫ZXing框架生成二维码的,所以我也就去学习了下,学完之后,我就把那些代码给封装起来了,为了以后使用方便些,嘿嘿,因为老是会忘记里面的代码功能,每次用到又忘记,所以我喜欢用到一个东西的时候就整理起来封装成类,下次用的时候不需要再去研究代码,直接使用就可以了,同样,这个生成带logo二维码的我也封装成一个类了,好了,开始讲怎么使用吧。
第一步:下载ZXing这个jar包,这里我给个下载地址吧,你也可以自己去网上下载
第二步:先贴出我的工具类代码
/** * 这个工具类是生成二维码的 * Created by jjg on 2017/9/14. */ public class ZXingGetQRCodeUtil { /** * 黑点颜色 */ private static final int BLACK = 0xFF000000; /** * 白色 */ private static final int WHITE = 0xFFFFFFFF; /** * 正方形二维码宽度 */ private static final int CODE_WIDTH = 440; /** * LOGO宽度值,最大不能大于二维码20%宽度值,大于可能会导致二维码信息失效 */ private static final int LOGO_WIDTH_MAX = CODE_WIDTH / 5; /** *LOGO宽度值,最小不能小于二维码10%宽度值,小于影响Logo与二维码的整体搭配 */ private static final int LOGO_WIDTH_MIN = CODE_WIDTH / 10; /** * 无参构造方法 */ public ZXingGetQRCodeUtil() { } /** * 生成带LOGO的二维码,返回的是一张带logo的二维码图片 * @param content 二维码显示的内容 * @param logoBitmap 二维码中间的那张logo图片 * @return * @throws WriterException */ public Bitmap createCode(String content, Bitmap logoBitmap) throws WriterException { int logoWidth = logoBitmap.getWidth(); int logoHeight = logoBitmap.getHeight(); int logoHaleWidth = logoWidth >= CODE_WIDTH ? LOGO_WIDTH_MIN : LOGO_WIDTH_MAX; int logoHaleHeight = logoHeight >= CODE_WIDTH ? LOGO_WIDTH_MIN : LOGO_WIDTH_MAX; // 将logo图片按martix设置的信息缩放 Matrix m = new Matrix(); /* * 给的源码是,由于优快云上传的资源不能改动,这里注意改一下 * float sx = (float) 2*logoHaleWidth / logoWidth; * float sy = (float) 2*logoHaleHeight / logoHeight; */ float sx = (float) logoHaleWidth / logoWidth; float sy = (float) logoHaleHeight / logoHeight; m.setScale(sx, sy);// 设置缩放信息 Bitmap newLogoBitmap = Bitmap.createBitmap(logoBitmap, 0, 0, logoWidth, logoHeight, m, false); int newLogoWidth = newLogoBitmap.getWidth(); int newLogoHeight = newLogoBitmap.getHeight(); Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置容错级别,H为最高 hints.put(EncodeHintType.MAX_SIZE, LOGO_WIDTH_MAX);// 设置图片的最大值 hints.put(EncodeHintType.MIN_SIZE, LOGO_WIDTH_MIN);// 设置图片的最小值 hints.put(EncodeHintType.MARGIN, 2);//设置白色边距值 // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_WIDTH, hints); int width = matrix.getWidth(); int height = matrix.getHeight(); int halfW = width / 2; int halfH = height / 2; // 二维矩阵转为一维像素数组,也就是一直横着排了 int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { /* * 取值范围,可以画图理解下 * halfW + newLogoWidth / 2 - (halfW - newLogoWidth / 2) = newLogoWidth * halfH + newLogoHeight / 2 - (halfH - newLogoHeight) = newLogoHeight */ if (x > halfW - newLogoWidth / 2&& x < halfW + newLogoWidth / 2 && y > halfH - newLogoHeight / 2 && y < halfH + newLogoHeight / 2) {// 该位置用于存放图片信息 /* * 记录图片每个像素信息 * halfW - newLogoWidth / 2 < x < halfW + newLogoWidth / 2 * --> 0 < x - halfW + newLogoWidth / 2 < newLogoWidth * halfH - newLogoHeight / 2 < y < halfH + newLogoHeight / 2 * -->0 < y - halfH + newLogoHeight / 2 < newLogoHeight * 刚好取值newLogoBitmap。getPixel(0-newLogoWidth,0-newLogoHeight); */ pixels[y * width + x] = newLogoBitmap.getPixel( x - halfW + newLogoWidth / 2, y - halfH + newLogoHeight / 2); } else { pixels[y * width + x] = matrix.get(x, y) ? BLACK: WHITE;// 设置信息 } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } /** * 这个方法是制作logo的,给出一张背景图片和一张logo图片,返回的是一张制作好的logo图片 * @param bgBitmap 背景图片 * @param logoBitmap logo图片 * @return 返回带有白色背景框logo */ public Bitmap modifyLogo(Bitmap bgBitmap, Bitmap logoBitmap) { int bgWidth = bgBitmap.getWidth(); int bgHeigh = bgBitmap.getHeight(); //通过ThumbnailUtils压缩原图片,并指定宽高为背景图的3/4 logoBitmap = ThumbnailUtils.extractThumbnail(logoBitmap,bgWidth*3/4, bgHeigh*3/4, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); Bitmap cvBitmap = Bitmap.createBitmap(bgWidth, bgHeigh, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(cvBitmap); // 开始合成图片 canvas.drawBitmap(bgBitmap, 0, 0, null); canvas.drawBitmap(logoBitmap,(bgWidth - logoBitmap.getWidth()) /2,(bgHeigh - logoBitmap.getHeight()) / 2, null); canvas.save(Canvas.ALL_SAVE_FLAG);// 保存 canvas.restore(); if(cvBitmap.isRecycled()){ cvBitmap.recycle(); } return cvBitmap; } }可以工具类中有三个方法,第一个是构造方法,原来创建这个工具类对象的,创建了对象后方能使用这个工具类对象去调用另外的两个方法,第二个方法是modifyLogo(背景图,logo图),这个方法是用来创建logo图片的,方法的参数只要你传进去两张图片即可,一张是背景图片,一张是你的logo图片,返回的是一张logo图片,第三个方法是制作二维码的方法createCode(第一个参数是你要二维码显示的内容,第二个参数是你的logo图片),事情就是这么简单,我只要会使用就可以了,具体二维码是怎么生成的,我就没有兴趣了,下面给出demo