我是靠谱客的博主 如意丝袜,最近开发中收集的这篇文章主要介绍java websocket传json,使用WebSocket将图像作为字节发送JSON,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I want to send and receive image from cv2.Videocapture using WebSocket.

It could get json, but it couldn't decoded.

We need result that can be opened using cv2.imshow().

Somebody help me...

This is Client

ret, image_np = cap.read()

IMAGE_SHAPE = image_np.shape

encoded_image = base64.b64encode(image_np)

print(type(encoded_image))

payload = {

'from': 'rasp',

'image': str(encoded_image),

'shape': IMAGE_SHAPE,

}

data = json.dumps(payload)

try:

# Send encoded image data.

await websocket.send(data)

# receive server message

received_data = await websocket.recv()

print('< {}'.format(received_data))

# image = base64.b64decode(received_data)

# np_image = np.fromstring(image, dtype=np.uint8)

# source = np_image.reshape(IMAGE_SHAPE)

return websocket

except Exception:

print('WebSocket send or receive error.')

exit(1)

This is Server

async def server_on(websocket, path):

payload = {

'from': 'image-server',

# 'result': {

# # Default is null.

# 'isPerson': ,

# 'centerPoint': ,

# },

}

data = json.dumps(payload)

try:

payload = await websocket.recv()

receive_data = json.loads(payload)

# At this line doesnt work...

decoded_image = base64.b64decode(receive_data['image'])

image_np = np.fromstring(decoded_image, dtype=np.uint8)

source = image_np.reshape(receive_data['shape'])

await websocket.send(data)

except Exception:

websocket.close()

return

解决方案

In your Client I would say that you have an extra operation not needed.

Based on your latest comment, you might not need to use: str(encoded_image).

You could try to use: base64.encodestring(image_np) that will send you back a string container.

ret, image_np = cap.read()

IMAGE_SHAPE = image_np.shape

encoded_image = base64.encodestring(image_np)

print(type(encoded_image))

payload = {

'from': 'rasp',

'image': encoded_image.decode('utf-8'),

'shape': IMAGE_SHAPE,

}

最后

以上就是如意丝袜为你收集整理的java websocket传json,使用WebSocket将图像作为字节发送JSON的全部内容,希望文章能够帮你解决java websocket传json,使用WebSocket将图像作为字节发送JSON所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部