Как захватить изображение на Windows Phone 8.1 с C #? - PullRequest
0 голосов
/ 08 декабря 2018

Я пытаюсь захватить изображение на Windows Phone 8.1.Я использую следующий код.Он отлично работает на эмуляторе. Однако при использовании устройства изображение отсутствует.Если я сохраню изображение в папке Cameraroll, я получу изображение размером 70 Кбайт.Я не понимаю, в чем дело ... Пожалуйста, помогите!

    using System;
    using System.Diagnostics;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    using Windows.Media.Capture;
    using Windows.Media.MediaProperties;
    using Windows.Storage.Streams;
    using Windows.UI.Xaml.Media.Imaging;
    using Windows.Storage;

    private async void Capture2()
    {
        var captureManager = new MediaCapture();

        // Find the camera device id to use
        string deviceId = "";
        var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
        for (var i = 0; i < devices.Count; i++)
        {
            Debug.WriteLine(devices[i]);
            deviceId = devices[i].Id;
        }

        // init the settings of the capture
        var settings = new MediaCaptureInitializationSettings();
        settings.AudioDeviceId = "";
        settings.VideoDeviceId = deviceId;
        settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Photo;
        settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
        await captureManager.InitializeAsync(settings);

        // Find the highest resolution available
        VideoEncodingProperties resolutionMax = null;
        int max = 0;
        var resolutions = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);
        for (var i = 0; i < resolutions.Count; i++)
        {
            if (resolutions[i].GetType() == typeof(VideoEncodingProperties))
            {
                VideoEncodingProperties res = (VideoEncodingProperties)resolutions[i];
                if (res.Width * res.Height > max)
                {
                    max = (int)(res.Width * res.Height);
                    resolutionMax = res;
                    Debug.WriteLine("resolution max : " + res.Width + "x" + res.Height);
                }
            }
        }
        await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutionMax);


        //declare image format
        ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg();

        //generate file in local folder
        //StorageFolder folder = KnownFolders.CameraRoll;
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        StorageFile capturefile = await folder.CreateFileAsync("photo_" + DateTime.Now.Ticks.ToString(), CreationCollisionOption.ReplaceExisting);

        ////take & save photo
        await captureManager.CapturePhotoToStorageFileAsync(format, capturefile);
        Debug.WriteLine("BitmapImage Height : " + capturefile.Path);

        //show captured photo
        BitmapImage img = new BitmapImage(new Uri(capturefile.Path));
        ImageCapture.Source = img;
        ImageCapture.Visibility = Visibility.Visible;
    }
...