[Windows Phone7] CameraCaptureTask 拍照取得照片

2010/08/23

這幾天小玩一下Windows Phone 7 SDK ..
想說玩一下他的拍照..
很酷..模擬器裡面也有模擬這功能…不錯不錯..

簡單介紹一下如何使用Windows Phone 7  去啟動拍照,並且將拍照後的照片取回..

簡單介紹一下版面..
sshot-49


有一個物件叫做imgPhoto 就是負責來裝拍完照之後的照片..
至於btnStart就是啟動拍照的Button 物件..
XMAL Code 如下:

<phone:PhoneApplicationPage 
x:Class="TakePicMemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
shell:SystemTray.IsVisible="True">
 
<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
 
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle" Text="當麻許測試" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="TEST APP" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
 
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentGrid" Grid.Row="1">
<Image Height="397" HorizontalAlignment="Left" Margin="21,30,0,0" Name="imgPhoto" Stretch="Fill" VerticalAlignment="Top" Width="430" />
<Button Content="啟動拍照" Height="72" HorizontalAlignment="Left" Margin="291,457,0,0" Name="btnStart" VerticalAlignment="Top" Width="160" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>

接下來 我們在C# Code 中..
首先因為你要啟動拍照,並且有些許使用到圖形
記得要先
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
接下來我們在按鈕的Click 事件加入..
void btnStart_Click(object sender, System.Windows.RoutedEventArgs e)
{
CameraCaptureTask cct = new CameraCaptureTask();
cct.Completed += cct_Completed;
cct.Show();
}


CameraCaptureTask Completed 後的事件:
void cct_Completed(object sender, PhotoResult e)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
this.imgPhoto.Source = bitmap;
}

建立一個 CameraCaptureTask 物件並且寫入完成的事件..
之後啟動Show();  在Completed 事件中把e.ChosenPhot 填入到 imgPhoto 中..
這樣就大功告成…
來執行看看
sshot-52

是成功啟動拍照了…
但是…………….

拍完的圖片呢…?!

他沒有填入,imgPhoto 一然是空曠一片..
sshot-491

原本還以為是很溫馨的可以直接寫出來… (泣

好吧..改一點做法..

using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Controls;
 
 
 
namespace TakePicMemo
{
public partial class MainPage : PhoneApplicationPage
{
//搬到function 外面
private CameraCaptureTask cct = new CameraCaptureTask();
 
// Constructor
public MainPage()
{
InitializeComponent();
btnStart.Click += btnStart_Click;
 
//將事件在Constructor中加入
cct.Completed += cct_Completed;
}
 
 
void btnStart_Click(object sender, System.Windows.RoutedEventArgs e)
{
cct.Show();
}
 
void cct_Completed(object sender, PhotoResult e)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
this.imgPhoto.Source = bitmap;
}
}
}

我將 CameraCaptureTask cct = new CameraCaptureTask(); 搬到function 之外..
並且將 cct.Completed  事件加入在 Constructor 內 ..
結果:

sshot-53

成功了..打完收工…

ps.
如果你啟動拍照後發現你不知道要按那裡..
因為我畫面會一直閃動我根本不知道要按哪邊,我瞎按按到才成功的..
不過有可能是因為我家境貧困只買得起CULV電腦當作工作筆電(淚~
所以搞不好只有我電腦才會這樣,如果您也有遇到可以紅色標示處
應該就會拍攝成功…
sshot-51

如果您對建立Windows Phone 7 專案有問題..
可以參考一下保哥 這篇 - Windows Phone 7 快速上手指南 ( 含學習資源整理 )

順帶一提..
這邊是 MSDN 寫到 CameraCaptureTask  的文件..
裡面依然有一句很有禪意的話..
[Note: This topic contains pre-release information and is subject to change in future releases. Blank topics are included as placeholders. References to "Beta" refer to the current version of Windows Phone Developer Tools and references to "Technical Preview" refer to the current version of Windows Phone OS 7.0 and Windows Phone 7.]

讚一下:

範例下載:


0 意見:

程式 . 生活 . D小調.@2010 | Binary Design: One Winged Angel.