using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
using System.IO;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace Darkthread
{ public class CameraProxy
{ private string gatewayUrl = null;
private IApplicationBar appBar = null;
private Border mask = new Border()
{ Background = new SolidColorBrush(Colors.White),
Opacity = 0.7,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Visibility = Visibility.Collapsed
};
//Constructor, url for WebCam Gateway, page for UI blocking
public CameraProxy(string url, PhoneApplicationPage page)
{ gatewayUrl = url;
if (page != null)
{ appBar = page.ApplicationBar;
//Try to get LayoutRoot element
Panel pnl =
VisualTreeHelper.GetChild(page, 0) as Panel;
//If the LayoutRoot is a container
if (pnl != null) //Add a mask element
pnl.Children.Add(mask);
else
mask = null;
}
}
//Completed event compatible with CameraCaptureTask
public event Action<object, PhotoResult> Completed;
public void Show()
{ WebClient wc = new WebClient();
wc.OpenReadCompleted += (sender, e) =>
{ try
{ //e.Result is the stream that contains image we need
PhotoResultHacking pr = new PhotoResultHacking(
Microsoft.Phone.Tasks.TaskResult.OK, e.Result);
if (Completed != null)
Completed(this, pr);
}
catch
{ //If any exception, return TaskResult.Cancel
PhotoResult epr = new PhotoResult(TaskResult.Cancel);
if (Completed != null)
Completed(this, epr);
}
finally
{ //Remove the blocking mask
if (mask != null)
mask.Visibility = Visibility.Collapsed;
//Show the ApplicationBar
if (appBar != null)
appBar.IsVisible = true;
}
};
//Add a mask to block the UI
if (mask != null)
mask.Visibility = Visibility.Visible;
//Hide ApplicationBar to block the UI
if (appBar != null)
appBar.IsVisible = false;
wc.OpenReadAsync(new Uri(gatewayUrl));
}
}
//A **compatible** PhotoResult providing writable property
public class PhotoResultHacking : PhotoResult
{ public Stream CameraPhotoStream;
public PhotoResultHacking(TaskResult result, Stream stream) :
base(result)
{ CameraPhotoStream = stream;
}
}
}