[英]Photo capture on Windows Store App for Windows Phone
好吧,我的問題很簡單:
如何使用相機使用適用於Windows Phone 8.1
的Windows Store App
捕獲圖片?
MSDN上的示例使用Windows.Media.Capture.CameraCaptureUI
,它在Windows Phone上不可用,或者用於Silverlight
。
我無法使用Windows運行時專門為Windows Phone應用找到任何文檔或示例。
如果有人知道,或者甚至有這方面的文件,我會很高興。
在WP8.1 Runtime(也在Silverlight中)中,您可以使用MediaCapture 。 簡而言之:
// First you will need to initialize MediaCapture
Windows.Media.Capture.MediaCapture takePhotoManager = new Windows.Media.Capture.MediaCapture();
await takePhotoManager.InitializeAsync();
如果需要預覽,可以使用CaptureElement :
// In XAML:
<CaptureElement x:Name="PhotoPreview"/>
然后在后面的代碼中,您可以像這樣開始/停止預覽:
// start previewing
PhotoPreview.Source = takePhotoManager;
await takePhotoManager.StartPreviewAsync();
// to stop it
await takePhotoManager.StopPreviewAsync();
最后拍攝照片,您可以將其直接帶到文件CapturePhotoToStorageFileAsync或Stream CapturePhotoToStreamAsync :
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
// a file to save a photo
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"Photo.jpg", CreationCollisionOption.ReplaceExisting);
await takePhotoManager.CapturePhotoToStorageFileAsync(imgFormat, file);
如果您想捕獲視頻,那么這里有更多信息 。
另外,不要忘記在清單文件的Capabilities
和Requirements
Front/Rear Camera
Webcam
中添加Webcam
。
如果您需要選擇相機(前/后),您需要獲取相機ID,然后使用所需的設置初始化MediaCapture
:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired));
}
async private void InitCamera_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
}
在通用Windows Phone 8.1(WinRT)應用程序中,不再可能直接跳入內置相機應用程序並在拍攝照片時接收回調。
為此,您必須如上所述實現Windows.Media.Capture.MediaCapture
。 曾經有CameraCatureUI
但它在Windows Phone 8.1的WinRT應用程序中不可用。
但是有一個“解決方法”。 您可以使用Windows.Storage.Pickers.FileOpenPicker
並將其配置為選擇圖像。 現在選擇器將有一個相機按鈕。 用戶可以單擊相機按鈕,內置的相機應用程序將打開。 用戶拍完照片后,您會在應用中收到回電信息。 FileOpenPicker
回調實現起來有點煩人,但它確實有效。 如果您可以接受可用性含義,那么這可能是一種有效的方法。
在2014年的微軟建設大會期間,有關於此主題的會議。 您可以通過此鏈接在線觀看會話。
您可以在此鏈接上采用該方法。 一切都很好地解釋了。
只需使用PhotoCamera
類,不要忘記在應用清單中啟用相機使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.