wxr
2024-04-11 9802cf8c8ad9f392c5c5342a352a17efeef55fc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using HDL_ON_iOS;
using Shared;
using ZXing.Mobile;
using HDL.Shared.IOS.ScanQRCode;
namespace HDL_ON
{
    public class Scan
    {
 
        public string TipMsg = "";
        public Scan()
        {
        }
        private static Scan ins;
 
        public static Scan Ins
        {
            get
            {
                if (ins == null)
                {
                    ins = new Scan();
                }
                return ins;
            }
 
        }
 
        void ScanResult(string result)
        {
            if (result != null)
            {
                action?.Invoke(result);
            }
            else
            {
                action?.Invoke(null);
                Console.WriteLine("二维码返回值为null");
            }
 
        }
 
        static MobileBarcodeScanner scanner;
 
 
        Action<string> action;
 
        public void OpenScan(Action<string> action)
        {
            this.action = action;
 
            string cancel = "取消";
            //string flashText = "";
            string titleText = "二维码扫描";
            if (Language.CurrentLanguage != "Chinese")
            {
                cancel = "Cancel";
                //flashText = "";
                titleText = "Scan";
            }
            var d1 = new HDLQRCodeScanFinish(ScanResult);
            HDL.Shared.IOS.ScanQRCode.HDLScanQRCodeSDK.SharedInstance().ScanQRCodeWith(cancel, titleText, d1);
 
 
 
            //if (scanner == null)
            //{
            //    var mZXingOverlayView = new ZXingOverlayView(cancel, flashText, titleText);
            //    scanner = new MobileBarcodeScanner(AppDelegate.rootViewController) { FlashButtonText = flashText, TopText = titleText, BottomText = "", CancelButtonText = cancel };
            //    scanner.UseCustomOverlay = true;
 
            //    scanner.CustomOverlay = mZXingOverlayView;
            //    var bOn = false;
            //    mZXingOverlayView.OnCancel += () =>
            //    {
            //        scanner?.Cancel();
            //    };
 
            //    mZXingOverlayView.OnTorch += () =>
            //    {
            //        bOn = !bOn;
            //        scanner?.Torch(bOn);
            //    };
 
            //}
 
            //var result = await scanner.Scan();
 
            //if (result != null)
            //{
            //    action?.Invoke(result.Text);
            //}
            //else
            //{
            //    action?.Invoke(null);
            //    Console.WriteLine("二维码返回值为null");
            //}
 
 
 
        }
 
        public static byte[] BytesFromText(string text, int width = 300, int height = 300)
        {
            var barcodeWriter = new ZXing.Mobile.BarcodeWriter
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Width = width,
                    Height = height,
                    Margin = 0
                }
            };
 
            barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
            var uiImage = barcodeWriter.Write(text);
            var data = uiImage.AsJPEG();
 
            var resultBytes = new byte[data.Length];
            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, resultBytes, 0, resultBytes.Length);
 
            return resultBytes;
        }
 
 
        //public static void OpenScan(Action<string> action)
        //{
 
        //    var scanner = new MobileBarcodeScanner(BaseViewController.Instance) { };
        //    // ZXing  
        //    scanner.UseCustomOverlay = false;
        //    // 底部两个按钮文字  
        //    scanner.FlashButtonText = "";//"识别";
        //    scanner.CancelButtonText = Language.CurrentLanguage == "Chinese" ? "取消" : "Cancel";
 
        //    // 方框上,下方文字提示  
        //    //scanner.TopText = Language.CurrentLanguage == "Chinese" ? "请将条形码对准方框内" : "Please align the bar code in the scan box";
        //    scanner.BottomText = "";
 
        //    // 延时三秒自动扫秒  
        //    var opt = new MobileBarcodeScanningOptions();
        //    opt.DelayBetweenContinuousScans = 3000;
        //    scanner.ScanContinuously(opt, (obj) => {
        //        scanner.Cancel();
        //        if (action != null)
        //        {
        //            action(obj.Text);
        //        }
        //    });
        //}
    }
}