在Windows8Javascript中关闭相机的正确方法是什么

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

本文关键字:方法 是什么 相机 Windows8Javascript      更新时间:2024-04-06

我正在javascript中使用MediaCapture来捕获我的相机
我有一个带有initCamera函数的Camera类。问题是,如果我试图在短时间内重新初始化我的相机,我会得到这个错误:Hardware MFT failed to start streaming due to lack of hardware resources.

现在我知道这意味着我的相机还在使用。我想知道的是:

  • 如何正确关闭相机
  • 如何检查我的相机是否正在使用或不可用

这里有一段代码:

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();
        });
    }
};

当前重新打开相机的方法是用camera类的新实例覆盖相机实例。

提前谢谢。

我在C#中使用MediaCapture时遇到了同样的问题。

我不得不在StopPreviewAsync之后致电Dispose()以更正它:

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

您看过相机入门套件UWP样本吗?它也有JS风格!

如果你想在使用完相机后不久就能可靠地访问它,你需要确保你正确地清理了所有资源。从你共享的代码来看,你似乎让系统来处理这件事,这意味着你的应用程序可能会在系统关闭所有资源之前返回。

你应该注意:

  • 停止任何可能正在进行的录制
  • 停止预览
  • 关闭MediaCapture

看看我上面链接的示例中的cleanupCameraAsync()方法,了解如何实现它。