zoukankan      html  css  js  c++  java
  • Unity中Zxing生成二维码只能生成256大小图片的解决方案

    /// <summary>
        /// 生成2维码 方法
        /// 经测试:能生成任意尺寸的正方形
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateQRCode2(string _qrcodeText, int _width, int _height, Action<Texture2D> _OnGenerateQRCode)
        {
            string qrcodeText = _qrcodeText;
            int width = _width;
            int height = _height;
            Action<Texture2D> OnGenerateQRCode = _OnGenerateQRCode;
    
            if (qrcodeText != null)
            {
                Loom.RunAsync(() =>
                {
                    try
                    {
                        // 编码成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.M);
                        BitMatrix bitMatrix = writer.encode(qrcodeText, BarcodeFormat.QR_CODE, width, height, hints);
    
                        // 转成texture2d
                        int w = bitMatrix.Width;
                        int h = bitMatrix.Height;
                      
    
                        Color32[] color32 = Encode(qrcodeText, width, height);
                        Loom.QueueOnMainThread(() =>
                        {
                            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.black);
                                    }
                                    else
                                    {
                                        //底色
                                        texture.SetPixel(y, x, Color.white);
                                    }
                                }
                            }
                            texture.Apply();
                            if (OnGenerateQRCode != null)
                            {
                                OnGenerateQRCode(texture);
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        Loom.QueueOnMainThread(() =>
                        {
                            Debug.Log("QRCodeHelper::GenerateQRCode - Exception : " + ex.ToString());
                        });
                    }
                });
            }
            else
            {
                if (OnGenerateQRCode != null)
                {
                    OnGenerateQRCode(null);
                }
            }
        }

    解码:

    public static void DecodeQRCode(Color32[] _data, int _width, int _height, Action<string> _OnDecodeQRCode)
        {
            Color32[] data = _data;
            int width = _width;
            int height = _height;
            Action<string> OnDecodeQRCode = _OnDecodeQRCode;
    
            Loom.RunAsync(() =>
            {
                BarcodeReader barcodeReader = new BarcodeReader { AutoRotate = true };
                barcodeReader.Options.TryHarder = true;
                try
                {
                    // decode the current frame  
                    Result result = barcodeReader.Decode(data, width, height);
                    Loom.QueueOnMainThread(() =>
                    {
                        if (result != null)
                        {
                            if (OnDecodeQRCode != null)
                            {
                                OnDecodeQRCode(result.Text);
                            }
                        }
                        else
                        {
                            if (OnDecodeQRCode != null)
                            {
                                OnDecodeQRCode(null);
                            }
                        }
                    });
                }
                catch
                {
                    Loom.QueueOnMainThread(() =>
                    {
                        if (OnDecodeQRCode != null)
                        {
                            OnDecodeQRCode(null);
                        }
                    });
                }
            });
            // create a reader with a custom luminance source
        }
    private static Color32[] Encode(string textForEncoding, int width, int height)
        {
            BarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width = width
                }
            };
            return writer.Write(textForEncoding);
        }
  • 相关阅读:
    Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程
    Xamarin开发手机聊天程序
    线上处理数据转载
    Logstash
    Webpack按需加载一切皆模块
    网络编程
    NET Core 事件总线
    Using INSERT IGNORE with MySQL to prevent duplicate key errors
    Spring的两种任务调度Scheduled和Async
    Embedded servlet containers
  • 原文地址:https://www.cnblogs.com/leesymbol/p/12050424.html
Copyright © 2011-2022 走看看