刚刚做过这类开发,所以就先献丑了,当然所贴上的源代码都是经过验证过的,已经执行成功了,希望能够给大家一些借鉴:

以下是metro UI代码:

<Page
x: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

最新文章

  1. java .bat批处理(java cmd命令)
  2. 关于腾讯云ubuntu服务器tomcat访问慢问题
  3. 青瓷引擎使用心得——修改引擎的loading界面
  4. C++ 使用SQLite
  5. css页面点击文字出现蓝色底色去掉方法
  6. 二叉查找树(二)之 C++的实现
  7. Http协议之Get和Post的区别
  8. [转]Cygwin的包管理器:apt-cyg
  9. 2013 ACM/ICPC Asia Regional Changsha Online G Goldbach
  10. Ncurses &lt;一&gt;
  11. 通过layout实现可拖拽自动排序的UICollectionView
  12. SQL -- 是否或推断线相交以在其内部的平面
  13. python之面向对象深入探测
  14. C# Winform开发以及控件开发的需要注意的,被人问怕了,都是基础常识
  15. python之路--模块和包
  16. ansible基础命令实例
  17. redis应用实践
  18. ionic打包报错Execution failed for task &#39;:processDebugResources&#39;
  19. 公共的service接口
  20. [转]MySQL实现分页查询

热门文章

  1. lightoj1027(期望dp)
  2. ASP.NET Core环境并运行 继续跨平台
  3. windows phone 三种数据共享的方式(8)
  4. Windows Phone开发(33):路径之其它Geometry
  5. hdu3873 有约束条件的最短路
  6. Android asynctask使用
  7. POJ 1475 Pushing Boxes 搜索- 两重BFS
  8. 静态方法使用bean
  9. Chromium Graphics: GPUclient的原理和实现分析之间的同步机制-Part II
  10. T-SQL基础(5) - 表表达式