0
votes

I am facing an issue with Windows Computer Vision API. If I send a request with contentType = application/json and image URL in JSON request body things work fine but on sending a binary image(base 64 encoded) with contentType = application/octet-stream it gives me ImageFormatInvalid in the response.

Any help is much appreciated. Thank you!

Code:

final String binaryData = "data:image/jpeg;base64, /9............
ByteArrayEntity requestEntity = new ByteArrayEntity(binaryData.getBytes(),
ContentType.APPLICATION_OCTET_STREAM);

request.setEntity(requestEntity);            
HttpResponse response = httpClient.execute(request);            
HttpEntity entity = response.getEntity();

Response: InvalidImageFormat

1
Not sure without seeing all the code, but I found in C# I had to strip out the "data:image/jpeg;base64, " It is in C# but here is my working example, might help - jimpaine
@jimpaine Can you share the link to your working example? - varunjain
For quick reference see the method that starts at line 143 - jimpaine
Hey @jimpaine, Thank you for sharing :) I have a similar thing done in java here, but for some reason, it is not working - varunjain

1 Answers

0
votes

It seems I was generating the binary incorrectly. Worked when made changes as below:

byte[] decodedString = Base64.decodeBase64(binaryData);
ByteArrayEntity requestEntity = new 
ByteArrayEntity(decodedString,ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(requestEntity);