wxr
2024-12-02 ea0b1e8e5f43c5fd0a7d479e25ede3b8cbea464a
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
using System;
using System.IO;
using Android.Content;
using Shared;
using ZXing.Mobile;
 
namespace HDL_ON
{
    public class Scan: Java.Lang.Object,HDL_ON_Android.ScanActivity.ScanResult
    {
        public Scan()
        {
        }
 
        private static Scan ins;
 
        public static Scan Ins
        {
            get
            {
                if (ins == null)
                {
                    ins = new Scan();
                }
                return ins;
            }
 
        }
 
        public string TipMsg = string.Empty;
 
        Action<string> resultAction;
        static MobileBarcodeScanner scanner;
 
 
        public void OpenScan(Action<string> action)
        {
            resultAction = action;
 
            ((BaseActivity)Shared.Application.Activity).SetCamera(async (obj) =>
            {
                if (obj)//权限是否打开
                {
                    if (scanner == null)
                    {
 
                        var intent = new Intent(Shared.Application.Activity, typeof(HDL_ON_Android.ScanActivity));
                        Shared.Application.Activity.StartActivity(intent);
 
                        HDL_ON_Android.ScanActivity.InitDdd(this);
 
                    }
                }
                else
                {
                    action?.Invoke(null);
                    Action action1 = () => {
                        Intent intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
                        intent.AddFlags(ActivityFlags.NewTask);
                        Android.Net.Uri uri = Android.Net.Uri.FromParts("package", Application.Activity.PackageName, null);
                        intent.SetData(uri);
                        Application.Activity.StartActivity(intent);
                    };
                    if (string.IsNullOrEmpty(TipMsg))
                    {
                        if (Language.CurrentLanguage == "Chinese")
                        {
                            new HDL_ON.UI.PublicAssmebly().TipOptionMsg(StringId.Tip, "相机访问权限已被拒绝,请前往系统设置打开相关权限。", action1);
                        }
                        else
                        {
                            new HDL_ON.UI.PublicAssmebly().TipOptionMsg(StringId.Tip, "Camera access has been denied. Please go to system settings to open relevant permissions.", action1);
                        }
                    }
                    else
                    {
                        new HDL_ON.UI.PublicAssmebly().TipOptionMsg(StringId.Tip, TipMsg,action1,45);
                        TipMsg = string.Empty;
                    }
                }
            });
        }
 
        public static byte[] BytesFromText(string text, int width = 300, int height = 300)
        {
            if (text == "") return null;
 
            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 bitmap = barcodeWriter.Write(text);
            using (var stream = new MemoryStream())
            {
                bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream);  // this is the diff between iOS and Android
                stream.Position = 0;
                return stream.ToArray();
            }
        }
 
        public void onResult(string result)
        {
            if (!string.IsNullOrEmpty( result))
                resultAction?.Invoke(result);
            else
                resultAction?.Invoke(null);
        }
    }
 
 
 
}