【问题标题】:ImageCapture.takePhoto throws DOMException: platform errorImageCapture.takePhoto 抛出 DOMException:平台错误
【发布时间】:2023-02-01 09:11:05
【问题描述】:

我正在尝试使用以下代码使用图像捕获的 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);
            });
    },

该应用程序在以下行中运行正常

this.imageCapture.takePhoto()

,但今天突然停止工作并抛出一般错误DOMException:平台错误. 我做了一些研究,意识到他们在一年前进行了更改,将其用作

拍照(照片设置)

正如mdn web docs 中的文档所说,由于 photoSettings 是可选的,我尝试使用

this.imageCapture.takePhoto(空)

它工作了一段时间,直到今天晚些时候,当它再次开始抛出同样的错误时。 有谁知道原因,ImageCapture 在生产中是否不稳定?如果不是,是否有替代方案或解决方法?

我在 chrome 下运行应用程序并使用 vue 作为框架。

【问题讨论】:

  • 请说明您如何启动 ImageCapture 及其输入 MediaStreamTrack。
  • 完成,是的,我忘记了那部分

标签: javascript webapi image-capture


【解决方案1】:

编辑 2023 年 1 月 31 日:Chrome 现在正在积极解决这个问题: https://bugs.chromium.org/p/chromium/issues/detail?id=1287227

似乎更新 Chrome 并不能解决问题,因为 Chrome 正在测试功能标志MediaFoundationD3D11VideoCapture。禁用MediaFoundationD3D11VideoCapture 时,不会出现此问题。

原始答案 2023 年 1 月 12 日

我的客户从 2023 年 1 月 12 日开始收到此错误。我认为 Chrome 发布了一个在网络摄像头 API 中存在错误的版本。

我让他们更新了他们的 chrome 版本,它立即又开始工作了。修复它的版本是 v109.0.5414.75

要更新铬:

对我有用的版本:

【讨论】:

  • 这不是我第一次遇到了,但这次更持久,难道真的是版本错误吗?
  • 更新 chrome 修复了我的大多数机器。更新后仍有一台机器收到“平台错误”消息。根据源代码,看起来这是失败的行:chromium.googlesource.com/chromium/src/+/…,“photo_state.is_null()”。我希望尽快得到答复。
猜你喜欢
  • 1970-01-01
  • 2018-01-12
  • 2019-09-08
  • 2012-10-12
  • 2016-05-13
  • 2023-03-22
  • 2014-04-03
  • 1970-01-01
  • 2020-02-21
相关资源
最近更新 更多