最近在弄 Windows Phone 遇到有關於拍照擷取影像的問題..
參考了一些範例..
因為很多範例因為力求寫的完整,所以我整理一些我用得到的..
此篇目的在於在相機取得影像後,顯示在Rectangle 中,並且可控制對焦,且儲存至相簿中..
先看一下XAML Code :
<Grid x:Name="ContentPanel" Grid.Row="0" Margin="0,0,0,0">
<Rectangle Grid.Row="1"
x:Name="preview">
<Rectangle.Fill>
<VideoBrush x:Name="previewBrush">
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="previewTransform"
CenterX=".5"
CenterY=".5" />
</VideoBrush.RelativeTransform>
</VideoBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
<Button Content="對焦" Grid.Row="1" Height="72" HorizontalAlignment="Left" Margin="12,16,0,0" Name="btnFocus" VerticalAlignment="Top" Width="109" Click="btnFocus_Click" />
<Button Content="拍照儲存" Height="72" Margin="127,16,133,0" Name="btnTake" VerticalAlignment="Top" Grid.Row="1" Click="btnTake_Click" />
中間那一塊白色的就是Rectangle的部分,將會引入相機的影像..
其中一些說明我就寫在註解裡面
首先 宣告一個 PhotoCamera
private PhotoCamera _photoCamera;
記得要先
using Microsoft.Devices;
之後我們看一下初始化..
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
//CameraType.Primary 為主鏡頭
//CameraType.FrontFacing 為前鏡頭
_photoCamera = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
//初始化事件
_photoCamera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(_photoCamera_Initialized);
//當呼叫CaptureImage 擷取影像時觸發事件
_photoCamera.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(_photoCamera_CaptureImageAvailable);
previewBrush.SetSource(_photoCamera);
}
為什麼不寫在Ctor 裡面呢? 請參考此篇 [Windows Phone 7] Mango 密記–關於 OnNavigatedTo 跟 Constructor 一些不同..
初始化 _photoCamera_Initialized :
void _photoCamera_Initialized(object sender, CameraOperationCompletedEventArgs e)
{
if (e.Succeeded)
{
this.Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show("初始化成功 ");
});
}
Dispatcher.BeginInvoke(() =>
{
previewTransform.Rotation = _photoCamera.Orientation;
});
_photoCamera.FlashMode = FlashMode.Auto;
}
其中 previewTransform.Rotation = _photoCamera.Orientation;
主要目的是讓您的影像,不會是向右轉90的…
擷取影像後的觸發事件
void _photoCamera_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
MediaLibrary library = new MediaLibrary();
string fileName = Guid.NewGuid().ToString() + ".jpg";
try
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show("進行儲存");
});
//儲存至 相簿
library.SavePictureToCameraRoll(fileName, e.ImageStream);
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message);
}
}
MediaLibrary 要使用必須先
using Microsoft.Xna.Framework.Media;
之後就可以呼叫 CaptureImage() 進行拍照
private void btnTake_Click(object sender, RoutedEventArgs e)
{
//拍照
_photoCamera.CaptureImage();
}
因為相機支援對焦 所以 可以呼叫 Focus() 進行對焦..
不然都會糊糊的..
private void btnFocus_Click(object sender, RoutedEventArgs e)
{
//對焦
_photoCamera.Focus();
}
參考文件:
拍照:
http://msdn.microsoft.com/en-us/library/hh202956%28v=VS.92%29.aspx
Flash mode:
http://msdn.microsoft.com/zh-tw/library/hh202949(v=vs.92).aspx
CaptureImageAvailable Event:
http://msdn.microsoft.com/zh-tw/library/microsoft.devices.photocamera.captureimageavailable(v=vs.92).aspx
下面有範例就自行下載測試..