直接附上代码,注视写的非常详细。
前台xaml代码:一个Image控件,显示通过摄像头拍摄的照片;一个按钮,负责启动摄像头;一个AppBar,负责保存图片
<Page.TopAppBar>
<AppBar>
<Button Name="btnSave" Click="btnSave_Click_1" Style="{StaticResource SaveAppBarButtonStyle}"/>
</AppBar>
</Page.TopAppBar>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image x:Name="CapturedPhoto" MaxWidth="392" Stretch="Uniform" />
<Button Content="启动摄像头" HorizontalAlignment="Left" Margin="378,37,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
</Grid>
后台c#代码:
首先声明一个全局StorageFile类型变量,指向的是通过摄像有拍摄的照片
StorageFile file = null;
在“启动摄像头”的单击事件中,启动摄像头,拍摄照片,显示照片
/*CameraCaptureUI类负责启动摄像头,并捕获照摄像头拍摄的照片
要想启动摄像头,需打开Package.appxmanifest文件,切换到“功能”界面,
勾选网络摄像机和麦克风*/
CameraCaptureUI dialog = new CameraCaptureUI();
Size aspectRatio = new Size(16, 9);
/*设定摄像头拍摄照片的高宽比,拍照完成之后就能看到效果,可以通过修改高宽比
来查看不同的效果*/
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;
/*CaptureFileAsync方法负责启动摄像有并拍摄照片或拍摄影片,其返回值为拍摄的照片或拍摄的影片.
CameraCaptureUIMode是个枚举,设置拍摄的是照片还是影片*/
file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
/*BitmapImage类是ImageSource类的子类,用来将图片填充到Image控件中*/
BitmapImage bitmapImage = new BitmapImage();
/*使用OpenAsync打开拍摄的照片,返回流filestream*/
using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
{
//设置bitmapImage的图片源为拍摄的照片
bitmapImage.SetSource(fileStream);
}
//将照片显示到图片控件中
CapturedPhoto.Source = bitmapImage;
}
保存按钮的单击事件代码
//实例化文件选取器
FileSavePicker picker = new FileSavePicker();
//设置图片保存的类型
picker.FileTypeChoices.Add("image file", new List<string>() { ".jpg",".png" });
/*返回文件选取器返回的新建的文件:调用PickSaveFileAsync方法后会在制定的位置
创建制定名称制定格式的文件*/
StorageFile storageFile = await picker.PickSaveFileAsync();
//打开通过摄像头拍摄的照片,并返回流,以流的形式读取文件
var StreamRandom = await file.OpenAsync(FileAccessMode.Read);
//将拍摄的照片以流的方式读取到缓冲区
IBuffer buffer = RandomAccessStreamToBuffer(StreamRandom);
//将缓冲区的内容(拍摄的照片)写入到新建的文件中
await FileIO.WriteBufferAsync(storageFile, buffer);
RandomAccessStreamToBuffer方法
<div class="dp-highlighter bg_csharp"><div class="bar"><div class="tools"><b>[csharp]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-c"><li class="alt"><span><span class="keyword">private</span><span> IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0)); </span></li><li class=""><span> MemoryStream memoryStream = <span class="keyword">new</span><span> MemoryStream(); </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (stream != </span><span class="keyword">null</span><span>) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">byte</span><span>[] bytes = ConvertStreamTobyte(stream); </span></span></li><li class=""><span> <span class="keyword">if</span><span> (bytes != </span><span class="keyword">null</span><span>) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> var binaryWriter = <span class="keyword">new</span><span> BinaryWriter(memoryStream); </span></span></li><li class="alt"><span> binaryWriter.Write(bytes); </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (<span class="keyword">int</span><span>)memoryStream.Length); </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> buffer; </span></span></li><li class=""><span> } </span></li></ol></div><pre class="csharp" name="code" style="display: none;">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;
}</pre><br>
<pre></pre>
<p>ConvertStreamTobyte方法</p>
<div class="dp-highlighter bg_csharp"><div class="bar"><div class="tools"><b>[csharp]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-c"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">byte</span><span>[] ConvertStreamTobyte(Stream input) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">byte</span><span>[] buffer = </span><span class="keyword">new</span><span> </span><span class="keyword">byte</span><span>[16 * 1024]; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">using</span><span> (MemoryStream ms = </span><span class="keyword">new</span><span> MemoryStream()) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">int</span><span> read; </span></span></li><li class=""><span> <span class="keyword">while</span><span> ((read = input.Read(buffer, 0, buffer.Length)) > 0) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> ms.Write(buffer, 0, read); </span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="keyword">return</span><span> ms.ToArray(); </span></span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li></ol></div><pre class="csharp" name="code" style="display: none;">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();
}
}</pre>
<p><br>
<br>
最后,为了看得更清楚,附上完整的源代码</p>
<div class="dp-highlighter bg_csharp"><div class="bar"><div class="tools"><b>[csharp]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-c"><li class="alt"><span><span class="comment">//声明变量,指向有摄像有拍摄的照片或影片</span><span> </span></span></li><li class=""><span> StorageFile file = <span class="keyword">null</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> MainPage() </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">this</span><span>.InitializeComponent(); </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">/// <summary></span><span> </span></span></li><li class="alt"><span> <span class="comment">/// 在此页将要在 Frame 中显示时进行调用。</span><span> </span></span></li><li class=""><span> <span class="comment">/// </summary></span><span> </span></span></li><li class="alt"><span> <span class="comment">/// <param name="e">描述如何访问此页的事件数据。Parameter</span><span> </span></span></li><li class=""><span> <span class="comment">/// 属性通常用于配置页。</param></span><span> </span></span></li><li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">override</span><span> </span><span class="keyword">void</span><span> OnNavigatedTo(NavigationEventArgs e) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="comment">//拍照</span><span> </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> async </span><span class="keyword">void</span><span> Button_Click_1(</span><span class="keyword">object</span><span> sender, RoutedEventArgs e) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="comment">/*CameraCaptureUI类负责启动摄像头,并捕获照摄像头拍摄的照片</span> </span></li><li class=""><span><span class="comment"> 要想启动摄像头,需打开Package.appxmanifest文件,切换到“功能”界面,</span> </span></li><li class="alt"><span><span class="comment"> 勾选网络摄像机和麦克风*/</span><span> </span></span></li><li class=""><span> CameraCaptureUI dialog = <span class="keyword">new</span><span> CameraCaptureUI(); </span></span></li><li class="alt"><span> Size aspectRatio = <span class="keyword">new</span><span> Size(16, 9); </span></span></li><li class=""><span> <span class="comment">/*设定摄像头拍摄照片的高宽比,拍照完成之后就能看到效果,可以通过修改高宽比</span> </span></li><li class="alt"><span><span class="comment"> 来查看不同的效果*/</span><span> </span></span></li><li class=""><span> dialog.PhotoSettings.CroppedAspectRatio = aspectRatio; </span></li><li class="alt"><span> <span class="comment">/*CaptureFileAsync方法负责启动摄像有并拍摄照片或拍摄影片,其返回值为拍摄的照片或拍摄的影片.</span> </span></li><li class=""><span><span class="comment"> CameraCaptureUIMode是个枚举,设置拍摄的是照片还是影片*/</span><span> </span></span></li><li class="alt"><span> file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo); </span></li><li class=""><span> <span class="keyword">if</span><span> (file != </span><span class="keyword">null</span><span>) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> <span class="comment">/*BitmapImage类是ImageSource类的子类,用来将图片填充到Image控件中*/</span><span> </span></span></li><li class="alt"><span> BitmapImage bitmapImage = <span class="keyword">new</span><span> BitmapImage(); </span></span></li><li class=""><span> <span class="comment">/*使用OpenAsync打开拍摄的照片,返回流filestream*/</span><span> </span></span></li><li class="alt"><span> <span class="keyword">using</span><span> (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="comment">//设置bitmapImage的图片源为拍摄的照片</span><span> </span></span></li><li class=""><span> bitmapImage.SetSource(fileStream); </span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="comment">//将照片显示到图片控件中</span><span> </span></span></li><li class="alt"><span> CapturedPhoto.Source = bitmapImage; </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="comment">//保存图片,为了显得正式些,使用了AppBar</span><span> </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> async </span><span class="keyword">void</span><span> btnSave_Click_1(</span><span class="keyword">object</span><span> sender, RoutedEventArgs e) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="comment">//实例化文件选取器</span><span> </span></span></li><li class=""><span> FileSavePicker picker = <span class="keyword">new</span><span> FileSavePicker(); </span></span></li><li class="alt"><span> <span class="comment">//设置图片保存的类型</span><span> </span></span></li><li class=""><span> picker.FileTypeChoices.Add(<span class="string">"image file"</span><span>, </span><span class="keyword">new</span><span> List<</span><span class="keyword">string</span><span>>() { </span><span class="string">".jpg"</span><span>,</span><span class="string">".png"</span><span> }); </span></span></li><li class="alt"><span> <span class="comment">/*返回文件选取器返回的新建的文件:调用PickSaveFileAsync方法后会在制定的位置</span> </span></li><li class=""><span><span class="comment"> 创建制定名称制定格式的文件*/</span><span> </span></span></li><li class="alt"><span> StorageFile storageFile = await picker.PickSaveFileAsync(); </span></li><li class=""><span> <span class="comment">//打开通过摄像头拍摄的照片,并返回流,以流的形式读取文件</span><span> </span></span></li><li class="alt"><span> var StreamRandom = await file.OpenAsync(FileAccessMode.Read); </span></li><li class=""><span> <span class="comment">//将拍摄的照片以流的方式读取到缓冲区</span><span> </span></span></li><li class="alt"><span> IBuffer buffer = RandomAccessStreamToBuffer(StreamRandom); </span></li><li class=""><span> <span class="comment">//将缓冲区的内容(拍摄的照片)写入到新建的文件中</span><span> </span></span></li><li class="alt"><span> await FileIO.WriteBufferAsync(storageFile, buffer); </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> } </span></li><li class="alt"><span> <span class="comment">//将图片写入到缓冲区</span><span> </span></span></li><li class=""><span> <span class="keyword">private</span><span> IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0)); </span></li><li class="alt"><span> MemoryStream memoryStream = <span class="keyword">new</span><span> MemoryStream(); </span></span></li><li class=""><span> <span class="keyword">if</span><span> (stream != </span><span class="keyword">null</span><span>) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> <span class="keyword">byte</span><span>[] bytes = ConvertStreamTobyte(stream); </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (bytes != </span><span class="keyword">null</span><span>) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> var binaryWriter = <span class="keyword">new</span><span> BinaryWriter(memoryStream); </span></span></li><li class=""><span> binaryWriter.Write(bytes); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (<span class="keyword">int</span><span>)memoryStream.Length); </span></span></li><li class=""><span> <span class="keyword">return</span><span> buffer; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="comment">//将流转换成二进制</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">byte</span><span>[] ConvertStreamTobyte(Stream input) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">byte</span><span>[] buffer = </span><span class="keyword">new</span><span> </span><span class="keyword">byte</span><span>[16 * 1024]; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">using</span><span> (MemoryStream ms = </span><span class="keyword">new</span><span> MemoryStream()) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">int</span><span> read; </span></span></li><li class=""><span> <span class="keyword">while</span><span> ((read = input.Read(buffer, 0, buffer.Length)) > 0) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> ms.Write(buffer, 0, read); </span></li><li class="alt"><span> } </span></li><li class=""><span> <span class="keyword">return</span><span> ms.ToArray(); </span></span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li></ol></div><pre class="csharp" name="code" style="display: none;"> //声明变量,指向有摄像有拍摄的照片或影片
StorageFile file = null;
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// 在此页将要在 Frame 中显示时进行调用。
/// </summary>
/// <param name="e">描述如何访问此页的事件数据。Parameter
/// 属性通常用于配置页。</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
//拍照
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
/*CameraCaptureUI类负责启动摄像头,并捕获照摄像头拍摄的照片
要想启动摄像头,需打开Package.appxmanifest文件,切换到“功能”界面,
勾选网络摄像机和麦克风*/
CameraCaptureUI dialog = new CameraCaptureUI();
Size aspectRatio = new Size(16, 9);
/*设定摄像头拍摄照片的高宽比,拍照完成之后就能看到效果,可以通过修改高宽比
来查看不同的效果*/
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;
/*CaptureFileAsync方法负责启动摄像有并拍摄照片或拍摄影片,其返回值为拍摄的照片或拍摄的影片.
CameraCaptureUIMode是个枚举,设置拍摄的是照片还是影片*/
file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
/*BitmapImage类是ImageSource类的子类,用来将图片填充到Image控件中*/
BitmapImage bitmapImage = new BitmapImage();
/*使用OpenAsync打开拍摄的照片,返回流filestream*/
using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
{
//设置bitmapImage的图片源为拍摄的照片
bitmapImage.SetSource(fileStream);
}
//将照片显示到图片控件中
CapturedPhoto.Source = bitmapImage;
}
}
//保存图片,为了显得正式些,使用了AppBar
private async void btnSave_Click_1(object sender, RoutedEventArgs e)
{
//实例化文件选取器
FileSavePicker picker = new FileSavePicker();
//设置图片保存的类型
picker.FileTypeChoices.Add("image file", new List<string>() { ".jpg",".png" });
/*返回文件选取器返回的新建的文件:调用PickSaveFileAsync方法后会在制定的位置
创建制定名称制定格式的文件*/
StorageFile storageFile = await picker.PickSaveFileAsync();
//打开通过摄像头拍摄的照片,并返回流,以流的形式读取文件
var StreamRandom = await file.OpenAsync(FileAccessMode.Read);
//将拍摄的照片以流的方式读取到缓冲区
IBuffer buffer = RandomAccessStreamToBuffer(StreamRandom);
//将缓冲区的内容(拍摄的照片)写入到新建的文件中
await FileIO.WriteBufferAsync(storageFile, 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();
}
}</pre>
<p><br>
<br>
<br>
<br>
</p>
<pre></pre>
<pre></pre>
CIO之家 www.ciozj.com 公众号:imciow