通过套接字io发送4k图像会导致javascript冻结一秒钟

时间:2019-11-27 00:55:58

标签: javascript python sockets socket.io electron

我正在开发一个电子应用程序,该应用程序使用套接字io将图像从相机中间件程序传递到GUI。这是可行的,但问题是,每次GUI中出现新映像时,冻结约2秒钟。我在电子应用程序的main.js中使用的代码如下:

socket.on("frame", data => {
        console.log("GOT FRAME");
        mainWindow.webContents.send("fodImage", data);
        if (cameraWindow) cameraWindow.webContents.send("fodImage", data);
    });

socketio检测到该消息时似乎发生了挂起,因为如果我注释掉函数中的所有内容,则gui仍然挂起。

python客户端代码如下:



@sio.event
def getImage(cameraId):
    print(cameraId)
    print(f"Received GetFrame command for camera {cameraId}")

    frameBytes = None
    if cameraId % 4 == 0:
        with open("../data/fod1.PNG", "rb") as img:
            frameBytes = img.read()
    elif cameraId % 4 == 1:
        with open("../data/fod2.PNG", "rb") as img:
            frameBytes = img.read()
    elif cameraId % 4 == 2:
        with open("../data/fod3.PNG", "rb") as img:
            frameBytes = img.read()
    elif cameraId % 4 == 3:
        with open("../data/fod4.PNG", "rb") as img:
            frameBytes = img.read()

    # libcam = ctypes.CDLL('libcam.dll')
    # framesize = 4144*2822*3
    # framebytes = (ctypes.c_char * framesize)()
    # cameraID = 0
    # libcam.GetFrame(cameraID, framebytes, framesize)
    # framebytes = base64.b64encode(framebytes)

    sio.emit('frame', frameBytes)

有什么办法可以阻止这种行为?还是仅由于图像非常大而引起?图片大约有16MB ...

0 个答案:

没有答案