win8 metro 调用摄像头拍摄照片并将照片保存在对应的位置
刚刚做过这类开发,所以就先献丑了,当然所贴上的源代码都是经过验证过的,已经执行成功了,希望能够给大家一些借鉴:
以下是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
win8 metro 调用摄像头拍摄照片并将照片保存在对应的位置的更多相关文章
- Win8 Metro(C#) 数字图像处理--1 图像打开,保存
原文:Win8 Metro(C#) 数字图像处理--1 图像打开,保存 作为本专栏的第一篇,必不可少的需要介绍一下图像的打开与保存,一便大家后面DEMO的制作. Win8Metro编程中,图像相关 ...
- win8 metro 自己写摄像头拍照项目
这个项目不是用的系统自带的CameraCaptureUI.是自己写的摄像头的调用,界面做的不好所以,不放了.可是能够实现拍照功能: 以下是using 程序命名空间: using Windows.Med ...
- Unity 3D 调用摄像头捕获照片 录像
1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用. 2,定义WebCamTexture的变量用于捕获单张照片. 3,连续捕获须启用线程. 实现代码: using UnityEngine; ...
- 微信公众号开发调用摄像头、拍摄或选择图片、OCR识别
一 .准备工作 <1> 域名认证准备工作 在需要调用摄像头的接口页面引入微信的js,具体地址为:(支持https):http://res.wx.qq.com/open/js/ ...
- win8 metro 自己写摄像头录像项目
这是要求不适用CameraCaptureUI等使用系统自带的 camera UI界面.要求我们自己写调用摄像头摄像的方法,如今我把我的程序贴下: UI界面的程序: <Page x:Class= ...
- web HTML5 调用摄像头的代码
最近公司要求做一个在线拍照的功能,具体代码如下: <html> <head> <title>html5调用摄像头拍照</title> <style ...
- Android开发中如何调用摄像头的功能
我们要调用摄像头的拍照功能,显然 第一步必须加入调用摄像头硬件的权限,拍完照后我们要将图片保存在SD卡中,必须加入SD卡读写权限,所以第一步,我们应该在Android清单文件中加入以下代码 & ...
- 用MVVM模式开发中遇到的零散问题总结(4)——自制摄像头拍摄大头贴控件
原文:用MVVM模式开发中遇到的零散问题总结(4)--自制摄像头拍摄大头贴控件 一直有个疑问,为什么silverlight对摄像头支持这么好,WPF却一个库都没有....于是我各种苦恼啊,各种Code ...
- android ——调用摄像头拍照和相册
先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
随机推荐
- wordpress博客近期变慢之解决(fonts.google.com)
近期发现站点訪问速度变慢.博客文章打开速度特慢,也没改动过东西. 并且近期发现google的服务非常多訪问都打不开或是变慢. 于是知道可能是那"伟大东西"在作坏事了. 症状: 网页 ...
- WPF-21:WPF实现仿安卓的图案密码键盘(初级)
希望大家有这方面好的代码给提供下,谢谢了! 想用C#做一个和手机上一样的图形密码键盘,貌似这方面资料比较少,虽然winphone手机上也有但是网上也没有这方面的代码.只好用常规的思维去实现一下,当然是 ...
- Java中动态代理技术生成的类与原始类的区别 (转)
用动态代理的时候,对它新生成的类长什么样子感到好奇.有幸通过一些资料消除了心里的疑惑. 平时工作使用的Spring框架里面有一个AOP(面向切面)的机制,只知道它是把类重新生成了一遍,在切面上加上了后 ...
- Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...
- rsync 只是测试,请看下一篇
实现从客户服务器去同步资源服务器 1.解压 # tar -xzpvf rsync-2.5.6.tar.gz 编译安装 # cd rsync-2.5.6/ # ./configure --pref ...
- 9、Cocos2dx 3.0游戏开发找小三之工厂方法模式与对象传值
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27704153 工厂方法模式 工厂方法是程序设计中一个 ...
- 移动web:图片切换(焦点图)
在web页面上图片切换(焦点图)效果实在是太常见了,PC端.移动端到处都有它的身影. 上次写了个tab选项卡的效果,在这里延续一下,改成图片切换的效果. 如果不需要自动播放,稍微修改下html标签.和 ...
- 自己写的sql排序
create function dbo.Fn_Sort ( @str varchar(1024) ) returns nvarchar(100) as begin declare @tb table ...
- HTML5之Canvas影片广场
HTML5之Canvas影片广场 1.设计源代码 <!DOCTYPE html> <head> <meta charset="utf-8" /> ...
- 微信小程序开发者工具集合包
开发论坛 http://www.henkuai.com/forum-56-1.html 工具包下载 https://yunpan.cn/ckXFpuzAeVi2s 访问密码 b4cc 开发文档 h ...