[英]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:
我想知道的是:
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: 你应该照顾:
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.