【问题标题】:Flask-SocketIO to update image dynamicallyFlask-SocketIO 动态更新图像
【发布时间】:2021-12-09 07:10:56
【问题描述】:

我正在尝试让我的服务器定期使用 flask-socketio 发送图像。我正在使用以下代码发送图像

app.py

with open(f'{app.static_folder}\\image.jpg', ) as f:
        img = f.read()

socketio.emit('my_response',
             {'data': 'Server generated event', 'count': count,
              'image': img})

test.js

socket.on('my_response', function(msg) {     

    let arrayBufferView = new Uint8Array(msg['image']);

    console.log(arrayBufferView);

    var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
    var img_url = URL.createObjectURL(blob);
    console.log(img_url);
    $("#img_cam").attr("src", img_url);
});

这不是为我更新图像。我看到使用 socketio 发送了正确的数据,并且数据也正好在 Uint8Array 之后。

如果我在 html 页面上创建一个按钮并将更新图像行与文件一起使用,它就可以正常工作。

如何获取发送过来的图像并更新图像源?

编辑:修复了我在 html 文件中拼写错误的 img_cam 的问题。发布的此代码有效。

【问题讨论】:

  • 请将问题标记为已解决。

标签: jquery flask flask-socketio


【解决方案1】:

通过修复我的 html 文件解决了我的问题。

html文件和js文件需要匹配的id。更新图像的代码有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2016-04-19
    • 2012-11-13
    相关资源
    最近更新 更多