uwp之拍照(使用后置摄像头)
uwp开启摄像头要借助CaptureElement呈现来自捕获设备(如照相机或网络摄像机)的流。今天讲讲如何打开摄像头,获取焦点,以及拍照。废话不多说,下面直接上代码。当然前提是一定要记住在appxmanifest文件Capabilities选项选择Webcam,不然会报错
首先 XAML代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<CaptureElement x:Name="capturePreview" Stretch="UniformToFill" />
<Image Name="ProfilePic" />
</Grid>
<StackPanel VerticalAlignment="Bottom">
<Slider
x:Name="FocusValueSlider"
Margin="12,0,15,0"
Header="焦点调节:"
LargeChange="25"
Maximum="1000"
Minimum="0"
SmallChange="1"
ValueChanged="FocusValueSlider_ValueChanged"
Value="500" />
<StackPanel Orientation="Horizontal">
<Button Click="PhotographButton_Click" Content="启动摄像头" />
<Button
Margin="50,0,0,0"
Click="CapturePhoto_Click"
Content="拍照" />
</StackPanel>
</StackPanel>
</Grid>
//启动摄像头
async private void PhotographButton_Click(object sender, RoutedEventArgs e)
{
if (captureManager == null)
{
capturePreview.Visibility = Visibility.Visible;
ProfilePic.Visibility = Visibility.Collapsed;
captureManager = new MediaCapture(); //选择后置摄像头
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
System.Diagnostics.Debug.WriteLine("No camera device found!");
return;
}
var settings = new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
//MediaCategory = MediaCategory.Other,
//AudioProcessing = AudioProcessing.Default,
//PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraDevice.Id
};
await captureManager.InitializeAsync(settings);
//摄像头旋转90度
//captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
capturePreview.Source = captureManager;
await captureManager.StartPreviewAsync();
}
}
private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desired)
{ var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired); return desiredDevice ?? allVideoDevices.FirstOrDefault();
}
//拍照
async private void CapturePhoto_Click(object sender, RoutedEventArgs e)
{ if (captureManager != null)
{
capturePreview.Visibility = Visibility.Collapsed;
ProfilePic.Visibility = Visibility.Visible;
ProfilePic.Source = null;
//declare string for filename
string captureFileName = string.Empty; //图片格式
ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg(); //创建本地存储文件夹
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"Photo"+DateTime.Now.ToString("yyMMddHHmmss")+".jpg",
CreationCollisionOption.ReplaceExisting); await captureManager.CapturePhotoToStorageFileAsync(format, file); BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); ProfilePic.Source = bmpImage;//释放摄像头资源
capturePreview.Visibility = Visibility.Visible;
ProfilePic.Visibility = Visibility.Collapsed;
//captureManager.Dispose();
//captureManager = null;
} }
private void FocusValueSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
try
{ uint focus = Convert.ToUInt32(e.NewValue);
SetFocus(focus);
}
catch
{ }
}
//设置摄像头焦点方法
private async void SetFocus(uint? focusValue = null)
{ try
{ if (!focusValue.HasValue)
{
focusValue = ;
} if (captureManager.VideoDeviceController.FocusControl.Supported)
{ captureManager.VideoDeviceController.FlashControl.AssistantLightEnabled = false; captureManager.VideoDeviceController.FocusControl.Configure(new FocusSettings() { Mode = FocusMode.Manual, Value = focusValue, DisableDriverFallback = true }); await captureManager.VideoDeviceController.FocusControl.FocusAsync();
}
}
catch { }
}
uwp之拍照(使用后置摄像头)的更多相关文章
- wp8.1之拍照(获取焦点,使用后置摄像头)
wp8.1 没有像wp8一样直接用启动器开启摄像头,他要开启摄像头要借助CaptureElement呈现来自捕获设备(如照相机或网络摄像机)的流.今天讲讲如何打开摄像头,获取焦点,以及拍照.废话不多说 ...
- android实现前置后置摄像头相互切换
首先自定义一个继承自SurfaceView并且实现了SurfaceHolder.Callback接口的组件: public class CameraView extends SurfaceView i ...
- win 10 禁用后置摄像头
2.双摄像头电脑,甄别时默认开启的是后置摄像头,识别不到人脸. (1)更换设备参加甄别: (2)自行调整:停用电脑后置摄像头,停用后甄别时会默认调取前置摄像头: 以下操作适用于Windows surf ...
- HTML5 选择前置摄像头,选择后置摄像头
最近发现我写的都是乱七八糟的,觉得应该给大家带点福利,于是写了这篇 背景:最近想做个web应用,需要用到摄像头,但是发现默认一直是前置摄像头,拍照很麻烦,于是找了很多文章,居然没有人提到,只好FQ去找 ...
- android Camera 如何判断当前使用的摄像头是前置还是后置
现在 android 平台的智能手机一般都标配有两颗摄像头.在 Camera 中都存在摄像头切换的功能. 并且有一些功能前后置摄像头上会有所不同.譬如人脸检测,人脸识别,自动对焦,闪光灯等功能, 如果 ...
- Linux笔记(固定USB摄像头硬件端口,绑定前后置摄像头)
在Android的系统会有前置摄像头和后置摄像头的定义,摄像头分为SOC类型的摄像头和USB这一类的摄像头,接下要分析就是USB摄像头这一类 . 一般在android或者linux系统中分析一个模块, ...
- Android切换前后置摄像头并录制视频
项目需要对微信的视频模块也看了一下,在此就对这块进行了一个开发.首先给出效果图 首先给出java代码 /** * RecordActivity.java * 版权所有(C) 2013 * 创建:cui ...
- JMeter学习-009-JMeter 后置处理器实例之 - 正则表达式提取器(二)多参数获取
前文简述了通过后置处理器 - 正则表达式提取器 获取 HTTP请求 响应结果中的特定数据,未看过的亲,敬请参阅 JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一). ...
- JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一)概述及简单实例
上文我们讲述了如何对 HTTP请求 的响应数据进行断言,以判断响应是否符合我们的预期,敬请参阅:JMeter学习-007-JMeter 断言实例之一 - 响应断言 那么我们如何获取 HTTP请求 响应 ...
随机推荐
- js如何操作表格(常用属性方法汇总)
js如何操作表格(常用属性方法汇总) 一.总结 一句话总结: 二.表格相关的属性和方法 1.1 Table 对象集合 cells[] 返回包含表格中所有单元格的一个数组. 语法:tableObject ...
- FMDB数据库框架
nFMDB nFMDB n什么是FMDB pFMDB是iOS平台的SQLite数据库框架 pFMDB以OC的方式封装了SQLite的C语言API p nFMDB的优点 p使用起来更加面向对象,省去 ...
- Oracle导入脚本文件乱码问题
用脚本直接导入,Oracle出现乱码 绝大多数情况是Oracle客户端环境变量NLS_LANG的值和数据库字符集不一致导致. (注nls_lang修改的是Oracle客户端字符集的编码,locale命 ...
- 数码测色计获取UIColor的RGB
1.打开电脑数码测色计 2.鼠标移动到需要的颜色上 3.获取相应RGB值: [UIColor colorWithRed:204.f/255.f green:0.f/255.f blue:212.f/2 ...
- Java Web 实用返回结果封装
实用的返回结果封装 使用示例 效果 Result ResultEnum 使用示例 /** * @Author: huangwenjun * @Description: * @Date: Created ...
- 【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- vim note (2)
:e edit :edit edit :e. edit current file '.' a dot is means current : w writ ...
- PHP设计模式——迭代模式
声明:这一系列的博客引用<大话设计模式>.程洁作者. 迭代器模式:迭代器模式是遍历集合的成熟模式.迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对 ...
- linux 命令学习 —— 硬件外设管理(dmesg、lsusb)
dmesg:print or control the kernel ring buffer dmesg命令设备故障的诊断是非常重要的.在dmesg命令的帮助下进行硬件的连接或断开连接操作时,我们可以看 ...
- [STM32] IAP不能跳转到APP
用w5500做网页iap升级, 文件上传正确, 对比内部flash内容也正确, 就是不跳转. 查资料, 怀疑是中断的影响. 在跳转前关闭中断, 问题解决! // IAP程序使用了中断, 跳转前关闭 _ ...