2013-07-15 33 views
0

我創建了一個圖像編輯應用程序。我想給用戶一個選項,他可以在運行時捕捉圖像,並對捕捉的圖像應用編輯效果。目前我正在按照Advanced Photo Capture for windows Phone 8的說明進行操作。從PhotocaptureDevice捕獲照片

有兩個問題 首先,我面臨的是,當我點擊捕獲按鈕時,捕獲的圖像將是一個倒置的圖像。如果我將手機置於橫向模式,則會拍攝正確的照片。

當應用程序啓動時,屏幕上沒有任何內容,換句話說它是全黑的。我想讓屏幕顯示相機視圖,以便用戶知道他將要捕捉的內容。

下面是我對Mainpage.xaml.cs文件和MainPage.xaml文件的代碼。

namespace CapturingPhoto { 
public partial class MainPage : PhoneApplicationPage { 

    private MemoryStream imageStream; 
    private PhotoCaptureDevice captureDevice; 
    private CameraCaptureSequence seq; 
    // Constructor 
    public MainPage() { 
     InitializeComponent(); 
     imageStream = new MemoryStream(); 
     // Sample code to localize the ApplicationBar 
     //BuildLocalizedApplicationBar(); 

     prepareResouceToCapture(); 
    } 

    private async void prepareResouceToCapture() { 
     if((!PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back)) && 
      (!PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Front))){ 
       return; 
     } 
     Windows.Foundation.Size size; 
     if(PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back)){ 
      IReadOnlyList <Windows.Foundation.Size> avalaibleSizeList = 
       PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back); 
      size = avalaibleSizeList[0]; 
      this.captureDevice = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, size); 
     } 
     else{ 

      IReadOnlyList<Windows.Foundation.Size> avalaibleSizeList = 
       PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Front); 
      size = avalaibleSizeList[0]; 
      this.captureDevice = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Front, size); 

     } 


    // await this.captureDevice.SetCaptureResolutionAsync(size); 
     // await this.captureDevice.SetPreviewResolutionAsync(size); 
    // BackgroundVideoBrush.SetSource(this.captureDevice); 




    } 



    private async void onCaptureImage(object sender, RoutedEventArgs e) { 
     seq = captureDevice.CreateCaptureSequence(1); 
     captureDevice.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.On); 
     captureDevice.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, false); 
     captureDevice.SetProperty(KnownCameraGeneralProperties.AutoFocusRange, AutoFocusRange.Normal); 
     seq.Frames[0].CaptureStream = imageStream.AsOutputStream(); 
     await captureDevice.PrepareCaptureSequenceAsync(seq); 
     CaptureImage(); 


    } 


    public async void CaptureImage() { 
     await seq.StartCaptureAsync(); 

     // Set the stream position to the beginning. 
     imageStream.Seek(0, SeekOrigin.Begin); 

     MediaLibrary library = new MediaLibrary(); 
     Picture picture1 = library.SavePictureToCameraRoll("image1", imageStream); 

    } 


} 

}

代碼XAML文件

<phone:PhoneApplicationPage 
x:Class="CapturingPhoto.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" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
shell:SystemTray.IsVisible="True"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent" Height="768" VerticalAlignment="Bottom"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!-- LOCALIZATION NOTE: 
     To localize the displayed strings copy their values to appropriately named 
     keys in the app's neutral language resource file (AppResources.resx) then 
     replace the hard-coded text value between the attributes' quotation marks 
     with the binding clause whose path points to that string name. 

     For example: 

      Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" 

     This binding points to the template's string resource named "ApplicationTitle". 

     Adding supported languages in the Project Properties tab will create a 
     new resx file per language that can carry the translated values of your 
     UI strings. The binding in these examples will cause the value of the 
     attributes to be drawn from the .resx file that matches the 
     CurrentUICulture of the app at run time. 
    --> 



    <!--ContentPanel - place additional content here--> 

    <Canvas x:Name="VideoCanvas" RenderTransformOrigin="0.5,0.5" Canvas.ZIndex="0"> 
     <Canvas.RenderTransform> 
      <CompositeTransform/> 
     </Canvas.RenderTransform> 
     <Canvas.Background> 
      <!-- The background contains the camera view finder 
       encapsulated in VideoBrush. --> 
      <VideoBrush x:Name="BackgroundVideoBrush" > 
       <VideoBrush.RelativeTransform> 
        <CompositeTransform x:Name="VideoBrushTransform" CenterY="0.5" CenterX="0.5"/> 
       </VideoBrush.RelativeTransform> 
      </VideoBrush> 
     </Canvas.Background> 
    </Canvas> 

    <Button x:Name="btn_Edit" Content="Capture" HorizontalAlignment="Left" 
       Margin="23,691,0,0" VerticalAlignment="Top" 
       Width="400" Click="onCaptureImage"/> 



    <!--Uncomment to see an alignment grid to help ensure your controls are 
     aligned on common boundaries. The image has a top margin of -32px to 
     account for the System Tray. Set this to 0 (or remove the margin altogether) 
     if the System Tray is hidden. 

     Before shipping remove this XAML and the image itself.--> 
    <!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />--> 
</Grid> 

回答

1

我看到字符串:

BackgroundVideoBrush.SetSource(this.captureDevice); 

是評論。 如果VideoBrush爲空,則無法看到相機視圖。

0

要回答你的第一個問題,請使用「EncodeWithOrientation」屬性。將下面的switch語句放在onCaptureImage()的頂部。這將確保在縱向模式下拍攝照片時保存的圖像處於正確的方向。

 // Initialize variables. 
     int encodedOrientation = 0; 
     int sensorOrientation = (Int32)this._camera.SensorRotationInDegrees; 

     switch (this.Orientation) 
     { 
      // Camera hardware shutter button up. 
      case PageOrientation.LandscapeLeft: 
       encodedOrientation = -90 + sensorOrientation; 
       break; 
      // Camera hardware shutter button down. 
      case PageOrientation.LandscapeRight: 
       encodedOrientation = 90 + sensorOrientation; 
       break; 
      // Camera hardware shutter button right. 
      case PageOrientation.PortraitUp: 
       encodedOrientation = 0 + sensorOrientation; 
       break; 
      // Camera hardware shutter button left. 
      case PageOrientation.PortraitDown: 
       encodedOrientation = 180 + sensorOrientation; 
       break; 
     } 
     // Apply orientation to image encoding. 
     this._camera.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, encodedOrientation); 

另請參見MSDN上的以下內容:http://msdn.microsoft.com/EN-US/library/windowsphone/develop/windows.phone.media.capture.knowncamerageneralproperties.encodewithorientation(v=vs.105).aspx