Unity 生成二维码和中间带图标的二维码、包括扫,识别二维码 - 基于zxing.unity.dll(Android 和 Windows亲测可用)

45 篇文章 10 订阅
43 篇文章 1 订阅

Unity 生成二维码和中间带图标的二维码、包括扫,识别二维码(Android 和 Windows亲测可用)

在这里插入图片描述
在这里插入图片描述

demo下载地址
https://download.csdn.net/download/qq_39735878/20328497

1,生成二维码(带图标)

    /// <summary>  
    /// 生成普通二维码  
    /// </summary>  
    public void Btn_CreatQr()
    {
       
        if (QrCodeStr[Nmuber].Length > 1)
        {
            //二维码写入图片    
            var color32 = Encode(QrCodeStr[Nmuber], encoded.width, encoded.height);
            encoded.SetPixels32(color32);
            encoded.Apply();
            //生成的二维码图片附给RawImage    
            image.texture = encoded;
        }
        else
        {
            GameObject.Find("Text_1").GetComponent<Text>().text = "没有生成信息";
        }
    }
    /// <summary>
    /// 定义方法生成二维码 
    /// </summary>
    /// <param name="textForEncoding">需要生产二维码的字符串</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    /// <returns></returns>       
    private static Color32[] Encode(string textForEncoding, int width, int height)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width
            }
        };
        return writer.Write(textForEncoding);
    }

    /// <summary>
    /// 生成2维码,添加小图标
    /// </summary>
    /// <param name="content"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    void GenerateQRImage3(string content, int width, int height, Texture2D centerIcon)
    {
        // 编码成color32
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.Add(EncodeHintType.MARGIN, 1);
        hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        // 转成texture2d
        int w = bitMatrix.Width;
        int h = bitMatrix.Height;
        Texture2D texture = new Texture2D(w, h);
        for (int x = 0; x < h; x++)
        {
            for (int y = 0; y < w; y++)
            {
                if (bitMatrix[x, y])
                {
                    texture.SetPixel(y, x, Color.blue);
                }
                else
                {
                    texture.SetPixel(y, x, Color.white);
                }
            }
        }
        // 添加小图
        int halfWidth = texture.width / 2;
        int halfHeight = texture.height / 2;
        int halfWidthOfIcon = centerIcon.width / 2;
        int halfHeightOfIcon = centerIcon.height / 2;
        int centerOffsetX = 0;
        int centerOffsetY = 0;
        for (int x = 0; x < h; x++)
        {
            for (int y = 0; y < w; y++)
            {
                centerOffsetX = x - halfWidth;
                centerOffsetY = y - halfHeight;
                if (Mathf.Abs(centerOffsetX) <= halfWidthOfIcon && Mathf.Abs(centerOffsetY) <= halfHeightOfIcon)
                {
                    texture.SetPixel(x, y, centerIcon.GetPixel(centerOffsetX + halfWidthOfIcon, centerOffsetY + halfHeightOfIcon));
                }
            }
        }
        texture.Apply();
        image.texture = texture;
        // 存储成文件
        byte[] bytes = texture.EncodeToPNG();
        string path = System.IO.Path.Combine(Application.dataPath, "qr.png");
        System.IO.File.WriteAllBytes(path, bytes);
    }

2,扫描,识别二维码

    /// <summary>
    /// 识别摄像机图片中的二维码信息
    /// 打印二维码识别到的信息
    /// </summary>
    void ScanQRCode()
    {
        //7、获取摄像机画面的像素颜色数组信息
        data = webCamTexture.GetPixels32();
        //8、获取图片中的二维码信息
        Result result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);
        //如果获取到二维码信息了,打印出来
        if (result != null)
        {
            Debug.Log(result.Text);//二维码识别出来的信息
            text.text = result.Text;//显示扫描信息

            //扫描成功之后的处理
            IsScanning = false;
            webCamTexture.Stop();
        }
    }
    /// <summary>
    /// 开启摄像机和准备工作
    /// </summary> 
    void DeviceInit()
    {
        //1、获取所有摄像机硬件
        WebCamDevice[] devices = WebCamTexture.devices;
        //2、获取第一个摄像机硬件的名称
        string deviceName = devices[0].name;//手机后置摄像机
        //3、创建实例化一个摄像机显示区域
        webCamTexture = new WebCamTexture(deviceName, 400, 300);
        //4、显示的图片信息
        cameraTexture.texture = webCamTexture;
        //5、打开摄像机运行识别
        webCamTexture.Play();

        //6、实例化识别二维码信息存储对象
        barcodeReader = new BarcodeReader();
    }

3,项目下载地址 https://download.csdn.net/download/qq_39735878/20328497

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unity_YTWJJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值