首页 > 其他分享 >Android 二维码相关(一)

Android 二维码相关(一)

时间:2024-03-08 11:37:11浏览次数:23  
标签:logoPercent bitmap 二维码 logo 相关 Android Bitmap size

Android 二维码相关(一)

本篇文章主要记录下android下使用zxing来创建二维码.

1: 导入依赖

api "com.google.zxing:core:3.5.1"

2: 创建二维码

  1. 创建QRCodeWriter对象

    QRCodeWriter qrCodeWriter = new QRCodeWriter();	
    
  2. 将文本内容转换成BitMatrix

    BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size);
    
  3. 创建bitmap

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
    
  4. 将BitMatrix渲染到bitmap

    for (int x = 0; x < size; x++) {
        for (int y = 0; y < size; y++) {
            //将BitMatrix渲染到bitmap
            bitmap.setPixel(x, y, encode.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }
    

完整的代码如下:

public class QRCodeUtils {
    private static final String TAG = "QRCodeUtils";
      /**
     * @param content 字符串内容
     * @param size    位图宽&高(单位:px)
     * @return
     */
    public static Bitmap createQRCodeBitmap(String content, int size) {
        if (TextUtils.isEmpty(content)) return null;
        if (size <= 0) return null;
        //创建QRCodeWriter对象
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        try {
            //使用QRCodeWriter将文本内容转换成BitMatrix.
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size);
            Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);

            for (int x = 0; x < size; x++) {
                for (int y = 0; y < size; y++) {
                    //将BitMatrix渲染到bitmap
                    bitmap.setPixel(x, y, encode.get(x, y) ? Color.BLACK : Color.WHITE);
                }
            }
            return bitmap;
        } catch (Throwable e) {
            Log.e(TAG, "createQRCodeBitmap: ", e);
        }
        return null;
    }
}

给二维码添加logo 与上篇文章添加水印类似.

都是通过canvas重新绘制,合成图片.

具体的代码如下:

  /**
     * @param srcBitmap   二维码
     * @param logoBitmap  二维码logo
     * @param logoPercent 二维码logo的占比 [0,1]
     * @return
     */
    public static Bitmap addQRCodeLogo(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent) {
        //校验参数合法
        if (srcBitmap == null) return null;
        if (logoBitmap == null) return srcBitmap;
        if (logoPercent < 0 || logoPercent > 1) logoPercent = 0.2f;

        //原图/logo的宽高
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
        int logoHeight = logoBitmap.getHeight();
        int logoWidth = logoBitmap.getWidth();

        //缩放
        float scaleWidth = srcWidth * logoPercent / logoWidth;
        float scaleHeight = srcHeight * logoPercent / logoHeight;

        //使用Canvas绘制
        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.RGB_565);
        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;
    }

新增创建二维码方法:

/**
 * @param content 字符串内容
 * @param size    位图宽&高(单位:px)
 * @param logo    二维码logo
 * @param logoPercent 二维码logo的占比 [0,1]
 * @return
 */
public static Bitmap createQRCodeBitmap(String content, int size, Bitmap logo, float logoPercent) {
    Bitmap qrCodeBitmap = createQRCodeBitmap(content, size);
    Bitmap bitmap = addQRCodeLogo(qrCodeBitmap, logo, logoPercent);
    return bitmap;
}

本文由博客一文多发平台 OpenWrite 发布!

标签:logoPercent,bitmap,二维码,logo,相关,Android,Bitmap,size
From: https://www.cnblogs.com/zhjing/p/18060586

相关文章

  • 解决 Android studio Connect to 127.0.0.1:[/127.0.0.1] failed: Connection refused
    前言由于代理变更,androidstudio会有一系列报错,其中一个是Connectto127.0.0.1:xxxxxx[/127.0.0.1]failed:Connectionrefused网上答案大都太片面了,无法完全解决问题,这里列举出四个可能的原因,希望对大家有用问题如下建议一下四种方案都尝试下,我相信总有一种能......
  • 前端水印相关解析
    一、问题背景为了防止信息泄露或知识产权被侵犯,在web的世界里,对于页面和图片等增加水印处理是十分有必要的,水印的添加根据环境可以分为两大类,前端浏览器环境添加和后端服务环境添加,根据可见性,网页水印可以分为可见水印和不可见水印(盲水印/隐水印),简单对比一下这两种方式的特点:前......
  • 对于core-js相关的报错 core-js/modules/es.array.push.js in ./node_modules/.store
    Thesedependencieswerenotfound:*core-js/modules/es.array.push.jsin./node_modules/.store/@[email protected]/node_modules/@babel/runtime/helpers/construct.js,./node_modules/.store/@[email protected]/node_modules/@babel/runtime/helpers/esm/obj......
  • Android mount: bad /etc/fstab: No such file or directory
    没有root权限的原因,需要su切换到root用户https://github.com/termux/termux-packages/issues/7256 I/OerrorRMX1901CN:/#mount/dev/block/by-name/abl/mnt/mntablmount:'/dev/block/by-name/abl'->'/mnt/mntabl':I/Oerrorablxbl都会出现I/Oerror,不知道什么原因......
  • 华企盾DSC的屏幕浮水印可以设置哪些相关法律话术?
    华企盾DSC防泄密系统的屏幕浮水印和进程浮水印能够设置相关法律话术作为水印内容,以增加警示作用。具体的法律话术设置并未给出直接的示例,但理论上可以设定涉及数据保密、著作权声明、适当的法律责任警告等任何合法、合规的法律性内容。例如: "本计算机受到公司数据保密政策......
  • Android hexedit toybox tcsetattr /dev/pts/0: Permission denied
    cas:/$/data/local/tmp/toybox-aarch64hexedit/data/local/tmp/tree.statichexedit:tcsetattr/dev/pts/0:Permissiondenied 好像是不能用tcsetattr,selinux会拒绝 cas:/$ls-l/dev/ptsls:/dev/pts:Permissiondenied https://blog.zhanghai.me/fixing-line-e......
  • 第三节:队列相关(滑动窗口最大值、)
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • bitset 相关优化
    bitset基础用法operator[]:访问其特定的一位。operator==/!=:比较两个bitset内容是否完全一样。operator&/&=/|/|=/^/^=/~:进行按位与/或/异或/取反操作。bitset只能与bitset进行位运算,若要和整型进行位运算,要先将整型转换为bitset。operator<>/<<=/>>=:进行......
  • Android开发学习之路01
    今日跟着一个人进行了Androidstudio上创建数据库和数据表的联系,这应该是老师留的作业中,进行数据库的连接。原文链接:https://blog.csdn.net/fjh_xx/article/details/131404230一.前言二.SQLite数据库介绍1.什么是SQLite数据库2.特点3.SQLite操作API4.SQLite数据类型三.S......
  • android studio
    避坑:1.此处替换路径下载地址:https://mirrors.cloud.tencent.com/gradle/2.D:\app\gradle-6.7.1\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-compiler-embeddable\1.8.10\2a38b258da57285fb187fc329f7374c0751576af此处下载对应文件后将原文件替换注意修改......