1

I have requirement to pass an image in json request and I am using javax.Base64 encoder.

I am able to encode the image to Base64 but I found that mime type "data:image/png;base64" is missing in generate encoded String.

So, my encoded String looks like below :

iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAAAAAACIqegAAABMEl**********

And, here is my simple code which I am trying with junit :

@Test
    public void getBiographicPanel() {
        byte[] image = bacodeGenerator.generateBarocdeImage("12345678");
        System.out.println(Base64.getEncoder().encodeToString(image));
        System.out.println(org.apache.commons.codec.binary.Base64.encodeBase64String(image));

        assertNotNull(image);
    }

How, is that any way or input parameter present in Base64 API which can we configure to generate Base64 image content with MimeType like below ?

data:image/png;base64,iVBORw0KGgoAAAANS

Gunjan Shah
  • 4,409
  • 11
  • 45
  • 68
  • 1
    I don't think it's the job of the encoder to add the MIME type – LppEdd Mar 15 '19 at 13:23
  • But if you will refer the questions and example on stackoverflow, most of the users has mentioned String with this header. So, I assuming that there must be some encoder which provides this meta data. In worst case, I need to append the meta data as prefix explicitly. – Gunjan Shah Mar 15 '19 at 13:27
  • Could you link those questions? The MIME type is set prior to calling the remote endpoint. – LppEdd Mar 15 '19 at 13:28
  • I was referring this question : https://stackoverflow.com/questions/11546917/sending-a-byte-array-in-json-using-jackson . But I may be wrong. I may need append it manually. – Gunjan Shah Mar 15 '19 at 13:41

1 Answers1

3

The MIME type is

a two-part identifier for file formats and format contents transmitted on the Internet.

That means if you don't need to exchange this file via internet protocols (e.g. HTTP), that type is absolutely irrelevant.


You're also confusing

data:image/png;base64

with a MIME type. That is not a media type.

data: is an actual URL format and is used to specify inline data inside the browser (see IETF).
In your case it would mean "hey browser! Look at this local resource encoded in Base64 to build-up the image!".

This is tipically used in <img /> tags, and must be set manually. That means you have to know the actual format of the data.

LppEdd
  • 16,731
  • 6
  • 53
  • 100
  • Thanks for clarification. I got little confused due to some block on net. In my case, I am sending a json Map to remote service. And the value of map may be a String or a Base64 encoded image. So basically, I need a workaround to determine weather the the value is a String or Image on the remote service. Here, I think only way to add prefix like "image/png;base64" to Base64 encoded String which we can check on remote service to determine that the value is an image. – Gunjan Shah Mar 15 '19 at 13:57
  • @GunjanShah the most correct way of carrying a MIME type is via HTTP headers (`Content-Type`). Will this map carry only the encoded/standard string, or other data also? If it has a mixture of data, than yes, you need to manually prefix the encoded string. You could also create a custom object format to hold the data and the format of the data `{ "data": "...", "format": "image/png;base64" }` – LppEdd Mar 15 '19 at 14:04
  • yup, this map contains mixture of data. It may have String and byte[] type values. And I can not request format as request contract is fixed to call third party web services. So, I think, I can only ask them to check the prefix of the image. – Gunjan Shah Mar 15 '19 at 14:12
  • @GunjanShah consider using a custom data format, like the one I specified above, for your `Map` values – LppEdd Mar 15 '19 at 14:13
  • I got another solution. We can use URLConnection.guessContentTypeFromStream(bin) function. This method will return null if I pass input stream of normal String. And it return exact image type "PNG" if I pass input stream of an image. So I can use these of remote service to check if the contents are image or not. – Gunjan Shah Mar 15 '19 at 14:48
  • @GunjanShah but you just said the request body content is mixed. So that won't work. – LppEdd Mar 15 '19 at 15:18
  • yup, Request body content will be mixture of String and Object. But I tested URLConnection class. It returns null if input param is String or something other than image . And It returns exact image type if I pass Base64 encoded image. So it will work for me. By the way, your answer helped me to clear my misunderstanding about meta data prefix in Base64 encoded image. I am marking this response as Correct. Thank you ! – Gunjan Shah Mar 17 '19 at 14:43
  • @GunjanShah thanks for the update! Good to know for the future! – LppEdd Mar 17 '19 at 14:48