简体   繁体   English

在Windows 8 Javascript中关闭相机的正确方法是什么?

[英]What is the proper way to close a camera in Windows 8 Javascript?

I'm using MediaCapture in javascript to capture my camera. 我在javascript中使用MediaCapture捕捉我的相机。
I have a Camera class with an initCamera function. 我有一个带有initCamera函数的Camera类。 The problem is, if I try to re-init my camera in a short time period I will get this error: Hardware MFT failed to start streaming due to lack of hardware resources. 问题是,如果我尝试在短时间内重新启动我的相机,我将收到此错误: Hardware MFT failed to start streaming due to lack of hardware resources.

Now I get that this means my camera is still in use. 现在我知道这意味着我的相机仍在使用中。 The thing I want to know is: 我想知道的是:

  • How do I properly close my camera 如何正确关闭相机
  • How do I check if my camera is in use or unavailable 如何检查相机是否正在使用或不可用

Here is a piece of code: 这是一段代码:

function Camera() {
    var that = this;              
    this.mediaCaptureElement = null;

    this.initCamera = function() {
        if (!that.mediaCaptureElement) {
        that.mediaCaptureElement = new Windows.Media.Capture.MediaCapture();

        that.mediaCaptureElement.addEventListener("failed", function (e) {
            console.warn("The camera has stopped working");
        }

        that.mediaCaptureElement.initializeAsync().then(function() {
            that.mediaCaptureElement.videoDeviceController.primaryUse = Windows.Media.Devices.CaptureUse.photo;
            that.getCameraResolution();
            that.orientationChanged();
            that.startCamera();
        });
    }
};

The way I re-open my camera currently is by overwriting the camera instance with a new instance of the Camera class. 我当前重新打开相机的方法是使用Camera类的新实例覆盖相机实例。

Thanks in advance. 提前致谢。

I had the same problem using MediaCapture in C# . 我在C#使用MediaCapture时遇到了同样的问题。

I had to call Dispose() after StopPreviewAsync in order to correct it : 我必须在StopPreviewAsync之后调用Dispose()才能纠正它:

await cameraControler.MediaCaptureInstance.StopPreviewAsync(); cameraControler.MediaCaptureInstance.Dispose();

Have you seen the Camera Starter Kit UWP sample ? 您是否看过相机入门套件UWP样品 It comes in a JS flavor too! 它也有JS风味!

If you want to be able to reliably access the camera shortly after being done using it, you need to make sure you're cleaning up all resources properly. 如果您希望在完成相机使用后能够可靠地访问相机,则需要确保正确清理所有资源。 From the code that you've shared, it seems like you're letting the system take care of this, which means your app might be coming back before the system is done closing out all resources. 从您共享的代码中,您似乎正在让系统处理此问题,这意味着您的应用可能会在系统完成所有资源之前回来。

You should take care of: 你应该照顾:

  • Stop any recordings that may be in progress 停止可能正在进行的任何录制
  • Stop the preview 停止预览
  • Close the MediaCapture 关闭MediaCapture

Have a look at the cleanupCameraAsync() method from the sample I linked above for an example on how to implement this. 看看我上面链接的示例中的cleanupCameraAsync()方法 ,以获取有关如何实现它的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关闭PhotoEditorSDK ReactUI的正确方法是什么? - What is the proper way to close PhotoEditorSDK ReactUI? 使用neo4j-javascript-driver关闭会话的正确方法是什么? - What is the proper way to close a session using neo4j-javascript-driver? 为Javascript函数转义HTML的正确方法是什么? - What is a proper way to escape HTML for Javascript function? 在Javascript中包含模块的正确方法是什么? - What is the proper way to include modules in Javascript? 格式化JavaScript标记的正确方法是什么? - What is the proper way to format a JavaScript tag? 在 javascript 中导入 webassembly 模块的正确方法是什么 - What is the proper way to import webassembly module in javascript 在javascript中控制相关对象的正确方法是什么? - What is the proper way to control related objects in javascript? 关闭JavaScript中承诺的连接的最干净的方法是什么 - What is the cleanest way to close promised connection in javascript 在 JavaScript 中创建类 onclick 的新实例的正确方法是什么? - What is the proper way of creating a new instance of a class onclick in JavaScript? 将HTML添加到容器的末尾:使用JavaScript执行此操作的正确方法是什么? - Adding HTML to the end of a container: What is the proper way to do it using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM