剛剛做過這類開發,所以就先獻丑了,當然所貼上的源代碼都是經過驗證過的,已經執行成功了,希望能夠給大家一些借鑒:
以下是metro UI代碼:
<Pagex:Class="Camera.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:Camera"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Button x:Name="btnCamera" Content="打開攝像頭" HorizontalAlignment="Left" Margin="48,259,0,0" VerticalAlignment="Top" Click="btnCamera_Click" Height="45"/><Image x:Name="img1" HorizontalAlignment="Left" Height="609" Margin="240,78,0,0" VerticalAlignment="Top" Width="718" Stretch="Fill"/><Button x:Name="btnSave" Content="保存圖片" HorizontalAlignment="Left" Margin="48,369,0,0" VerticalAlignment="Top" Height="44" Click="btnSave_Click"/></Grid>
</Page>
顯示的界面事實上非常easy,可是這不重要,功能才是基本的,以下貼上基本的開發代碼:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage.Streams;// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238/** * 作者:李天鵬* 功能:調用PC上自帶的camera實現拍照的功能,并保存在對應的目錄下* */
namespace Camera
{/// <summary>/// An empty page that can be used on its own or navigated to within a Frame./// </summary>public sealed partial class MainPage : Page{private StorageFile file = null;public MainPage(){this.InitializeComponent();}private async void btnCamera_Click(object sender, RoutedEventArgs e){CameraCaptureUI dialog = new CameraCaptureUI();dialog.PhotoSettings.CroppedAspectRatio = new Size(16, 9);file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);if (file != null){BitmapImage bitmapImage = new BitmapImage();using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)){bitmapImage.SetSource(fileStream);}img1.Source = bitmapImage;}}private async void btnSave_Click(object sender, RoutedEventArgs e){if (img1.Source == null)return;else{FileSavePicker picker = new FileSavePicker();picker.CommitButtonText = "保存";picker.SuggestedFileName = "hello";picker.FileTypeChoices.Add("圖片",new string[]{".jpg",".jpeg",".bmp",".png"});picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;StorageFile filePath = await picker.PickSaveFileAsync();if (filePath != null){//打開通過攝像頭拍攝的照片,并返回流,以流的形式讀取文件var streamRandom = await file.OpenAsync(FileAccessMode.Read);//將拍攝的照片以流的形式讀取到緩沖區IBuffer buffer = RandomAccessStreamToBuffer(streamRandom);//將緩沖區內容寫入對應的目錄中await FileIO.WriteBufferAsync(filePath, buffer);}}}//將圖片寫入到緩沖區private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream){Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0));MemoryStream memoryStream = new MemoryStream();if (stream != null){byte[] bytes = ConvertStreamTobyte(stream); //將流轉化為字節型數組if (bytes != null){var binaryWriter = new BinaryWriter(memoryStream);binaryWriter.Write(bytes);}}IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length);return buffer;}//將流轉換成二進制public static byte[] ConvertStreamTobyte(Stream input){byte[] buffer = new byte[16 * 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();}}}
}
可是這還不夠,假設須要調用camera,還須要一些權限,應該在Package.appxmanifest里面改動權限,
改動例如以下:僅僅要勾上webcam即可了。
源代碼下載:
http://download.csdn.net/detail/litianpeng1991/7548065