在Windows Phone 8.1上将照片保存到手机

时间:2014-12-01 00:06:25

标签: c# windows-phone-8.1

我正在尝试将照片保存到Windows手机中。照片将由相机拍摄,并且应该保存到实际手机上的相册中,以便我以后可以将它用于我应用的第2部分。

我可以让照片从一页到另一页,因为照片存储在手机的RAM中。但我必须将照片保存到手机中的相册中,否则我无法在我的应用程序外使用它(我错了吗?)

class CameraCapture : IDisposable
{
    MediaCapture mediaCapture;
    ImageEncodingProperties imgEncodingProperties;
    MediaEncodingProfile videoEncodingProperties;
    public VideoDeviceController VideoDeviceController
    {
        get { return mediaCapture.VideoDeviceController; }
    }

    public async Task<MediaCapture> Initialize(CaptureUse primaryUse = CaptureUse.Photo)
    {
        // Create MediaCapture and init
        mediaCapture = new MediaCapture();
        await mediaCapture.InitializeAsync();
        mediaCapture.VideoDeviceController.PrimaryUse = primaryUse;

        // Create photo encoding properties as JPEG and set the size that should be used for photo capturing
        imgEncodingProperties = ImageEncodingProperties.CreateJpeg();
        imgEncodingProperties.Width = 640;
        imgEncodingProperties.Height = 480;

        // Create video encoding profile as MP4 
        videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
        // Lots of properties for audio and video could be set here...

        return mediaCapture;
    }

    public async Task<InMemoryRandomAccessStream> CapturePhoto(string desiredName = "photo.jpg")
    {
        // Create new unique file in the pictures library and capture photo into it
        //var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);
        var stream = new InMemoryRandomAccessStream();
        await mediaCapture.CapturePhotoToStreamAsync(imgEncodingProperties, stream);
        return stream;
    }

    public async Task<StorageFile> StartVideoRecording(string desiredName = "video.mp4")
    {
        // Create new unique file in the videos library and record video! 
        var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);
        await mediaCapture.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
        return videoStorageFile;
    }

    public async Task StopVideoRecording()
    {
        // Stop video recording
        await mediaCapture.StopRecordAsync();
    }

    public async Task StartPreview()
    {
        // Start Preview stream
        await mediaCapture.StartPreviewAsync();
    }
    public async Task StartPreview(IMediaExtension previewSink, double desiredPreviewArea)
    {
        // List of supported video preview formats to be used by the default preview format selector.
        var supportedVideoFormats = new List<string> { "nv12", "rgb32" };

        // Find the supported preview size that's closest to the desired size
        var availableMediaStreamProperties =
            mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview)
                .OfType<VideoEncodingProperties>()
                .Where(p => p != null && !String.IsNullOrEmpty(p.Subtype) && supportedVideoFormats.Contains(p.Subtype.ToLower()))
                .OrderBy(p => Math.Abs(p.Height * p.Width - desiredPreviewArea))
                .ToList();
        var previewFormat = availableMediaStreamProperties.FirstOrDefault();

        // Start Preview stream
        await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewFormat);
        await mediaCapture.StartPreviewToCustomSinkAsync(new MediaEncodingProfile { Video = previewFormat }, previewSink);
    }

    public async Task StopPreview()
    {
        // Stop Preview stream
        await mediaCapture.StopPreviewAsync();
    }



    public void Dispose()
    {
        if (mediaCapture != null)
        {
            mediaCapture.Dispose();
            mediaCapture = null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解了核心,但如果您想将图像保存到文件中,那么您只需使用CapturePhotoToStorageFileAsync方法:

private async Task<IRandomAccessStreamWithContentType> TakePhotoBtn_Click(object sender, RoutedEventArgs e)
{
    // create a file in Camera Roll folder
    StorageFile photoFile = await KnownFolders.CameraRoll.CreateFileAsync("myPhoto.jpg", CreationCollisionOption.ReplaceExisting);
    // take a photo with choosen Encoding
    await captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoFile);
    return await photoFile.OpenReadAsync();
}

在上面的示例中,我返回 IRandomAccessStreamWithContentType ,因此我不知道您的应用需要什么,但您也可以返回 StorageFile Stream 或其他。

相关问题