简体   繁体   English

尝试使用后置摄像头,然后使用前置摄像头C#MediaCapture Windows Phone 8.1

[英]Trying to use back camera followed by front camera c# mediacapture windows phone 8.1

I am writing a program where I want to take pic from back camera and then front camera using a single button. 我正在编写一个程序,我想使用一个按钮从后置摄像头然后从前置摄像头拍摄照片。 First it takes pic using back camera with no issue. 首先,使用后置摄像头拍摄照片毫无问题。 but I try takin pic using front camera, it gives me exception saying like "No error text found associated with this error code" at await newFrontCapture.StartPreviewAsync(); 但是我尝试使用前置摄像头拍摄takin pic,它在等待newFrontCapture.StartPreviewAsync()时给了我异常提示,例如“未找到与此错误代码关联的错误文本”; line where newFrontCapture is the object of MediaCapture. 这行,其中newFrontCapture是MediaCapture的对象。 Following is the code I am trying: 以下是我正在尝试的代码:

//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;
            }

Below code is working. 下面的代码正在工作。 it captures both font and back pictures in one click. 一键捕获字体和背面图片。

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;
        }
    }
}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MediaCapture Windows Phone 8.1 RT上更改Camera的默认设置? - How to change the default settings of Camera on MediaCapture Windows Phone 8.1 RT? 如何使用现有的相机应用程序(不是使用 MediaCapture 创建一个)并在 windows phone 8.1(WinRT) 中拍照? - How to use existing camera app(not create one using MediaCapture) and take picture in windows phone 8.1(WinRT)? Windows(Phone)8.1相机使用 - Windows (Phone) 8.1 Camera Use Windows Phone 8.1 Javascript应用程序-使用相机 - Windows Phone 8.1 Javascript Application - Use Camera Windows Phone 8.1:如何更改前置摄像头的旋转角度? - Windows Phone 8.1: how to change front camera rotation? 在Windows 8.1商店应用程序中找不到Front,Back Camera - Cannot find Front, Back Camera in windows 8.1 store app Windows Phone 8.1摄像头初始化 - Windows Phone 8.1 camera initialization 在Windows Phone中同时显示来自前置和后置摄像头的视频 - Showing video from front and back camera at the same time in Windows Phone 从现有的相机应用程序Windows Phone 8.1运行时C中获取图片 - get Picture from exising camera app Windows Phone 8.1 runtime c# 如何使用C#在Windows Phone中使用前置摄像头捕获视频 - How to capture video using front camera in windows phone using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM