-2

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.

hardillb
  • 54,545
  • 11
  • 67
  • 105
Ray T
  • 9
  • 1
  • 2
  • 1
    How big an image are you trying to publish? MQTT payloads can be up to 256mb. Also what have you already tried? – hardillb Oct 12 '18 at 16:30
  • I need to publish the string of the image byte array, so the string will be very long, is the long string may cause it to be failed to publish on the broker? – Ray T Oct 13 '18 at 04:55
  • If you have actual errors add those to the question as well rather than guess what the problem is – hardillb Oct 13 '18 at 07:16
  • Base64 encoding will only make the payload bigger, just send the actual bytes – hardillb Oct 13 '18 at 07:17
  • It didn't show the actual errors, so I didn't know what is the problem that has cause the error. – Ray T Oct 13 '18 at 07:42
  • Image data is not the only data that I need to publish, there is other data that need put together with the image data in the payload and published at the same time. So how to put the byte array into the string payload? – Ray T Oct 13 '18 at 07:53
  • is there a solution in objective c ? – Marin May 17 '20 at 18:27

1 Answers1

4
  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();
}
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Convert to hex ASCII code is the requirements of the project, and image data is not the only data that need to put in the mqtt message, there is still other data that need to publish together with the image data, so it need to have a string payload. – Ray T Oct 13 '18 at 08:23
  • Then remove the base64 encoding, doing both is meaningless and wastes space, but as I said it is VERY unlikely you have exceeded the max message size – hardillb Oct 13 '18 at 08:26
  • but i need to put the image data into the string payload with other data, so it need to be converted into string. – Ray T Oct 13 '18 at 08:28