2011年11月20日日曜日

アラームとカメラを使うアプリ

「2011年11月11日11時11分11秒を見逃すな」というアプリを公開しました。

http://windowsphone.com/s?appid=d8719221-93e1-4cc0-be2f-efcf5b85c727

指定した時間にアラームが鳴る機能と日付の焼きこんだ写真が撮れる機能を合わせたアプリです。

参考にしたページ

高橋 忍のブログ : アプリケーション自身のスクリーンキャプチャ機能の実装ブリリアントなWP7 : カメラで撮影した画像を保存する 画像ファイルを端末内に保存する
CH3COOH(酢酸)の実験室 : 静止画撮影を行う
です。特に高橋忍さんのページはコードをほぼコピーしている箇所があって、シャッターを押した後のメッセージ内容がまったく同じままにしてしまっていました。ごめんなさい。見る人が見れば、コピペしただけだろーと突っ込まれるかもしれないので、ここで白状しておきます

時計の表示を更新する部分も誰かの記述を参考にしましたが、どこだったか思い出せません




起動画面、アラームの設定と、カメラの撮影ページへの移行ができます

撮影した写真。画像サイズや撮影モードを切り替えたりとかはできません!ただシャッターボタンを押すだけです

時計部分のコードとXAMLをのっけておきます。
時計はタイマーで更新してます。文字の縁取りをするために数ピクセルずらした黒文字を4方向分の4回レンダリングしてます。(こんなんでいいのか?)
カメラの撮影は参考にしたコードをほぼそのままで、普通に撮影します。
撮影完了したら、写真の画像をlayoytrootの背景に描き出して、スクリーンキャプチャします

ほんとにこんなんで良いのか?って気もしますが、以上です。ここまでは数時間でできました。

一番時間がかかったのは、日時設定画面を作る事とアイコン作る事です。アイコンはちょっと画像がデカすぎたようです...


  1. public partial class ClockPage : PhoneApplicationPage  
  2.     {  
  3.         bool nowCapturering = false;  
  4.         PhotoCamera camera = null;  
  5.         public ClockPage()  
  6.         {  
  7.             InitializeComponent();  
  8.   
  9.   
  10.             CameraButtons.ShutterKeyPressed += new EventHandler(CameraButtons_ShutterKeyPressed);  
  11.             DispatcherTimer tmr = new DispatcherTimer();  
  12.             tmr.Interval = TimeSpan.FromSeconds(1);  
  13.             tmr.Tick += new EventHandler(tmr_Tick);  
  14.             tmr.Start();  
  15.         }  
  16.   
  17.         void camera_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)  
  18.         {  
  19.             System.Diagnostics.Debug.WriteLine("camera_CaptureThumbnailAvailable");  
  20.         }  
  21.   
  22.         void camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e)  
  23.         {  
  24.             System.Diagnostics.Debug.WriteLine("camera_CaptureImageAvailable");  
  25.             this.Dispatcher.BeginInvoke(delegate()  
  26.             {  
  27.                 BitmapImage bmp = new BitmapImage();  
  28.                 bmp.SetSource(e.ImageStream);  
  29.                 CaptureBrush.ImageSource = bmp;  
  30.                 TakeCapture();  
  31.                 nowCapturering = false;  
  32.             });  
  33.         }  
  34.   
  35.         void camera_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)  
  36.         {  
  37.             System.Diagnostics.Debug.WriteLine("camera_CaptureCompleted");  
  38.         }  
  39.   
  40.         void CameraButtons_ShutterKeyPressed(object sender, EventArgs e)  
  41.         {  
  42.             nowCapturering = true;  
  43.             camera.CaptureImage();  
  44.         }  
  45.   
  46.         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
  47.         {  
  48.             camera = new PhotoCamera(CameraType.Primary);  
  49.             //camera.CaptureThumbnailAvailable += new EventHandler<contentreadyeventargs&rt;(camera_CaptureThumbnailAvailable);  
  50.             camera.CaptureImageAvailable += new EventHandler<contentreadyeventargs&rt;(camera_CaptureImageAvailable);  
  51.             camera.CaptureCompleted += new EventHandler<cameraoperationcompletedeventargs&rt;(camera_CaptureCompleted);  
  52.             nowCapturering = false;  
  53.             PreviewBrush.SetSource(camera);  
  54.             base.OnNavigatedTo(e);  
  55.         }  
  56.   
  57.         protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)  
  58.         {  
  59.             if (camera != null)  
  60.             {  
  61.                 camera.Dispose();  
  62.                 camera.CaptureImageAvailable -= camera_CaptureImageAvailable;  
  63.                 camera.CaptureCompleted -= camera_CaptureCompleted;  
  64.                 camera = null;  
  65.             }  
  66.             base.OnNavigatingFrom(e);  
  67.         }  
  68.   
  69.         void tmr_Tick(object sender, EventArgs e)  
  70.         {  
  71.             if (!nowCapturering)  
  72.             {  
  73.                 textBlock1.Text = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");  
  74.             }  
  75.         }  
  76.   
  77.         private void TakeCapture()  
  78.         {  
  79.             WriteableBitmap bitmap = new WriteableBitmap(this.LayoutRoot, null);  
  80.             MemoryStream stream = new MemoryStream();  
  81.   
  82.             bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 80);  
  83.   
  84.             using (MediaLibrary medialib = new MediaLibrary())  
  85.             {  
  86.                 Picture pic = medialib.SavePictureToCameraRoll(DateTime.Now.ToString("yyMMddHHmmss"), stream.ToArray());  
  87.             }  
  88.               
  89.             MessageBox.Show("Saved");  
  90.         }  

xaml
  1. <phone:PhoneApplicationPage   
  2.     x:Class="Serial11.ClockPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  10.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  11.     Foreground="{StaticResource PhoneForegroundBrush}"  
  12.     SupportedOrientations="Landscape" Orientation="Landscape"  
  13.     mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="728"  
  14.     Language="ja-JP"  
  15.     shell:SystemTray.IsVisible="True">  
  16.   
  17.     <!--LayoutRoot は、すべてのページ コンテンツが配置されるルート グリッドです-->  
  18.     <Grid x:Name="LayoutRoot">  
  19.         <Grid.Background>  
  20.             <ImageBrush x:Name="CaptureBrush"/>  
  21.         </Grid.Background>  
  22.         <Grid x:Name="ContentPanel">  
  23.             <Rectangle x:Name="PreviewRectangle">  
  24.                 <Rectangle.Fill>  
  25.                     <VideoBrush x:Name="PreviewBrush"/>  
  26.                 </Rectangle.Fill>  
  27.             </Rectangle>  
  28.             <StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">  
  29.                 <Grid Margin="0,0,10,0">  
  30.                     <TextBlock x:Name="textBlock3" Text="{Binding Text, ElementName=textBlock1}" VerticalAlignment="Top" FontFamily="Segoe WP Black" FontSize="48" Margin="0" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" Foreground="Black">  
  31.                         <TextBlock.RenderTransform>  
  32.                             <CompositeTransform TranslateX="3" TranslateY="3"/>  
  33.                         </TextBlock.RenderTransform>  
  34.                     </TextBlock>  
  35.                     <TextBlock x:Name="textBlock4" Text="{Binding Text, ElementName=textBlock1}" VerticalAlignment="Top" FontFamily="Segoe WP Black" FontSize="48" Margin="0" Foreground="Black" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" >  
  36.                         <TextBlock.RenderTransform>  
  37.                             <CompositeTransform TranslateX="-3" TranslateY="3"/>  
  38.                         </TextBlock.RenderTransform>  
  39.                     </TextBlock>  
  40.                     <TextBlock x:Name="textBlock5" Text="{Binding Text, ElementName=textBlock1}" VerticalAlignment="Top" FontFamily="Segoe WP Black" FontSize="48" Margin="0" Foreground="Black" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" >  
  41.                         <TextBlock.RenderTransform>  
  42.                             <CompositeTransform TranslateX="-3" TranslateY="-3"/>  
  43.                         </TextBlock.RenderTransform>  
  44.                     </TextBlock>  
  45.                     <TextBlock x:Name="textBlock6" Text="{Binding Text, ElementName=textBlock1}" VerticalAlignment="Top" FontFamily="Segoe WP Black" FontSize="48" Margin="0" Foreground="Black" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" >  
  46.                         <TextBlock.RenderTransform>  
  47.                             <CompositeTransform TranslateX="3" TranslateY="-3"/>  
  48.                         </TextBlock.RenderTransform>  
  49.                     </TextBlock>  
  50.                     <TextBlock x:Name="textBlock1" Text="----/--/-- --:--:--" VerticalAlignment="Top" FontFamily="Segoe WP Black" FontSize="48" Margin="0" Foreground="Yellow" HorizontalAlignment="Right" />  
  51.                 </Grid>  
  52.             </StackPanel>  
  53.         </Grid>  
  54.     </Grid>  
  55.    
  56.     <!--ApplicationBar の使用法を示すサンプル コード-->  
  57.     <!--<phone:PhoneApplicationPage.ApplicationBar>  
  58.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">  
  59.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>  
  60.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>  
  61.             <shell:ApplicationBar.MenuItems>  
  62.                 <shell:ApplicationBarMenuItem Text="MenuItem 1"/>  
  63.                 <shell:ApplicationBarMenuItem Text="MenuItem 2"/>  
  64.             </shell:ApplicationBar.MenuItems>  
  65.         </shell:ApplicationBar>  
  66.     </phone:PhoneApplicationPage.ApplicationBar>-->  
  67.   
  68. </phone:PhoneApplicationPage>  

0 件のコメント: