用Windows 10,WinRT拍照 [英] Take picture with Windows 10, WinRT

查看:335
本文介绍了用Windows 10,WinRT拍照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望它的东西简单蠢,但我旋转我的轮子。



我有一个Surface Pro 4,Windows 10和使用Visual Studio 2013专业版。
使用C#4.5开发WPF。



总而言之,我想要做一个简单的相机捕获来保存一个图像而不诉诸其他第三方库没有控制权。这篇文章的其余部分是其他研究结果的详细信息,我已经研究和尝试工作,什么失败和编译器这样的消息。



再次,简单



编辑1样本代码 p>

这里是来自WPF表单的代码。我甚至没有任何控制的形式,因为我甚至不能等待编译。

 使用System; 
使用Windows.Media.Capture;
使用System.Windows;

命名空间CameraCapture
{
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent ;
JustDoIt();
}

private MediaCapture _mediaManager;
private async void JustDoIt()
{
//使用默认的相机初始化mediacapture
_mediaManager = new MediaCapture();
await _mediaManager.InitializeAsync();

if(_mediaManager == null)
MessageBox.Show(Failed Initialize);

await _mediaManager.StartPreviewAsync();
}
}
}

从下面研究的其他链接,dll为

  System.Runtime.dll和
System.Runtime.WindowsRuntime.dll

每个'await'的编译错误

 错误1'await'要求类型Windows.Foundation.IAsyncAction具有适当的GetAwaiter方法。你缺少System的using指令吗? 

作为第一行。使用WinRT或默认系统包括时,是否还有其他等待?



编辑结束



我开始使用 https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.cameracaptureui.aspx
CameraCaptureUI类



我找到此链接 https://www.eternalcoding.com/?p=183
如何从桌面应用程序使用特定的WinRT API
我尝试了所有的步骤,并得到与

相关的错误

等待...有一个合适的GetAwaiter方法。



所以,我看了关于asynch和等待,遇到



如何以及何时使用`async`和`await` < a>
如何以及何时使用 async await



所以,我废弃了第一个相机捕获项目,开始一个新的,并做了版本
有简单的thread.sleep延迟,以显示asynch / await的概念。



现在,我添加了对项目的引用,并手动编辑以添加

 < PropertyGroup> 
< TargetPlatformVersion> 8.0< / TargetPlatformVersion>
< / PropertyGroup>

公开参考文档会根据第一个链接公开Windows / Core,然后访问Windows。媒体,Windows.Storage。我只添加了一些关于CameraCaptureUI和CaptureFileAsync的代码只是一个起点,如...

  private async void Camera1()
{
var cameraUi = new Windows.Media.Capture.CameraCaptureUI();
var capturedMedia = await cameraUi.CaptureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.Video);

if(capturedMedia == null)
return;

MessageBox.Show(Valid Camera);
}

并获得关于以下内容的编译错误:



'await'要求类型
'Windows.Foundation.IAsyncOperation'
具有合适的GetAwaiter方法。
是否缺少System的using指令?



此外,通过

  async void Camera2()
{
CameraCaptureUI dialog = new CameraCaptureUI();
Windows.Foundation.Size aspectRatio = new Windows.Foundation.Size(16,9);
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if(file == null)
return;

MessageBox.Show(Valid Storage File Captured);
}



我还有System.Runtime和System.RunTime.WindowsRuntime作为但仍然无法编译。



我缺少什么。我知道不同版本之间的事情,如Windows 8.1和Windows 10,以及功能/库的升级,但为什么await工作一种方式,而不是另一个。

解决方案

对于那些像我一样的头脑,我终于遇到了一个缺少链接的帖子...



如何在我的WPF项目中导入windows.media.capture?



区别在于

 < TargetPlatformVersion> ; 8.1< / TargetPlatformVersion> 

而不是8.0。





一旦更改为8.1并识别WINDOWS dll,所有的工作与基础,媒体等的单独的.dll引用。尊重媒体管理器或CameraCaptureUI选项。


hope its something simple stupid, but I am spinning my wheels.

I have a Surface Pro 4, Windows 10 and using Visual Studio 2013 Professional.
Developing WPF using C# 4.5.

In summary, all I am trying to do a simple camera capture to save an image without resorting to other 3rd party libraries I have no control over. The rest of this post are details of other research findings that I HAVE looked into and tried working out and what has failed and what such message from the compiler.

Again, simple camera, capture picture, save to disk, but all the async-await options appear to throw compiler errors.

EDIT 1 SAMPLE CODE

Here is code from the WPF form. I do not even have any control in the form as I can not even get the 'await' to compile.

using System;
using Windows.Media.Capture;
using System.Windows;

namespace CameraCapture
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            JustDoIt();
        }

        private MediaCapture _mediaManager;
        private async void JustDoIt()
        {
            //initialize mediacapture with default camera
            _mediaManager = new MediaCapture();
            await _mediaManager.InitializeAsync();

            if (_mediaManager == null)
                MessageBox.Show("Failed Initialize");

            await _mediaManager.StartPreviewAsync();
        }
    }
}

And my project also has per other links researched from below, the dlls for

System.Runtime.dll   and
System.Runtime.WindowsRuntime.dll

Compile error for each of the 'await'

Error   1   'await' requires that the type 'Windows.Foundation.IAsyncAction' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

and I have "using System;" as the first line. Is there some other "await" going on when using WinRT vs default System include?

END OF EDIT

I started with https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.cameracaptureui.aspx CameraCaptureUI class

I then found this link https://www.eternalcoding.com/?p=183 How to use specific WinRT API from Desktop apps I tried going through all the steps as outlined and getting errors associated with

await ... have a suitable GetAwaiter method.

So, I looked up about asynch and await and came across

how and when to use `async` and `await` how and when to use async and await.

So, I scrapped the first camera capture project, started a new, and did that version that has simple thread.sleep delay to show the concept of asynch / await. That part works.

So now, I add back the reference to the project and manually edit to add the

  <PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
  </PropertyGroup>

to expose the References expose the Windows / Core per the first link and then getting access to the Windows.Media, Windows.Storage. I add in just the first little bit of code about the CameraCaptureUI and CaptureFileAsync as just a starting point such as...

private async void Camera1()
{
    var cameraUi = new Windows.Media.Capture.CameraCaptureUI();
    var capturedMedia = await cameraUi.CaptureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.Video);

    if (capturedMedia == null)
        return;

    MessageBox.Show("Valid Camera");
}

and get a compile error about:

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

Also, back to the original Windows version attempt via

private async void Camera2()
{
    CameraCaptureUI dialog = new CameraCaptureUI();
    Windows.Foundation.Size aspectRatio = new Windows.Foundation.Size(16, 9);
    dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file == null)
        return;

    MessageBox.Show("Valid Storage File Captured");
}

I even have System.Runtime and System.RunTime.WindowsRuntime as references to the project but still fail on compile.

What am I missing. I know things change between different versions such as Windows 8.1 and Windows 10, and upgrades to features / libraries, but why await works one way, but not another.

解决方案

For those scratching their heads as I did, I finally came across another post that had a missing link...

How can i import windows.media.capture in my WPF project?

The difference was the

<TargetPlatformVersion>8.1</TargetPlatformVersion>

instead of 8.0.

8.1 showed many of the individual .dll references of Foundation, Media, etc but not the single "Windows" dll.

Once changed to 8.1 and recognized the WINDOWS dll, all worked with respect to the media manager or CameraCaptureUI options.

这篇关于用Windows 10,WinRT拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆