Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

base64 - Publish image data on Mqtt broker

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()));
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. I do not believe your actual image is bigger than 256mb the max payload size for a MQTT message. A 12 megapixel jpeg is only about 3.5mb, a lossless PNG will be about 7mb. (source)

  2. 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)

  3. 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)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...