如何使用C#在Windows Phone 8.1上捕获图像?

时间:2018-12-08 16:43:12

标签: c# windows-phone-8.1

我正在尝试在Windows Phone 8.1上捕获图像。 我正在使用以下代码。 它在模拟器上完美运行 但是,使用设备时,没有图像。 如果将图像保存在cameraroll文件夹中,则会得到70KB的图像。我不明白怎么了... 请帮忙!

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

0 个答案:

没有答案