尝试使用后置摄像头,然后使用前置摄像头 c# mediacapture windows phone 8.1
Trying to use back camera followed by front camera c# mediacapture windows phone 8.1
我正在编写一个程序,我想使用一个按钮从后置摄像头拍照,然后从前置摄像头拍照。
首先,它使用后置摄像头拍摄照片没有问题。但我尝试使用前置摄像头拍摄羚羊照片,它给了我异常说像 "No error text found associated with this error code" at await newFrontCapture.StartPreviewAsync();行,其中 newFrontCapture 是 MediaCapture 的对象。
以下是我正在尝试的代码:
//code to take back camera image
webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
MediaCapture newCapture = null;
DeviceInformationCollection webcamList;
const string filename = "mysetting.txt";
StorageFolder sf = null;
DeviceInformation backWebcam;
try
{
if (newCapture!= null)
newCapture.Dispose();
newCapture = new MediaCapture();
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = backWebcam.Id
});
cp.Source = newCapture;
// Start the preview
await newCapture.StartPreviewAsync();
}
catch (Exception ex)
{
newCapture.Dispose();
}
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picPath = "Image_Test_" + Convert.ToString(new Random());
StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);
BitmapImage bitmapToShow = new BitmapImage(new Uri(captureFile.Path));
imagePreivew.Source = bitmapToShow; // show image on screen inside Image
captureFile = null;
await newCapture.StopPreviewAsync();
newCapture.Dispose();
Frame.Navigate(typeof(FrontImagePage),imagePreivew);
}
catch (Exception ex)
{
printvlaue.Text = ex.Message;
await newCapture.StopPreviewAsync();
newCapture.Dispose();// disposing the object of mediacapture (back camera object)
}
// Code to take front camera pic
try
{
webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
frontWebCam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
newFrontCapture = new MediaCapture();
await newFrontCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = frontWebCam.Id,
PhotoCaptureSource = PhotoCaptureSource.Photo,
StreamingCaptureMode=StreamingCaptureMode.Video
});
//await newFrontCapture.InitializeAsync(new MediaCaptureInitializationSettings()
//{
// VideoDeviceId = frontWebCam.Id,
// PhotoCaptureSource = PhotoCaptureSource.Photo
//});
await newFrontCapture.StartPreviewAsync();
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picFront = "Image_Test_Front" + Convert.ToString(new Random());
StorageFile captureFrontFile = await folder.CreateFileAsync(picFront, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageFrontProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newFrontCapture.CapturePhotoToStorageFileAsync(imageFrontProperties, captureFrontFile);
BitmapImage bitmapToShowFront = new BitmapImage(new Uri(captureFrontFile.Path));
imageFront.Source = bitmapToShowFront;
newFrontCapture.Dispose();
newFrontCapture = null;
imageBack.Source = this.im_.Source;
}
catch (Exception ex)
{
await newFrontCapture.StopPreviewAsync();
newFrontCapture.Dispose();
//throw;
}
以下代码有效。一键捕获字体和背面图片。
private async Task CaptureBackAndFront()
{
//front Camera capture...
DeviceInformationCollection webcamList;
webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation backWebcam;
backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
MediaCapture newCapture = new MediaCapture();
StorageFolder folder = ApplicationData.Current.LocalFolder;
try
{
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = backWebcam.Id
});
cp.Source = newCapture;
await newCapture.StartPreviewAsync();
var picPath = "Image_Test_" + Convert.ToString(new Random());
StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);
BitmapImage bitmapToShow = new BitmapImage(new Uri(captureFile.Path));
imagePreivew.Source = bitmapToShow; // show image on screen inside Image
captureFile = null;
}
catch (Exception ex)
{
//handel error situation...
}
finally
{
await newCapture.StopPreviewAsync();
newCapture.Dispose();
}
// Code to take front camera pic
MediaCapture newFrontCapture = new MediaCapture();
try
{
var frontWebCam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
await newFrontCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = frontWebCam.Id,
});
cp.Source = newFrontCapture;
await newFrontCapture.StartPreviewAsync();
var picFront = "Image_Test_Front" + Convert.ToString(new Random());
StorageFile captureFrontFile = await folder.CreateFileAsync(picFront, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageFrontProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newFrontCapture.CapturePhotoToStorageFileAsync(imageFrontProperties, captureFrontFile);
BitmapImage bitmapToShowFront = new BitmapImage(new Uri(captureFrontFile.Path));
imagePreivew1.Source = bitmapToShowFront;
}
catch (Exception ex)
{
// Hanel error situation...
}
finally
{
await newFrontCapture.StopPreviewAsync();
newFrontCapture.Dispose();
newFrontCapture = null;
}
}
}
希望对您有所帮助
我正在编写一个程序,我想使用一个按钮从后置摄像头拍照,然后从前置摄像头拍照。 首先,它使用后置摄像头拍摄照片没有问题。但我尝试使用前置摄像头拍摄羚羊照片,它给了我异常说像 "No error text found associated with this error code" at await newFrontCapture.StartPreviewAsync();行,其中 newFrontCapture 是 MediaCapture 的对象。 以下是我正在尝试的代码:
//code to take back camera image
webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
MediaCapture newCapture = null;
DeviceInformationCollection webcamList;
const string filename = "mysetting.txt";
StorageFolder sf = null;
DeviceInformation backWebcam;
try
{
if (newCapture!= null)
newCapture.Dispose();
newCapture = new MediaCapture();
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = backWebcam.Id
});
cp.Source = newCapture;
// Start the preview
await newCapture.StartPreviewAsync();
}
catch (Exception ex)
{
newCapture.Dispose();
}
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picPath = "Image_Test_" + Convert.ToString(new Random());
StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);
BitmapImage bitmapToShow = new BitmapImage(new Uri(captureFile.Path));
imagePreivew.Source = bitmapToShow; // show image on screen inside Image
captureFile = null;
await newCapture.StopPreviewAsync();
newCapture.Dispose();
Frame.Navigate(typeof(FrontImagePage),imagePreivew);
}
catch (Exception ex)
{
printvlaue.Text = ex.Message;
await newCapture.StopPreviewAsync();
newCapture.Dispose();// disposing the object of mediacapture (back camera object)
}
// Code to take front camera pic
try
{
webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
frontWebCam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
newFrontCapture = new MediaCapture();
await newFrontCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = frontWebCam.Id,
PhotoCaptureSource = PhotoCaptureSource.Photo,
StreamingCaptureMode=StreamingCaptureMode.Video
});
//await newFrontCapture.InitializeAsync(new MediaCaptureInitializationSettings()
//{
// VideoDeviceId = frontWebCam.Id,
// PhotoCaptureSource = PhotoCaptureSource.Photo
//});
await newFrontCapture.StartPreviewAsync();
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picFront = "Image_Test_Front" + Convert.ToString(new Random());
StorageFile captureFrontFile = await folder.CreateFileAsync(picFront, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageFrontProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newFrontCapture.CapturePhotoToStorageFileAsync(imageFrontProperties, captureFrontFile);
BitmapImage bitmapToShowFront = new BitmapImage(new Uri(captureFrontFile.Path));
imageFront.Source = bitmapToShowFront;
newFrontCapture.Dispose();
newFrontCapture = null;
imageBack.Source = this.im_.Source;
}
catch (Exception ex)
{
await newFrontCapture.StopPreviewAsync();
newFrontCapture.Dispose();
//throw;
}
以下代码有效。一键捕获字体和背面图片。
private async Task CaptureBackAndFront()
{
//front Camera capture...
DeviceInformationCollection webcamList;
webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation backWebcam;
backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
MediaCapture newCapture = new MediaCapture();
StorageFolder folder = ApplicationData.Current.LocalFolder;
try
{
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = backWebcam.Id
});
cp.Source = newCapture;
await newCapture.StartPreviewAsync();
var picPath = "Image_Test_" + Convert.ToString(new Random());
StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);
BitmapImage bitmapToShow = new BitmapImage(new Uri(captureFile.Path));
imagePreivew.Source = bitmapToShow; // show image on screen inside Image
captureFile = null;
}
catch (Exception ex)
{
//handel error situation...
}
finally
{
await newCapture.StopPreviewAsync();
newCapture.Dispose();
}
// Code to take front camera pic
MediaCapture newFrontCapture = new MediaCapture();
try
{
var frontWebCam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
await newFrontCapture.InitializeAsync(new MediaCaptureInitializationSettings()
{
VideoDeviceId = frontWebCam.Id,
});
cp.Source = newFrontCapture;
await newFrontCapture.StartPreviewAsync();
var picFront = "Image_Test_Front" + Convert.ToString(new Random());
StorageFile captureFrontFile = await folder.CreateFileAsync(picFront, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageFrontProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newFrontCapture.CapturePhotoToStorageFileAsync(imageFrontProperties, captureFrontFile);
BitmapImage bitmapToShowFront = new BitmapImage(new Uri(captureFrontFile.Path));
imagePreivew1.Source = bitmapToShowFront;
}
catch (Exception ex)
{
// Hanel error situation...
}
finally
{
await newFrontCapture.StopPreviewAsync();
newFrontCapture.Dispose();
newFrontCapture = null;
}
}
}
希望对您有所帮助