win8 metro 自己寫網路攝影機拍照項目

來源:互聯網
上載者:User

標籤: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();            }        }


可以運行,謝謝大家指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.