我正在使用Raspberry Pi的PyCamera模块捕获图像并将其存储为.jpg
。
首先使用base64.encodestring()对图像进行编码。但是在将编码后的字符串发送到PubNub服务器时,我在my_publish_callback
上收到错误
('ERROR: ', 'Expecting value: line 1 column 1 (char 0)')
('ERROR: ', JSONDecodeError('Expecting value: line 1 column 1 (char 0)',))
我尝试使用base64.b64encode(),但仍然遇到相同的错误。我已经在python 2和3中尝试了该脚本;
def my_publish_callback(envelope, status):
if not status.is_error():
pass # Message successfully published to specified channel.
else:
#print("recv: ", envelope)
print("ERROR: ", status.error_data.information)
print("ERROR: ", status.error_data.exception)
def publish(channel, msg):
pubnub.publish().channel(channel).message(msg).async(my_publish_callback)
def captureAndSendImage():
camera.start_preview()
time.sleep(2)
camera.capture("/home/pi/Desktop/image.jpg")
camera.stop_preview()
with open("/home/pi/Desktop/image.jpg", "rb") as f:
encoded = base64.encodestring(f.read())
publish(myChannel, str(encoded))
我无法找到或打印完整的错误回溯,因此我可以获得有关错误发生位置的更多线索。但是看来PubNub试图解析JSON中的数据,但失败了。
答案 0 :(得分:1)
我意识到.jpg文件的大小为154KB,而PubNub最大数据包的大小为32KB,因此应该清楚地说明所有内容。 PubNub建议发送大型消息,方法是将它们拆分并在订户端重新安排。感谢@Craig引用该链接,尽管它通过support.pubnub.com/support/discussions/topics/14000006326