wxr
2020-06-09 77e7b5223dd04a6e036dc952efb38f2b770a6828
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
using System;
using System.Net;
using CoreGraphics;
using UIKit;
 
namespace Shared
{
    public class UrlMonitor : View
    {
        public UrlMonitor()
        {
            uiView = new UIImageView { Tag=int.MinValue};
        }
 
        public 
 
        //是否已经启动
        bool isStarted;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName = "";
        /// <summary>
        /// 密码
        /// </summary>
        public string Password = "";
 
        /// <summary>
        /// 摄像头的图片地址
        /// </summary>
        public string URL;
 
        /// <summary>
        /// 启动
        /// </summary>
        public void Start()
        {
            if (isStarted)
            {
                return;
            }
            isStarted = true;
            System.Threading.Tasks.Task.Run(() =>
                {
                    while (isStarted)
                    {
                        System.Threading.Thread.Sleep(100);
                        try
                        {
                            WebClient webClient = new WebClient();
                            webClient.Headers.Add("Accept-Encoding", "gzip, deflate");
                            webClient.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(UserName + ":" + Password)));
                            var bytes = webClient.DownloadData(URL);
                            var uiImage = UIImage.LoadFromData(Foundation.NSData.FromArray(bytes));
                            Application.RunOnMainThread(() =>
                            {
                                try
                                {
                                    (uiView as UIImageView).Image = imimage(uiImage);
                                }
                                catch { }
                            });
                        }
                        catch { }
                    }
                });
        }
        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            isStarted = false;
        }
 
        /// <summary>
        /// 旋转当前画面
        /// </summary>
        /// <returns>源图</returns>
        /// <param name="image">旋转后的界面</param>
        UIImage imimage(UIImage image)
        {
            if (Application.IsPad)
            {
                return image;
            }
            else
            {
                var rotate = 3 * Math.PI / 2;
                var rect = new CGRect(0, 0, image.Size.Height, image.Size.Width);
                nfloat translateX = -rect.Size.Height;
                nfloat translateY = 0;
                nfloat scaleX = rect.Size.Height / rect.Size.Width;
                nfloat scaleY = rect.Size.Width / rect.Size.Height;
                //case UIImageOrientation.Right:
                //rotate = 3 * Math.PI / 2;
                //    rect = new CGRect(0, 0, image.Size.Height, image.Size.Width);
                //    translateX = -rect.Size.Height;
                //    translateY = 0;
                //    scaleY = rect.Size.Width / rect.Size.Height;
                //    scaleX = rect.Size.Height / rect.Size.Width;
                //    break;
                //case UIImageOrientation.Down:
                //    rotate = M_PI;
                //    rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
                //    translateX = -rect.Size.Width;
                //    translateY = -rect.Size.Height;
                //    break;
                //default:
                //    rotate = 0.0f;
                //    rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
                //    translateX = 0;
                //    translateY = 0;
                //    break;
 
                UIGraphics.BeginImageContext(rect.Size);
                var context = UIGraphics.GetCurrentContext();
                //做CTM变换
                context.TranslateCTM(0.0f, rect.Size.Height);
                context.ScaleCTM(1.0f, -1.0f);
                context.RotateCTM((nfloat)rotate);
                context.TranslateCTM(translateX, translateY);
                context.ScaleCTM(scaleX, scaleY);
 
                //绘制图片
                context.DrawImage(new CGRect(0, 0, rect.Size.Width, rect.Size.Height), image.CGImage);
                return UIGraphics.GetImageFromCurrentImageContext();
            }
        }
    }
}