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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using AVFoundation;
using CoreGraphics;
using UIKit;
 
namespace HDL_ON
{
    public class ZXingOverlayView : UIView
    {
        public UILabel textBottom;
        UIView redLine;
        public Action OnCancel;
        public Action OnTorch;
        public UIBarButtonItem cancelButton;
        public UIBarButtonItem torchButton;
        public string cancelText = "取消";
        public string flashText = "闪光灯";
        public string bottomText = "扫一扫";
 
        CGSize screenSize = UIScreen.MainScreen.Bounds.Size;
 
        public ZXingOverlayView(string cancelText = "取消", string flashText = "闪光灯", string titleText = "扫一扫") : base()
        {
            this.cancelText = cancelText;
            this.flashText = flashText;
            this.bottomText = titleText;
 
            Opaque = false;
            BackgroundColor = UIColor.Clear;
 
            textBottom = new UILabel()
            {
                Frame = new CGRect(0, (screenSize.Height - 44 + screenSize.Width * 0.75f) / 2, screenSize.Width, 40f),
                Text = bottomText,
                Font = UIFont.SystemFontOfSize(13),
                TextAlignment = UITextAlignment.Center,
                TextColor = UIColor.White,
                Lines = 0,
                BackgroundColor = UIColor.Clear
            };
            this.AddSubview(textBottom);
 
            var captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);
 
            bool hasTorch = false;
 
            if (captureDevice != null)
                hasTorch = captureDevice.TorchAvailable;
 
            InvokeOnMainThread(delegate {
                // Setting tool bar
                var toolBar = new UIToolbar(new CGRect(0, Frame.Height - 44, Frame.Width, 44));
 
                var buttons = new List<UIBarButtonItem>();
                cancelButton = new UIBarButtonItem(cancelText, UIBarButtonItemStyle.Done, delegate { OnCancel?.Invoke(); });
                cancelButton.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
                buttons.Add(cancelButton);
                if (hasTorch)
                {
                    buttons.Add(new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace));
 
                    torchButton = new UIBarButtonItem(flashText, UIBarButtonItemStyle.Done, delegate { OnTorch?.Invoke(); });
                    torchButton.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
                    buttons.Add(torchButton);
                }
 
                toolBar.Items = buttons.ToArray();
                toolBar.BarTintColor = UIColor.Clear;
                toolBar.Translucent = true;
                toolBar.TintColor = UIColor.Black;
                toolBar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin;
                Add(toolBar);
            });
 
        }
 
        public override void Draw(CGRect rect)
        {
            CGRect screenDrawRect = new CGRect(0, 0, screenSize.Width, screenSize.Height - 44);
 
            //中间清空的矩形框
            CGRect clearDrawRect = new CGRect(screenDrawRect.Size.Width * 0.125f, (screenDrawRect.Size.Height - screenDrawRect.Size.Width * 0.75f) / 2,
                                              screenDrawRect.Size.Width * 0.75f, screenDrawRect.Size.Width * 0.75f);
 
            CGContext ctx = UIGraphics.GetCurrentContext();
            AddScreenFillRect(ctx, screenDrawRect);
            AddCenterClearRect(ctx, clearDrawRect);
            AddWhiteRect(ctx, clearDrawRect);
            AddCornerLineWithContext(ctx, clearDrawRect);
        }
        //添加屏幕半透明填充色
        private void AddScreenFillRect(CGContext ctx, CGRect rect)
        {
            ctx.SetFillColor(new CGColor(0, 0, 0, 0.5f));
            ctx.FillRect(rect);
        }
        //添加中心全透明填充色
        private void AddCenterClearRect(CGContext ctx, CGRect rect)
        {
            ctx.ClearRect(rect);
        }
        //根据点画线
        private void AddLine(CGPoint[] pointA, CGPoint[] pointB, CGContext ctx)
        {
            ctx.SetLineWidth(2f);
            ctx.AddLines(pointA);
            ctx.AddLines(pointB);
        }
        //添加白色方框
        private void AddWhiteRect(CGContext ctx, CGRect rect)
        {
            ctx.StrokeRect(rect);
            ctx.SetStrokeColor(new CGColor(1, 1, 1, 1));
            ctx.SetLineWidth(0.8f);
            ctx.AddRect(rect);
            ctx.StrokePath();
        }
        //添加四个角的蓝色
        private void AddCornerLineWithContext(CGContext ctx, CGRect rect)
        {
            //画四个边角
            ctx.SetLineWidth(2f);
            ctx.SetStrokeColor(UIColor.FromRGB(22, 118, 188).CGColor);
 
            //左上角
            CGPoint[] poinsTopLeftA = new CGPoint[] { new CGPoint(rect.Location.X + 0.7f, rect.Location.Y), new CGPoint(rect.Location.X + 0.7f, rect.Location.Y + 15f) };
 
            CGPoint[] poinsTopLeftB = new CGPoint[] { new CGPoint(rect.Location.X, rect.Location.Y + 0.7f), new CGPoint(rect.Location.X + 15f, rect.Location.Y + 0.7f) };
 
            AddLine(poinsTopLeftA, poinsTopLeftB, ctx);
 
            //左下角
            CGPoint[] poinsBottomLeftA = new CGPoint[] { new CGPoint(rect.Location.X + 0.7f, rect.Location.Y + rect.Size.Height - 15f), new CGPoint(rect.Location.X + 0.7f, rect.Location.Y + rect.Size.Height) };
 
            CGPoint[] poinsBottomLeftB = new CGPoint[] { new CGPoint(rect.Location.X, rect.Location.Y + rect.Size.Height - 0.7f), new CGPoint(rect.Location.X + 0.7f + 15f, rect.Location.Y + rect.Size.Height - 0.7f) };
 
            AddLine(poinsBottomLeftA, poinsBottomLeftB, ctx);
 
            //右上角
            CGPoint[] poinsTopRightA = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 15f, rect.Location.Y + 0.7f), new CGPoint(rect.Location.X + rect.Size.Width, rect.Location.Y + 0.7) };
 
            CGPoint[] poinsTopRightB = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 0.7f, rect.Location.Y), new CGPoint(rect.Location.X + rect.Size.Width - 0.7f, rect.Location.Y + 15 + 0.7) };
 
            AddLine(poinsTopRightA, poinsTopRightB, ctx);
 
            CGPoint[] poinsBottomRightA = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 0.7f, rect.Location.Y + rect.Size.Height - 15f), new CGPoint(rect.Location.X - 0.7f + rect.Size.Width, rect.Location.Y + rect.Size.Height) };
 
            CGPoint[] poinsBottomRightB = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 15f, rect.Location.Y + rect.Size.Height - 0.7f), new CGPoint(rect.Location.X + rect.Size.Width, rect.Location.Y + rect.Size.Height - 0.7f) };
 
            AddLine(poinsBottomRightA, poinsBottomRightB, ctx);
 
            ctx.StrokePath();
        }
 
        public void Destroy()
        {
            InvokeOnMainThread(() => {
                textBottom.RemoveFromSuperview();
                redLine.RemoveFromSuperview();
 
                textBottom = null;
                redLine = null;
            });
        }
 
    }
}