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; /// /// 用户名 /// public string UserName = ""; /// /// 密码 /// public string Password = ""; /// /// 摄像头的图片地址 /// public string URL; /// /// 启动 /// 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 { } } }); } /// /// 停止播放 /// public void Stop() { isStarted = false; } } }