简体   繁体   English

ImageCapture.takePhoto 抛出 DOMException:平台错误

[英]ImageCapture.takePhoto throws DOMException: platform error

Im trying to use the takePhoto method of image capture, using the following code.我正在尝试使用以下代码使用图像捕获的 takePhoto 方法。

addPicture: function (typeEvidence) {
    if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
        this.typeEvidence = typeEvidence;
        var _self = this;
        this.initializeCamera();
        navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } })
            .then(mediaStream => {
                _self.video.srcObject = mediaStream;
                _self.video.onloadedmetadata = () => {
                    _self.video.play();
                };
                _self.track = mediaStream.getVideoTracks()[0];
                _self.imageCapture = new ImageCapture(_self.track);
            })
            .catch((err) => {
                $.MenssagesGrowl.Show(3, 'Error: ' + err.name + ' - ' + err.message);
            });
        $("#modalAddPicture").modal("show");
    }
    else {
        $.MenssagesGrowl.Show(3, 'The browser does not support media devices');
    }
},    
initializeCamera: function () {
        var _self = this;
        this.dataURL = '';
        this.disableTakePicture = true;
        this.blob = null;
        this.streaming = false;
        this.imageCapture= null;
        this.video = document.querySelector('video');
        this.canvas = document.getElementById('cameraCanvas');
        this.video.addEventListener('canplay', (ev) => {
            if (!_self.streaming) {
    
                _self.picWidth = _self.video.videoWidth;
                _self.picHeight = _self.video.videoHeight;
                //_self.picWidth = _self.video.videoWidth / (_self.video.videoHeight / _self.picHeight);
    
                // Firefox currently has a bug where the height can't be read from
                // the video, so we will make assumptions if this happens.
    
                if (isNaN(_self.picWidth)) {
                    _self.picWidth = _self.picHeight / (4 / 3);
                }
    
                _self.video.setAttribute('width', _self.picWidth);
                _self.video.setAttribute('height', _self.picHeight);
                _self.canvas.setAttribute('width', _self.picWidth);
                _self.canvas.setAttribute('height', _self.picHeight);
    
                _self.streaming = true;
                _self.disableTakePicture = false;
            }
        }, false);
        this.clearPhoto();
    },
    takePicture: function () {
        var _self = this;
        this.imageCapture.takePhoto(null)
            .then((blob) => {
                return Promise.all([createImageBitmap(blob),blob]);
            })
            .then((result) => {
                //result[0] bitmap
                //result[1] blob
                //guardar foto
                var _sequence = _self.getConsecutiveForFileName();
                var _filename = _self.PrevioHeader.ControlNum + "_" + _self.PreviousConsignment.ConsignmentNum + "_" + _sequence + ".png";
                var _file = new File([result[1]], _filename, {
                    type: 'image/png'
                });
                var _formData = new FormData();
                _formData.append("image", _file);
                _formData.append("id", _self.PreviousConsignment.Guid);
                axios({
                    method: 'post',
                    url: this.url,
                    data: _formData,
                    headers: { "Content-Type": "multipart/form-data" }
                }
                ).then((response) => {
                    if (response.data.result == true) {
                        $.MenssagesGrowl.Show(response.data.typeMessage, response.data.message);
                        var _context = _self.canvas.getContext('2d');
                        if (_self.picWidth && _self.picHeight) {
                            _self.canvas.width = _self.picWidth;
                            _self.canvas.height = _self.picHeight;
                            _context.drawImage(result[0], 0, 0, _self.picWidth, _self.picHeight);
                            var _dataURL = _self.canvas.toDataURL('image/png');
                            _self.PreviousConsignment.Images.push({
                                dataURL: _dataURL,
                                fileName: _filename,
                                id: response.data.id,
                                sequence: _sequence,
                                typeEvidence: _self.typeEvidence,
                                temporal: 1
                            });
                        }
                        else
                            _self.clearPhoto();
                    }
                });
            })
            .catch((error) => {
                console.log(error);
            });
    },

The application was working ok with the following line该应用程序在以下行中运行正常

this.imageCapture.takePhoto() this.imageCapture.takePhoto()

, but suddenly stopped working today throwing a generic error DOMException: platform error . ,但今天突然停止工作并抛出一般错误DOMException: platform error I did a little bit of research and realized they had made a change a year ago to use it as我做了一些研究,意识到他们在一年前进行了更改,将其用作

takePhoto(photoSettings)拍照(照片设置)

as the documentation in mdn web docs says, since photoSettings is optional i tried to use正如mdn web 文档中的文档所说,由于 photoSettings 是可选的,我尝试使用

this.imageCapture.takePhoto(null) this.imageCapture.takePhoto(空)

and it worked for a while until later this day, when it started throwing again the same error.它工作了一段时间,直到今天晚些时候,当它再次开始抛出同样的错误时。 Does anyone knows the reason, is the ImageCapture not stable for production and if it is not is there an alternative or workaround?有谁知道原因,ImageCapture 在生产中是否不稳定?如果不是,是否有替代方案或解决方法?

Im running the app under chrome and using vue as framework.我在 chrome 下运行应用程序并使用 vue 作为框架。

My customers started getting this error on 1/12/2023.我的客户从 2023 年 1 月 12 日开始收到此错误。 I think Chrome released a version with a bug in the webcam APIs.我认为 Chrome 发布了一个在网络摄像头 API 中存在错误的版本。

I had them update their version of chrome and it immediately started working again.我让他们更新了他们的 chrome 版本,它立即又开始工作了。 The version that fixed it was v109.0.5414.75修复它的版本是 v109.0.5414.75

To update chrome:要更新铬:

更新 Chrome

Version that worked for me:对我有用的版本:

有效的版本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM