Ejemplo n.º 1
0
    /// <summary>
    /// Take a photo async with specified options
    /// </summary>
    /// <param name="options">Camera Media Options</param>
    /// <returns>Media file of photo or null if canceled</returns>
    public async Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options)
    {
      if (!IsCameraAvailable)
        throw new NotSupportedException();

      options.VerifyOptions();

      var capture = new CameraCaptureUI();
      var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo, options);
      if (result == null)
        return null;

      StorageFolder folder = ApplicationData.Current.LocalFolder;

      string path = options.GetFilePath(folder.Path);
      var directoryFull = Path.GetDirectoryName(path);
      var newFolder = directoryFull.Replace(folder.Path, string.Empty);
      if (!string.IsNullOrWhiteSpace(newFolder))
        await folder.CreateFolderAsync(newFolder, CreationCollisionOption.OpenIfExists);

      folder = await StorageFolder.GetFolderFromPathAsync(directoryFull);

      string filename = Path.GetFileName(path);

      var file = await result.CopyAsync(folder, filename, NameCollisionOption.GenerateUniqueName).AsTask();
      return new MediaFile(file.Path, () => file.OpenStreamForReadAsync().Result);
    }
    /// <summary>
    /// Take a photo async with specified options
    /// </summary>
    /// <param name="options">Camera Media Options</param>
    /// <returns>Media file of photo or null if canceled</returns>
    public Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options)
    {
      if (!IsCameraAvailable)
        throw new NotSupportedException();

      options.VerifyOptions();

      var ntcs = new TaskCompletionSource<MediaFile>(options);
      if (Interlocked.CompareExchange(ref completionSource, ntcs, null) != null)
        throw new InvalidOperationException("Only one operation can be active at a time");

      this.cameraCapture.Show();

      return ntcs.Task;
    }