我試圖讓我的服務器定期使用flask-socketio發送影像。我正在使用以下代碼發送影像
應用程式
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})
測驗.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 的問題。發布的此代碼有效。
uj5u.com熱心網友回復:
通過修復我的 html 檔案解決了我的問題。
需要在 html 檔案和 js 檔案之間匹配的 id。更新影像的代碼有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339953.html
