在Mqtt代理上发布图像数据 [英] Publish image data on Mqtt broker

查看:1182
本文介绍了在Mqtt代理上发布图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过mqtt代理将图像字节数组发送给订户.但是图像字节数组数据的大小太大,无法在mqtt代理上发布,那么如何将图像字节数组数据发送给订户?

I want to send the image byte array to the subscriber through the mqtt broker. But the size of the image byte array data is too big to be published on the mqtt broker, so how can I send the image byte array data to the subscriber?

PicBitmap = ((BitmapDrawable)iVpic.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PicBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
picbyte = bos.toByteArray();

String s = Base64.encodeToString(picbyte,Base64.DEFAULT);
String pichex = toHexString(s);

String payload = pichex;
byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes("UTF-8");
        MqttMessage message = new MqttMessage(encodedPayload);
        message.setQos(qos);
        mqttClient.publish(topic, message);
    } catch (UnsupportedEncodingException | MqttException e) {
        e.printStackTrace();
    }

public static String toHexString(String input) {
    return String.format("%x", new BigInteger(1, input.getBytes()));
}

我需要先将字节数组转换为字符串和十六进制ascii代码,然后才在代理上发布.但是从字节Arary转换的字符串太长,每次我尝试发布时都无法发布.

I need to convert the byte array to string and hex ascii code first then only publish on the broker. But the string converted from byte arary is too long, it failed to be published everytime i try to publish it.

推荐答案

  1. 我不认为您的实际图像大于MQTT消息的最大有效负载大小256mb.一个12兆像素的jpeg大约只有3.5mb,无损的PNG大约是7mb. ()

对图像进行Base64编码将增加它的大小约为1/3.因为MQTT消息只是字节流,所以不需要这样做,因此不需要对图像进行编码. (仍然只有9.3mb)

Base64 encoding the images will increase it's size by approximately 1/3. This is not needed as MQTT messages are just a stream of bytes so there is no need to encode the image. (this would still be only 9.3mb)

您的toHexString()函数只是将您的有效载荷加倍,没有任何好处. base64编码的输出已经是一个字符串,将表示该字符串的每个字节(以1个字符表示)转换为2个字符(在0-9a-f范围内)没有任何用处. (仍然只有18.6mb)

Your toHexString() function is just doubling your payload for no benefit. The output of the base64 encode is already a string, converting each of the bytes (represented by 1 char) that represents that string into 2 chars (in the range 0-9a-f) does nothing useful. (still only 18.6mb)

如果您仍然有问题,我会编辑掉所有不需要的代码,并提出一个新问题,并包括e.printStackTrace()输出中的stacktrace,以便我们了解真正的问题是什么.

I have edited out all the unneeded code, if you still have a problem ask a new question and include the stacktrace from the e.printStackTrace() output so we can see what the real issue is.

PicBitmap = ((BitmapDrawable)iVpic.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PicBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
picbyte = bos.toByteArray();

try {
    MqttMessage message = new MqttMessage(picbyte);
    message.setQos(qos);
    mqttClient.publish(topic, message);
} catch (MqttException e) {
    e.printStackTrace();
}

这篇关于在Mqtt代理上发布图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆