【问题标题】:picture capture by using universal app windows phone 8.1 winrt使用通用应用程序 windows phone 8.1 winrt 捕获图片
【发布时间】:2015-04-27 11:41:49
【问题描述】:

我需要启动通用相机应用来拍照并将照片返回到我的应用。我不能使用照片选择器任务,因为它在 WinRT 上不受支持,并且我不想要媒体捕获。 有什么想法吗?

【问题讨论】:

    标签: windows windows-runtime windows-phone-8.1


    【解决方案1】:

    在 Windows 8 应用程序中,CameraCaptureTask 的等效项是 CameraCaptureUI。不幸的是,它不适用于 Windows Phone 8.1。因此,您唯一的选择是使用 MediaCapture。详情查看此帖:Photo capture on Windows Store App for Windows Phone

    您还可以使用一个辅助类作为替代方法:CameraCaptureUI for Windows Phone。但是它非常初级,缺乏自定义。

    【讨论】:

      【解决方案2】:

      好的,让我解释一下它应该如何实现:

      1) 创建类名为:CameraCapture:

      public class CameraCapture : IDisposable
      {
          MediaCapture mediaCapture;
          ImageEncodingProperties imgEncodingProperties;
          MediaEncodingProfile videoEncodingProperties;
      
          public VideoDeviceController VideoDeviceController
          {
              get { return mediaCapture.VideoDeviceController; }
          }
      
          public async Task<MediaCapture> Initialize(CaptureUse primaryUse = CaptureUse.Photo)
          {
              // Create MediaCapture and init
              mediaCapture = new MediaCapture();
              var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
              await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
              {
                  PhotoCaptureSource = PhotoCaptureSource.Photo,
                  AudioDeviceId = string.Empty,
                  VideoDeviceId = devices[1].Id
              });
              mediaCapture.VideoDeviceController.PrimaryUse = primaryUse;
      
              // Create photo encoding properties as JPEG and set the size that should be used for photo capturing
              imgEncodingProperties = ImageEncodingProperties.CreateJpeg();
              imgEncodingProperties.Width = 640;
              imgEncodingProperties.Height = 480;
      
              // Create video encoding profile as MP4 
              videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
              // Lots of properties for audio and video could be set here...
      
              return mediaCapture;
          }
      
          public async Task<StorageFile> CapturePhoto(string desiredName = "warranty.jpg")
          {
              // Create new unique file in the pictures library and capture photo into it
              var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);
      
      
              await mediaCapture.CapturePhotoToStorageFileAsync(imgEncodingProperties, photoStorageFile);
              return photoStorageFile;
          }
      
          public async Task<StorageFile> StartVideoRecording(string desiredName = "video.mp4")
          {
              // Create new unique file in the videos library and record video! 
              var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);
              await mediaCapture.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
              return videoStorageFile;
          }
      
          public async Task StopVideoRecording()
          {
              // Stop video recording
              await mediaCapture.StopRecordAsync();
          }
      
          public async Task StartPreview()
          {
              // Start Preview stream
              await mediaCapture.StartPreviewAsync();
          }
          public async Task StartPreview(IMediaExtension previewSink, double desiredPreviewArea)
          {
              // List of supported video preview formats to be used by the default preview format selector.
              var supportedVideoFormats = new List<string> { "nv12", "rgb32" };
      
              // Find the supported preview size that's closest to the desired size
              var availableMediaStreamProperties =
                  mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview)
                      .OfType<VideoEncodingProperties>()
                      .Where(p => p != null && !String.IsNullOrEmpty(p.Subtype) && supportedVideoFormats.Contains(p.Subtype.ToLower()))
                      .OrderBy(p => Math.Abs(p.Height * p.Width - desiredPreviewArea))
                      .ToList();
              var previewFormat = availableMediaStreamProperties.FirstOrDefault();
      
              // Start Preview stream
              await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewFormat);
              await mediaCapture.StartPreviewToCustomSinkAsync(new MediaEncodingProfile { Video = previewFormat }, previewSink);
          }
      
          public async Task StopPreview()
          {
              // Stop Preview stream
              await mediaCapture.StopPreviewAsync();
          }
      
      
      
          public void Dispose()
          {
              if (mediaCapture != null)
              {
                  mediaCapture.Dispose();
                  mediaCapture = null;
              }
          }
      }
      

      2) 将 MediaElement 添加到您的 xaml 页面代码:

      <Grid Background="#FF40B9F5">
          <Grid.RowDefinitions>
              <RowDefinition Height="543*"/>
              <RowDefinition Height="97*"/>
          </Grid.RowDefinitions>
          <CaptureElement x:Name="CapturePreview" Grid.Row="0"/>
          <Button x:Name="TakeWarrantyPhoto_Button" Content="TAKE PHOTO" HorizontalAlignment="Center" Grid.Row="1" VerticalAlignment="Center" Click="TakeWarrantyPhoto_Button_Click" BorderBrush="Black" Foreground="Black" FontFamily="Book Antiqua" FontWeight="Bold"/>
      </Grid>
      

      3) 页面C#代码:

      public sealed partial class AddNewWarrantyPhotoPage : Page
      {
      
          private CameraCapture cameraCapture;
      
          public AddNewWarrantyPhotoPage()
          {
              this.InitializeComponent();
              DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
          }
      
      
          /// <summary>
          /// Invoked when this page is about to be displayed in a Frame.
          /// </summary>
          /// <param name="e">Event data that describes how this page was reached.
          /// This parameter is typically used to configure the page.</param>
          protected async override void OnNavigatedTo(NavigationEventArgs e)
          {
              cameraCapture = new CameraCapture();
              CapturePreview.Source = await cameraCapture.Initialize();
              await cameraCapture.StartPreview();
          }
      
      
          protected override async void OnNavigatedFrom(NavigationEventArgs e)
          {
              // Release resources
              if (cameraCapture != null)
              {
                  await cameraCapture.StopPreview();
                  CapturePreview.Source = null;
                  cameraCapture.Dispose();
                  cameraCapture = null;
              }
          }
      
      
      
          private async void TakeWarrantyPhoto_Button_Click(object sender, RoutedEventArgs e)
          {
              var photoStorageFile = await cameraCapture.CapturePhoto();
              var bitmap = new BitmapImage();
              await bitmap.SetSourceAsync(await photoStorageFile.OpenReadAsync());
      
              //you can show it in your picture if you declare it in xaml:
             // WarrantyPhotoDisplay_Image.Source = bitmap;
      
          }
      
      }
      

      希望它会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多