使用通用应用程序windows phone 8.1 winrt捕获图片
我需要启动通用相机应用程序来拍照并将照片返回到我的应用程序。我无法使用照片选择器任务,因为WinRT不支持该任务,并且我不希望捕获媒体。使用通用应用程序windows phone 8.1 winrt捕获图片,windows,windows-runtime,windows-phone-8.1,Windows,Windows Runtime,Windows Phone 8.1,我需要启动通用相机应用程序来拍照并将照片返回到我的应用程序。我无法使用照片选择器任务,因为WinRT不支持该任务,并且我不希望捕获媒体。 有什么想法吗?在Windows 8应用程序中,与CameraCaptureTask等效的是CameraCaptureUI。不幸的是,它不适用于Windows Phone 8.1。因此,您唯一的选择是使用MediaCapture。查看此帖子了解详细信息: 还有一个助手类,您可以将其用作替代对象:。但是它非常简单,缺乏定制。好的,让我解释一下应该如何实现它: 1)
有什么想法吗?在Windows 8应用程序中,与CameraCaptureTask等效的是CameraCaptureUI。不幸的是,它不适用于Windows Phone 8.1。因此,您唯一的选择是使用MediaCapture。查看此帖子了解详细信息:
还有一个助手类,您可以将其用作替代对象:。但是它非常简单,缺乏定制。好的,让我解释一下应该如何实现它: 1) 创建名为CameraCapture的类:
public 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();
var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = devices[1].Id
});
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<StorageFile> CapturePhoto(string desiredName = "warranty.jpg")
{
// Create new unique file in the pictures library and capture photo into it
var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);
await mediaCapture.CapturePhotoToStorageFileAsync(imgEncodingProperties, photoStorageFile);
return photoStorageFile;
}
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;
}
}
}
公共类摄像机捕捉:IDisposable
{
媒体捕获媒体捕获;
图像编码属性imgEncodingProperties;
MediaEncodingProfile视频编码属性;
公共视频设备控制器视频设备控制器
{
获取{return mediaCapture.VideoDeviceController;}
}
公共异步任务初始化(CaptureUse primaryUse=CaptureUse.Photo)
{
//创建MediaCapture和init
mediaCapture=新的mediaCapture();
var设备=等待设备信息.FindAllAsync(DeviceClass.VideoCapture);
等待mediaCapture.InitializeAsync(新的MediaCaptureInitializationSettings
{
PhotoCaptureSource=PhotoCaptureSource.Photo,
AudioDeviceId=string.Empty,
VideoDeviceId=设备[1]。Id
});
mediaCapture.VideoDeviceController.PrimaryUse=PrimaryUse;
//以JPEG格式创建照片编码属性,并设置用于照片捕获的大小
imgEncodingProperties=ImageEncodingProperties.CreateJpeg();
imgEncodingProperties.Width=640;
imgEncodingProperties.Height=480;
//将视频编码配置文件创建为MP4
videoEncodingProperties=MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
//许多音频和视频的属性可以在这里设置。。。
返回媒体捕获;
}
公共异步任务CapturePhoto(字符串desiredName=“warranty.jpg”)
{
//在图片库中创建新的唯一文件并将照片捕获到其中
var photoStorageFile=await KnownFolders.PicturesLibrary.CreateFileAsync(desiredName,CreationCollisionOption.GenerateUniqueName);
等待mediaCapture.CapturePhotoToStorageFileAsync(imgEncodingProperties,photoStorageFile);
返回photoStorageFile;
}
公共异步任务StartVideoRecording(字符串desiredName=“video.mp4”)
{
//在视频库中创建新的唯一文件并录制视频!
var videoStorageFile=await KnownFolders.VideosLibrary.CreateFileAsync(desiredName,CreationCollisionOption.GenerateUniqueName);
等待mediaCapture.StartRecordToStorageFileAsync(视频编码属性,视频存储文件);
返回视频存储文件;
}
公共异步任务StopVideoRecording()
{
//停止录像
等待mediaCapture.StopRecordAsync();
}
公共异步任务StartPreview()
{
//启动预览流
等待mediaCapture.StartPreviewSync();
}
公共异步任务StartPreview(IMediaExtension PreviewLink,双期望PreviewArea)
{
//默认预览格式选择器使用的受支持视频预览格式列表。
var supportedVideoFormats=新列表{“nv12”,“rgb32”};
//查找最接近所需大小的受支持预览大小
var可用的离散资产=
mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview)
第()类
.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();
//启动预览流
等待mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview,previewFormat);
等待mediaCapture.StartPreviewToCustomSinkAsync(新的MediaEncodingProfile{Video=previewFormat},PreviewLink);
}
公共异步任务检查()
{
//停止预览流
等待mediaCapture.StopReviewSync();
}
公共空间处置()
{
如果(mediaCapture!=null)
{
mediaCapture.Dispose();
mediaCapture=null;
}
}
}
2) 将MediaElement添加到xaml页面代码:
<Grid Background="#FF40B9F5">
<Grid.RowDefinitions>
<RowDefinition Height="543*"/>
<RowDefinition Height="97*"/>
</Grid.RowDefinitions>
<CaptureElement x:Name="CapturePreview" Grid.Row="0"/>
<Button x:Name="TakeWarrantyPhoto_Button" Content="TAKE PHOTO" HorizontalAlignment="Center" Grid.Row="1" VerticalAlignment="Center" Click="TakeWarrantyPhoto_Button_Click" BorderBrush="Black" Foreground="Black" FontFamily="Book Antiqua" FontWeight="Bold"/>
</Grid>
3) 第C页#代码:
public sealed分部类AddNewWarrantyPhotoPage:Page
{
私人摄影;
public AddNewWarrantyPhotoPage()
{
this.InitializeComponent();
DisplayInformation.AutoRotationPreferences=显示方向.横向;
}
///
///当此页面即将显示在框架中时调用。
///
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的异步重写无效OnNavigatedTo(NavigationEventArgs e)
{
cameraCapture=新的cameraCapture();
CapturePreview.Source=等待cameraCapture.Initialize();
等待拍照。开始预览();
}
受保护的覆盖异步无效OnNavigatedFrom(NavigationEventArgs e)
{
//释放资源
如果(cameraCapture!=null)
{
等待cameraCapture.StopReview();
CapturePreview.Source=null;
cameraCapture.Dispose();
cameraCapture=null;
}
}
私有异步void takewarranceyphoto_按钮_单击(对象发送方,路由目标)
{
var photoStorageFile=wait cameraCapture.Captur
public sealed partial class AddNewWarrantyPhotoPage : Page
{
private CameraCapture cameraCapture;
public AddNewWarrantyPhotoPage()
{
this.InitializeComponent();
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
cameraCapture = new CameraCapture();
CapturePreview.Source = await cameraCapture.Initialize();
await cameraCapture.StartPreview();
}
protected override async void OnNavigatedFrom(NavigationEventArgs e)
{
// Release resources
if (cameraCapture != null)
{
await cameraCapture.StopPreview();
CapturePreview.Source = null;
cameraCapture.Dispose();
cameraCapture = null;
}
}
private async void TakeWarrantyPhoto_Button_Click(object sender, RoutedEventArgs e)
{
var photoStorageFile = await cameraCapture.CapturePhoto();
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await photoStorageFile.OpenReadAsync());
//you can show it in your picture if you declare it in xaml:
// WarrantyPhotoDisplay_Image.Source = bitmap;
}
}