The error "TypeError: Cannot read properties of undefined (reading 'drawImage')" occurs because the this.$refs.canvas is undefined in the Paint method.

To fix this error, make sure that you are referencing the correct element using $refs in the mounted method. Also, ensure that the canvas element has the ref attribute set to "canvas".

Here's an updated code snippet:

mounted() {
    const canvas = document.getElementById('video-canvas');
    const url = 'ws://localhost:9990';
    this.player = new JSMpeg.Player(url, {
        canvas: canvas
    });
    players.on('error', function(error) {
        console.error(error);
        setTimeout(function() {
            players.destroy();
            players = new JSMpeg.Player(urls, {
                canvas: canvas
            });
        }, 1000);
    });
    this.video = canvas.getContext('2d');
},
methods: {
    Paint() {
        // 构建识别请求数据
        const canvas = document.getElementById('video-canvas');
        var recognitionUrl = 'http://192.168.31.51:8189/ocr/prediction';

        // 获取canvas中当前帧的画面
        this.video.drawImage(this.player.canvas, 0, 0, canvas.width, canvas.height);

        // 将画面转化为base64编码赋值给imageData
        const imageData = canvas.toDataURL();
    }
}

In the mounted method, this.$refs.canvas is replaced with canvas when initializing this.video. And in the Paint method, this.$refs.canvas is replaced with document.getElementById('video-canvas')

mounted const canvas = documentgetElementByIdvideo-canvas; const url = wslocalhost9990; thisplayer = new JSMpegPlayerurl canvas canvas ; playersonerror functionerror co

原文地址: http://www.cveoy.top/t/topic/ieEc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录