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