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

本文关键字:照片 保存 手机 Windows Phone | 更新日期: 2023-09-27 18:32:45

我正在尝试将照片保存到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;
        }
    }
}

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

我不确定我是否完全理解,但是如果您想将图像保存到文件中,那么您可以使用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或其他。