通过Redis从Cpp应用程序发送到Rust应用程序的编码图像,无效的UTF-8- TypeError

eqoofvh9  于 11个月前  发布在  Redis
关注(0)|答案(1)|浏览(89)

我从Cpp应用程序通过redis pubsub发送一个编码的cv图像:

cv::Mat output_image;
output_image = cv::Mat(pic_height, pic_width * 2, CV_8UC3);
cv::imwrite(filename, output_image);
std::vector<uchar> encoded_image;
cv::imencode(".png", output_image, encoded_image);
std::string image_str(encoded_image.begin(), encoded_image.end());
string image_msg;
image_msg = to_string(pic_id) + "~" + metadata + "~" + image_str;
RedisCommunicator::GetInstance()->Publish(image_channel, image_msg);

字符串
我正在使用redis-rsRust应用程序中接收图像:

let mut redis_pubsub = redis_connection.as_pubsub();

redis_pubsub.subscribe(image_channel).expect("Failed to subscribe to channel");
info!("subscribed to channel: {}", image_channel);

redis_pubsub.set_read_timeout(Some(Duration::from_secs(5))).expect("Failed to set read timeout");

for _ in 0..3 {
    info!("###### Waiting for message...");
    let log = match redis_pubsub.get_message(){
        Ok(T) => T,
        Err(e) => {info!("Failed to get message: {}", e); continue;},
    };
    let payload : String = match log.get_payload() {
        Ok(T) => T,
        Err(e) => {info!("Failed to get payload: {}", e);continue;},
    };
    if log_enabled!(Level::Debug) {
        debug!("Got log from channel: '{}', log: {}", log.get_channel_name(), payload);
    }
    info!("{{\"{}\": {}}}", image_channel, payload);
    sleep(Duration::from_secs(1));
};


我在Rust中收到一个格式错误。

[rusty_record] subscribed to channel: image_sender
[rusty_record] ###### Waiting for message...
[rusty_record] Failed to get payload: Invalid UTF-8- TypeError
[rusty_record] ###### Waiting for message...
[rusty_record] Failed to get payload: Invalid UTF-8- TypeError
[rusty_record] ###### Waiting for message...
[rusty_record] Failed to get payload: Invalid UTF-8- TypeError


查看redis-cli,在pubsub通道中有消息:

redis-cli subscribe image_sender

1) "message"
2) "image_sender"
3) "3707~{\"pic_gain\":0,\"pic_exposure\":0,\"pic_time\":0,\"camera_id\":0,\"latitude\":0.000000,\"longitude\":0.000000,\"altitude\":0.000000,\"heading_degrees\":0.000000,\"auto_white_balance\":true}~\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x02\x01\x01\x01\x01\x01\x02\x01\x01\x01\x02\x02\x02\x02\x02\x04\x03\x02\x02\x02\x02\x05\x04\x04\x03\x04\x06\x05\x06\x06\x06\x05\x06\x06\x06\a\t\b\x06\a\t\a\x06\x06\b\x0b\b\t\n\n\n\n\n\x06\b\x0b\x0c\x0b\n\x0c\t\n\n\n\xff\xdb\x00C\x01\x02\x02\x02\x02\x02\x02\x05\x03\x03\x05\n\a\x06\a\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\xff\xc0\x00\x11\b\x00\xc0\x05\xa0\x03\x01\"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\a\"q\x142\x81\x91\xa1\b#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\...

如果我发送一个常规字符串,它工作正常。

Cpp:

string image_msg;
image_msg = to_string(pic_id) + "~" + metadata + "~" + "image_str";
RedisCommunicator::GetInstance()->Publish(image_channel, image_msg);


我在Rust中接收消息。:

[rusty_record] subscribed to channel: image_sender
[rusty_record] ###### Waiting for message...
[rusty_record] Got log from channel: 'image_handler_0', log: 7812~{"pic_gain":0,"pic_exposure":0,"pic_time":0,"camera_id":0,"latitude":0.000000,"longitude":0.000000,"altitude":0.000000,"heading_degrees":0.000000,"auto_white_balance":true}~image_str
[rusty_record] {"image_sender": 7812~{"pic_gain":0,"pic_exposure":0,"pic_time":0,"camera_id":0,"latitude":0.000000,"longitude":0.000000,"altitude":0.000000,"heading_degrees":0.000000,"auto_white_balance":true}~image_str}
[rusty_record] ###### Waiting for message...
[rusty_record] Got log from channel: 'image_handler_0', log: 7813~{"pic_gain":0,"pic_exposure":0,"pic_time":0,"camera_id":0,"latitude":0.000000,"longitude":0.000000,"altitude":0.000000,"heading_degrees":0.000000,"auto_white_balance":true}~image_str
[rusty_record] {"image_sender": 7813~{"pic_gain":0,"pic_exposure":0,"pic_time":0,"camera_id":0,"latitude":0.000000,"longitude":0.000000,"altitude":0.000000,"heading_degrees":0.000000,"auto_white_balance":true}~image_str}
[rusty_record] ###### Waiting for message...
[rusty_record] Got log from channel: 'image_handler_0', log: 7814~{"pic_gain":0,"pic_exposure":0,"pic_time":0,"camera_id":0,"latitude":0.000000,"longitude":0.000000,"altitude":0.000000,"heading_degrees":0.000000,"auto_white_balance":true}~image_str
[rusty_record] {"image_sender": 7814~{"pic_gain":0,"pic_exposure":0,"pic_time":0,"camera_id":0,"latitude":0.000000,"longitude":0.000000,"altitude":0.000000,"heading_degrees":0.000000,"auto_white_balance":true}~image_str}

**Rust仍然是新手,如果有一些新手的错误,请原谅我。

yks3o0rb

yks3o0rb1#

Rust中的String应该是UTF-8编码的 text。你应该使用Vec<u8>来处理像你的图像这样的二进制数据。大多数“字符串”操作函数也存在于字节片&[u8],以及解引用字节片的类型,比如Vec<u8>,因此,您可以像解析String一样轻松地解析Vec<u8>(例如使用splitn),并将包含文本的部分转换为str::from_utf8

let mut redis_pubsub = redis_connection.as_pubsub();

redis_pubsub.subscribe(image_channel).expect("Failed to subscribe to channel");
info!("subscribed to channel: {}", image_channel);

redis_pubsub.set_read_timeout(Some(Duration::from_secs(5))).expect("Failed to set read timeout");

for _ in 0..3 {
    info!("###### Waiting for message...");
    let log = match redis_pubsub.get_message(){
        Ok(T) => T,
        Err(e) => {info!("Failed to get message: {}", e); continue;},
    };
    let payload : Vec<u8> = match log.get_payload() {
        Ok(T) => T,
        Err(e) => {info!("Failed to get payload: {}", e);continue;},
    };
    if log_enabled!(Level::Debug) {
        debug!("Got log from channel: '{}', log: {}", log.get_channel_name(), payload);
    }
    let parts: Vec<_> = payload.splitn (3, |&c| c == b'~').collect();

    info!("id: {:?}", str::from_utf8 (parts[0]));
    info!("metadata: {:?}", str::from_utf8 (parts[1]));
    info!("data: {:?}", parts[2]);
    sleep(Duration::from_secs(1));
};

字符串

相关问题