Android 生成二维码

2017-06-23  本文已影响145人  silencefun

二维码的开发使用大多都是使用Google提供的zxing这个类库.:引用的JAR包下载地址

核心生成 代码段

 /**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000;
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {
            bitmap = addLogo(bitmap, logoBm);
        }
        //建议使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}

也可以为二维码中间添加logo

添加 logo的 为

/**
 * 在二维码中间添加Logo图案
 */
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
    if (src == null) {
        return null;
    }
    if (logo == null) {
        return src;
    }
    //获取图片的宽高
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    int logoWidth = logo.getWidth();
    int logoHeight = logo.getHeight();
    if (srcWidth == 0 || srcHeight == 0) {
        return null;
    }
    if (logoWidth == 0 || logoHeight == 0) {
        return src;
    }
    //logo大小为二维码整体大小的1/5
    float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
    Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
    try {
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(src, 0, 0, null);
        canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
        canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
    } catch (Exception e) {
        bitmap = null;
        e.getStackTrace();
    }
    return bitmap;
}

在需要使用的地方

 Bitmap qrBitmap= EncodingUtils.createQRCode(url, 400, 400, null);
  imageview.setImageBitmap(qrBitmap);

其实这个根本没有什么难度和技巧.尝试分析bitmap作为二维码绘制内存占用和优化办法.

bitmap作为一个大胖子.分析下构成.
拿绘制二维码的配置来言:Bitmap.Config.ARGB_8888

官方解释:

Each pixel is stored on 4 bytes. 
Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.) 
This configuration is very flexible and offers the best quality. It should be used whenever possible
一个像素占用4个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占8个bites共32bites,即4个字节.
(注:ARGB是一种色彩模式,里面A代表Alpha,R表示red,G表示green,B表示blue,其实所有的可见色都是红绿蓝组成的,所以红绿蓝又称为三原色。)

那么 就像上边写的 400px*400px ,那么一共消耗:

ARGB_8888  图片长度*图片宽度*4  400*400*4=640000字节=625kb

如果更大,或者展示数量多,不可避免的需要消耗内存更多,那么必须要考虑优化.

压缩(compress)

一般是一搜一堆的这样的方法:

ByteArrayOutputStream out = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 80, out);  
imageView.setImageBitmap(bm);  

很容易进入误区,这是压缩存储的大小.

Bitmap.compress方法确实可以压缩图片,但压缩的是存储大小

这样子根本是不起作用的,正常的做法是压缩宽高比(对于那些巨大的Bitmap).

当然及时回收也是有必要的recycle()方法.

BitmapDrawable drawable = (BitmapDrawable) erweicode.getDrawable();
    Bitmap bmp = drawable.getBitmap();
    if (null != bmp && !bmp.isRecycled()) {
        bmp.recycle();
        bmp = null;
    }
上一篇 下一篇

猜你喜欢

热点阅读