2015-05-18 44 views
1

我很難讓捕獲的圖像正確旋轉,bitmapencoder類似乎沒有做任何事情。Windows Phone 8.1媒體捕獲旋轉不正確

private async Task CapturePhoto() 
    { 
     ImageEncodingProperties pIEPEncoder = ImageEncodingProperties.CreateJpeg(); 
     InMemoryRandomAccessStream pIMSCapture = new InMemoryRandomAccessStream(); 
     using (pIMSCapture) 
     { 
      await cMCeCapture.CapturePhotoToStreamAsync(pIEPEncoder, pIMSCapture); 

      pIMSCapture.Seek(0); 

      BitmapDecoder pBDrDecoder = await BitmapDecoder.CreateAsync(pIMSCapture); 
      BitmapEncoder pBErEncoder = await BitmapEncoder.CreateForTranscodingAsync(pIMSCapture, pBDrDecoder); 
      pBErEncoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; 
      pBErEncoder.BitmapTransform.Rotation = ConvertDegreesToBitmapRotation(cIntRotationDegrees); 
      await pBErEncoder.FlushAsync(); 

      BitmapImage pBIeCapture = new BitmapImage(); 
      pIMSCapture.Seek(0); 
      await pBIeCapture.SetSourceAsync(pIMSCapture); 

      CaptureImage.Source = pBIeCapture; 
      CaptureImage.Stretch = Stretch.Uniform; 
      CaptureImage.Width = LayoutRoot.ActualWidth; 
      CaptureImage.Height = LayoutRoot.ActualHeight; 
     } 

     await CleanupCameraAsync(); 
    } 

任何想法如何解決這個問題?相機應用程序接口似乎是我的遲到,因此我很沮喪地得到任何我想要的東西。

+0

只是FYI,我可以設置pBErEncoder.BitmapTransform.Rotation值爲任何東西,它總是看起來一樣。順時針旋轉90度。 – Nick

+0

好吧我現在有更多的補充,旋轉和翻轉似乎在Windows 8.1桌面上完全不同。如果我在轉碼之前將流設置爲0,旋轉將不起作用,翻轉會損壞圖像。 如果我在轉碼之前不尋求0,則旋轉將工作,但翻轉會產生「未找到組件」異常。我會做一個小項目來證明這個問題。在目前看來,我不可能使用運行Windows 8.1的Surface Pro 3上的位圖編碼器翻轉圖像,它會破壞或拋出異常。 – Nick

回答

0

我已經找到一個替代的解決方案,這就是使用從這裏獲得的WritableBitmapEx類,

https://writeablebitmapex.codeplex.com/

這實際上做什麼它應該做的,不像某些原因的BitmapEncoder類。另外它非常易於使用。雖然翻轉水平似乎翻轉垂直,而不是完全確定爲什麼,但無論如何它的作品。我只是將水平和垂直切換過來。

private async Task CapturePhoto() 
    { 
     ImageEncodingProperties pIEPEncoder = ImageEncodingProperties.CreateJpeg(); 
     InMemoryRandomAccessStream pIMSCapture = new InMemoryRandomAccessStream(); 
     using (pIMSCapture) 
     { 
      await cMCeCapture.CapturePhotoToStreamAsync(pIEPEncoder, pIMSCapture); 

      if(!cBlnExternalCamera) 
      { 
       pIMSCapture.Seek(0); 
       BitmapImage pBIeCapture = new BitmapImage(); 
       await pBIeCapture.SetSourceAsync(pIMSCapture); 

       pIMSCapture.Seek(0); 
       WriteableBitmap pWBpBitmap = new WriteableBitmap(pBIeCapture.PixelWidth, pBIeCapture.PixelHeight); 
       await pWBpBitmap.SetSourceAsync(pIMSCapture); 

       if (cIntRotationDegrees != 0) 
        pWBpBitmap = pWBpBitmap.Rotate(cIntRotationDegrees); 

       if (cBlnMirroringPreview) 
        pWBpBitmap = pWBpBitmap.Flip(cDInDisplayInformation.CurrentOrientation == DisplayOrientations.Landscape || cDInDisplayInformation.CurrentOrientation == DisplayOrientations.LandscapeFlipped ? WriteableBitmapExtensions.FlipMode.Vertical : WriteableBitmapExtensions.FlipMode.Horizontal); 

       CaptureImage.Source = pWBpBitmap; 
       CaptureImage.Stretch = Stretch.Uniform; 
       CaptureImage.Width = LayoutRoot.ActualWidth; 
       CaptureImage.Height = LayoutRoot.ActualHeight; 
      } 
      else 
      { 
       pIMSCapture.Seek(0); 
       BitmapImage pBIeCapture = new BitmapImage(); 
       await pBIeCapture.SetSourceAsync(pIMSCapture); 

       CaptureImage.Source = pBIeCapture; 
       CaptureImage.Stretch = Stretch.Uniform; 
       CaptureImage.Width = LayoutRoot.ActualWidth; 
       CaptureImage.Height = LayoutRoot.ActualHeight; 
      } 
     } 

     await CleanupCameraAsync(); 
    }