標籤:camera metro
這個項目不是用的系統內建的CameraCaptureUI,是自己寫的網路攝影機的調用,介面做的不好所以,不放了,但是可以實現拍照功能:
下面是using 程式命名空間:
using Windows.Media.Capture;using Windows.Media.MediaProperties;using Windows.UI.Xaml.Media.Imaging;using Windows.Storage;using Windows.Storage.Pickers;using Windows.Storage.Streams;using Windows.Devices.Enumeration;
可以看看win8 引用中這些方法的調用情況,
下面是全域變數:
<span style="white-space:pre"></span>private StorageFile file = null; private StorageFile photo = null; private StorageFile video = null; private MediaCapture mediaPhoto = null; private MediaCapture mediaVideo = null; private bool cameraStarted = false;
首先我們不調用系統內建的網路攝影機方法CameraCaptureUI,我們可以使用另外一種方法,下面是調用網路攝影機的觸發方法:
private async void btnCamera_Click(object sender, RoutedEventArgs e) { try { DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (devices.Count > 0) { if (mediaPhoto == null) { mediaPhoto = new MediaCapture(); await mediaPhoto.InitializeAsync(); capture1.Source = mediaPhoto; await mediaPhoto.StartPreviewAsync(); cameraStarted = true; } } } catch (Exception msg) { mediaPhoto = null; } }
調用了網路攝影機,還要求實現拍照的功能:
<span style="white-space:pre"></span>private async void btnPhoto_Click(object sender, RoutedEventArgs e) { if (mediaPhoto != null) { ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); photo = await ApplicationData.Current.LocalFolder.CreateFileAsync("hello.jpg", CreationCollisionOption.GenerateUniqueName); await mediaPhoto.CapturePhotoToStorageFileAsync(imgFormat, photo); } }
上面是拍照的觸發方法。
我們程式還要求實現將拍好的照片儲存好,這個還要求一個觸發方法:
<span style="white-space:pre"></span>private async void photoSave_Click(object sender, RoutedEventArgs e) { if (photo != null) { FileSavePicker photoPicker = new FileSavePicker(); photoPicker.CommitButtonText = "儲存圖片"; photoPicker.SuggestedFileName = "hello"; photoPicker.FileTypeChoices.Add("圖片",new string[]{".jpg",".jpeg",".png",".bmp"}); photoPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; StorageFile photoFile = await photoPicker.PickSaveFileAsync(); if (photoFile != null) { //開啟通過網路攝影機拍攝的照片,並返迴流,以流的形式讀取檔案 var streamRandom = await photo.OpenAsync(FileAccessMode.Read); //將拍攝的照片以流的形式讀取到緩衝區 IBuffer buffer = RandomAccessStreamToBuffer(streamRandom); //將緩衝區內容寫入相應的檔案夾中 await FileIO.WriteBufferAsync(photoFile, buffer); } } }
下面是自己寫的兩個函數:
//將圖片寫入到緩衝區 private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream) { Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0)); MemoryStream memoryStream = new MemoryStream(); IBuffer buffer = null; if (stream != null) { byte[] bytes = ConvertStreamTobyte(stream); //將流轉化為位元組型數組 if (bytes != null) { var binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(bytes); } } try { buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length); } catch (Exception msg) { } return buffer; } //將流轉換成二進位 public static byte[] ConvertStreamTobyte(Stream input) { byte[] buffer = new byte[1024 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } }
可以運行,謝謝大家指正。