这个项目不是用的系统自带的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();
}
}
能够执行,谢谢大家指正。