wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
using System;
using System.Net;
using Android.Graphics;
using Android.Graphics.Drawables;
 
namespace Shared
{
    public class UrlMonitor : View
    {
        public UrlMonitor()
        {
            AndroidView = new Android.Widget.TextView(Application.Activity);
        }
 
        //是否已经启动
        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 bitmpap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
 
                            BitmapDrawable background = null;
                            if (Application.IsPad)
                            {
                                background = new BitmapDrawable(bitmpap);
                            }
                            else
                            {
                                var matrix = new Matrix();
                                //旋转图片 动作
                                matrix.PostRotate(90);
                                var newBitmpap = Bitmap.CreateBitmap(bitmpap, 0, 0, bitmpap.Width, bitmpap.Height, matrix, false);
                                background = new BitmapDrawable(newBitmpap);
                                if (bitmpap != newBitmpap)
                                {
                                    bitmpap.Recycle();
                                }
                            }
                            
                            Shared.Application.RunOnMainThread(() =>
                                    {
                                        try
                                        {
                                            AndroidView.Background = background;
                                        }
                                        catch { }
                                    });
                        }
                        catch { }
                    }
                });
        }
        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            isStarted = false;
        }
    }
}