windows 10个手机摄像头

windows 10 mobile camera

我正在尝试在 Windows 10 移动应用程序中使用相机,但是当我拍照并尝试在屏幕上显示时出现错误。

代码如下:

CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);

StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (photo == null)
{
    // User cancelled photo capture
    return;
}

StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);

await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();

IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied);

SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

imageControl.Source = bitmapSource;

异常信息:

An exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll but was not handled in user code

Additional information: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

If there is a handler for this exception, the program may be safely continued."

有人可以帮我解决这个问题吗?

这是因为你删除了photo,然后试图读取刚刚删除的photo,所以会抛出异常"FileNotFound"。请删除以下代码行,它将起作用。

 await photo.DeleteAsync();

但我认为你真正想做的是删除从CameraCaptureUI中获取的照片,然后从已经复制的本地文件夹中读取照片。在这种情况下,代码应如下所示:

await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
StorageFile newphoto = await destinationFolder.GetFileAsync("ProfilePhoto.jpg");
IRandomAccessStream stream = await newphoto.OpenAsync(FileAccessMode.Read);