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

2024-09-19 23:43:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个电子应用程序,它使用socketio将图像从相机中间件程序传输到GUI。这是可行的,但问题是,每当一个新的图像进入GUI冻结约2秒。我在electron应用程序的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


Tags: 图像imgreaddatapngaswithopen