这个项目不是用的系统自带的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();
}
}

能够执行,谢谢大家指正。

win8 metro 自己写摄像头拍照项目的更多相关文章

  1. win8 metro 自己写摄像头录像项目

    这是要求不适用CameraCaptureUI等使用系统自带的 camera  UI界面.要求我们自己写调用摄像头摄像的方法,如今我把我的程序贴下: UI界面的程序: <Page x:Class= ...

  2. android: 调用摄像头拍照

    很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄 ...

  3. VC/Wince 实现仿Win8 Metro风格界面1——设计概述和自绘Button(附效果图)

    去年用VC做了一个仿Win8 Metro风格的界面,感觉挺有意思,最近打算把实现过程和一些技术原理记录下来. 主要是风格上类似Win8,其实功能上很多借鉴了Android的操作方式.界面只支持两种大小 ...

  4. VS2010开发MFC ActiveX,摄像头拍照上传Webservice(1)

    最近工作项目,BS中需要用到摄像头拍照,需要存储本地,同时上传到服务器,尝试使用vc++做ActiveX来实现. 完全没有使用过vc,上网搜索各种知识,初步完成.在这里记录下有帮助的资料. 第一步:编 ...

  5. C# - VS2019调用AForge库实现调用摄像头拍照功能

    前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...

  6. 摄像头拍照,PHP输入流php://input的使用分析

    在做一个摄像头拍照然后上传的功能,php中使用php://input来获取内容.于是就了解了下php://input. 从官网信息来看,php://input是一个只读信息流,当请求方式是post的, ...

  7. 怎样让HTML5调用手机摄像头拍照——实践就是一切

    原文:怎样让HTML5调用手机摄像头拍照--实践就是一切 NanShan 小编将思路提供给了大家.学编程最重要的是实践,我这尽管有完好的代码,可是希望大家都能够自己写出属于自己的代码 HTML5 Th ...

  8. Android开发手记(32) 使用摄像头拍照

    在Android中,使用摄像头拍照一般有两种方法, 一种是调用系统自带的Camera,另一种是自己写一个摄像的界面. 我们要添加如下权限: <uses-permission android:na ...

  9. winform摄像头拍照 C#利用摄像头拍照

    这是我的第一篇博文,决定以后每个程序都要记录下来,方便以后查阅! 本人小菜一名,本程序也是查阅了网上各位前辈的博客和百度知道所整理出来的一个小程序. 第一次写有点不知道从何写起,先贴一张程序图吧. 程 ...

随机推荐

  1. React Native(六)——PureComponent VS Component

    先看两段代码: export class ywg extends PureComponent { …… render() { return ( …… ); } } export class ywg e ...

  2. webform的学习(2)

    突然回想一下,两周之后放假回家,三周之后重返学习,四周之后就要真正的面对社会,就这样有好多的舍不得在脑海中回旋,但是又是兴奋的想快点拥有自己的小生活,似乎太多的人在说程序的道路甚是艰难,我不知道我的选 ...

  3. php pear包打包方法

    一)首先下载工具onion 浏览器打开,服务器上wget测试无法正常下载 地址:https://raw.github.com/c9s/Onion/master/onion 二)在临时目录下,建立相关目 ...

  4. Cross-compilation using Clang

    Introduction This document will guide you in choosing the right Clang options for cross-compiling yo ...

  5. 利用按钮打开tabBar页面

    场景:当tabBar上有个人中心的时候,这里假设需要登陆才可以看到个人中心A页面,在A页面onload中先判断是否登陆,如果没有登陆就跳转到登陆页面B,待输入用户名和密码,点击登陆按钮后再跳转到A页面 ...

  6. centos 7安装jdk、tomcat

    jdk安装 创建上传目录: [root@ckl1 home]# pwd /home [root@ckl1 home]# mkdir upload 安装上传工具: yum install lrzsz 上 ...

  7. VI 你不知道的事

    1G 顶部 G 底部 ctrl+F 前进 ctrl+B 后退 /text   向前搜索 ?text 向后搜索 I i 插入字符串 a 光标后插入字符 A 跳到句末尾 wq 写入并退出 h k j l ...

  8. 【咸鱼教程】JsZip压缩与解压教程

    引擎版本3.0.6 教程目录一 为什么要用jszip二 如何使用jszip    2.1 下载jszip库    2.2 导入jszip库    2.3 加载和解压zip代码三 Demo源码下载 一 ...

  9. [右键]如何添加Sublime为右键菜单

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text\command] @=&q ...

  10. java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException

    启动spring boot项目的时候遇到了报错: -Sep- ::15.513 INFO [main] org.apache.catalina.core.StandardService.startIn ...