Android动态生成二维码

Android动态生成二维码,第1张

概述什么是二维码二维码又称二维条码,常见的二维码为QRCode,QR全称QuickResponse,是一个近几年来移动设备上超流行的一种编码方式,它比传统的BarCode条形码能存更多的信息,也能表示更多的数据类型。动态生成二维码方案在查找二维码生成方案时,发现很多方案的源头都指向了GitHub的开源库htt

什么是二维码

二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的bar Code条形码能存更多的信息,也能表示更多的数据类型。

动态生成二维码方案

在查找二维码生成方案时,发现很多方案的源头都指向了GitHub的开源库https://github.com/zxing/zxing。
1. ZXing简介:

ZXing全称zebra crossing,翻译过来就是『斑马线』的意思。ZXing是一个采用Java实现的、开源的、支持多格式(一维/二维)的条形码图像处理库。

其中,QRCode格式就是我们常说的二维码格式。

注:QRCode(Quick Response Code:快速响应码)是二维条形码中最常用的一种格式,所以很多人直接将QRCode翻译为二维码,而且连百度百科都这样称呼,笔者也暂时就这么称呼了。


2. ZXing库引入

对于开发者来讲,我们需要下载ZXing库的一个jar包(core-x.x.x.jar)或者通过添加依赖的方式引入库文件,具体方法如下:

方法一:ZXing提供了Maven库,让我们可以根据自己的需要选择想要的jar包版本进行下载。Maven库:https://repo1.maven.org/maven2/com/Google/zxing/core/

方法二(推荐):对于使用AndroIDStudio开发的程序员而言,可能更习惯于在.gradle文件中添加依赖。具体代码如下(3.3.0是笔者使用时的最新版本,想知道最新版本是多少可以去Maven库查):

dependencIEs {
   compile 'com.Google.zxing:core:3.3.0'
}

3. ZXing库的封装

/**
* @Classname: QRCodeUtil
* @Description: 二维码工具类
* @Author Wangnan
* @Date 2017/2/10
*/
public class QRCodeUtil {
   /**
    * 创建二维码位图
    *
    * @param content 字符串内容
    * @param size 位图宽&高(单位:px)
    * @return
    */
   @Nullable
   public static Bitmap createQRCodeBitmap(@Nullable String content, int size){
       return createQRCodeBitmap(content, size, "UTF-8", "H", "4", color.BLACK, color.WHITE, null, null, 0F);
   }
   /**
    * 创建二维码位图 (自定义黑、白色块颜色)
    *
    * @param content 字符串内容
    * @param size 位图宽&高(单位:px)
    * @param color_black 黑色色块的自定义颜色值
    * @param color_white 白色色块的自定义颜色值
    * @return
    */
   @Nullable
   public static Bitmap createQRCodeBitmap(@Nullable String content, int size, @colorInt int color_black, @colorInt int color_white){
       return createQRCodeBitmap(content, size, "UTF-8", "H", "4", color_black, color_white, null, null, 0F);
   }
   /**
    * 创建二维码位图 (带logo小图片)
    *
    * @param content 字符串内容
    * @param size 位图宽&高(单位:px)
    * @param logoBitmap logo图片
    * @param logoPercent logo小图片在二维码图片中的占比大小,范围[0F,1F]。超出范围->默认使用0.2F
    * @return
    */
   @Nullable
   public static Bitmap createQRCodeBitmap(String content, int size, @Nullable Bitmap logoBitmap, float logoPercent){
       return createQRCodeBitmap(content, size, "UTF-8", "H", "4", color.BLACK, color.WHITE, null, logoBitmap, logoPercent);
   }
   /**
    * 创建二维码位图 (Bitmap颜色代替黑色) 注意!!!注意!!!注意!!! 选用的Bitmap图片一定不能有白色色块,否则会识别不出来!!!
    *
    * @param content 字符串内容
    * @param size 位图宽&高(单位:px)
    * @param targetBitmap 目标图片 (如果targetBitmap != null, 黑色色块将会被该图片像素色值替代)
    * @return
    */
   @Nullable
   public static Bitmap createQRCodeBitmap(String content, int size, Bitmap targetBitmap){
       return createQRCodeBitmap(content, size, "UTF-8", "H", "4", color.BLACK, color.WHITE, targetBitmap, null, 0F);
   }
   /**
    * 创建二维码位图 (支持自定义配置和自定义样式)
    *
    * @param content 字符串内容
    * @param size 位图宽&高(单位:px)
    * @param character_set 字符集/字符转码格式 (支持格式:{@link CharacterSetECI })。传null时,zxing源码默认使用 "ISO-8859-1"
    * @param error_correction 容错级别 (支持级别:{@link ErrorCorrectionLevel })。传null时,zxing源码默认使用 "L"
    * @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
    * @param color_black 黑色色块的自定义颜色值
    * @param color_white 白色色块的自定义颜色值
    * @param targetBitmap 目标图片 (如果targetBitmap != null, 黑色色块将会被该图片像素色值替代)
    * @param logoBitmap logo小图片
    * @param logoPercent logo小图片在二维码图片中的占比大小,范围[0F,1F],超出范围->默认使用0.2F。
    * @return
    */
   @Nullable
   public static Bitmap createQRCodeBitmap(@Nullable String content, int size,
                                           @Nullable String character_set, @Nullable String error_correction, @Nullable String margin,
                                           @colorInt int color_black, @colorInt int color_white, @Nullable Bitmap targetBitmap,
                                           @Nullable Bitmap logoBitmap, float logoPercent){
       /** 1.参数合法性判断 */
       if(TextUtils.isEmpty(content)){ // 字符串内容判空
           return null;
       }
       if(size <= 0){ // 宽&高都需要>0
           return null;
       }
       try {
           /** 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */
           Hashtable<EncodeHintType, String> hints = new Hashtable<>();
           if(!TextUtils.isEmpty(character_set)) {
               hints.put(EncodeHintType.CHaraCTER_SET, character_set); // 字符转码格式设置
           }
           if(!TextUtils.isEmpty(error_correction)){
               hints.put(EncodeHintType.ERROR_CORRECTION, error_correction); // 容错级别设置
           }
           if(!TextUtils.isEmpty(margin)){
               hints.put(EncodeHintType.margin, margin); // 空白边距设置
           }
           BitMatrix bitMatrix = new QRCodeWriter().encode(content, barcodeFormat.QR_CODE, size, size, hints);
           /** 3.根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
           if(targetBitmap != null){
               targetBitmap = Bitmap.createScaledBitmap(targetBitmap, size, size, false);
           }
           int[] pixels = new int[size * size];
           for(int y = 0; y < size; y++){
               for(int x = 0; x < size; x++){
                   if(bitMatrix.get(x, y)){ // 黑色色块像素设置
                       if(targetBitmap != null) {
                           pixels[y * size + x] = targetBitmap.getPixel(x, y);
                       } else {
                           pixels[y * size + x] = color_black;
                       }
                   } else { // 白色色块像素设置
                       pixels[y * size + x] = color_white;
                   }
               }
           }
           /** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象 */
           Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
           bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
           /** 5.为二维码添加logo小图标 */
           if(logoBitmap != null){
               return addlogo(bitmap, logoBitmap, logoPercent);
           }
           return bitmap;
       } catch (WriterException e) {
           e.printstacktrace();
       }
       return null;
   }
   /**
    * 向一张图片中间添加logo小图片(图片合成)
    *
    * @param srcBitmap 原图片
    * @param logoBitmap logo图片
    * @param logoPercent 百分比 (用于调整logo图片在原图片中的显示大小, 取值范围[0,1], 传值不合法时使用0.2F)
    *                    原图片是二维码时,建议使用0.2F,百分比过大可能导致二维码扫描失败。
    * @return
    */
   @Nullable
   private static Bitmap addlogo(@Nullable Bitmap srcBitmap, @Nullable Bitmap logoBitmap, float logoPercent){
       /** 1. 参数合法性判断 */
       if(srcBitmap == null){
           return null;
       }
       if(logoBitmap == null){
           return srcBitmap;
       }
       if(logoPercent < 0F || logoPercent > 1F){
           logoPercent = 0.2F;
       }
       /** 2. 获取原图片和logo图片各自的宽、高值 */
       int srcWIDth = srcBitmap.getWIDth();
       int srcHeight = srcBitmap.getHeight();
       int logoWIDth = logoBitmap.getWIDth();
       int logoHeight = logoBitmap.getHeight();
       /** 3. 计算画布缩放的宽高比 */
       float scaleWIDth = srcWIDth * logoPercent / logoWIDth;
       float scaleHeight = srcHeight * logoPercent / logoHeight;
       /** 4. 使用Canvas绘制,合成图片 */
       Bitmap bitmap = Bitmap.createBitmap(srcWIDth, srcHeight, Bitmap.Config.ARGB_8888);
       Canvas canvas = new Canvas(bitmap);
       canvas.drawBitmap(srcBitmap, 0, 0, null);
       canvas.scale(scaleWIDth, scaleHeight, srcWIDth/2, srcHeight/2);
       canvas.drawBitmap(logoBitmap, srcWIDth/2 - logoWIDth/2, srcHeight/2 - logoHeight/2, null);
       return bitmap;
   }
}

4.动态生成二维码

Bitmap bitmap = QRCodeUtils.createQRCodeBitmap(qrUrl, 500);

5. 效果

总结

以上是内存溢出为你收集整理的Android动态生成二维码全部内容,希望文章能够帮你解决Android动态生成二维码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1053364.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)