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;
|
}
|
}
|
}
|