0

I am not sure what I am exactly doing here so please give some advise and forgive the mistakes.

I have a image byte[] called idCardImage and I did the following to convert it to a String:

String s = new String(idCardImage);

And it prints out like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAEHUlEQVQ4TzWUW49VRRCFv6rq3ufMmTNn....

Did some search online and it seems this image is in a png format with base 64 encoded.

What I need to do is to convert it to a jpeg format and then store it in a new byte array. Can you give some advise on how to do it with Java?

5
  • actually, that's a data uri, which happens to contain a base64-encoded png image. Commented Sep 29, 2015 at 18:16
  • 1
    mkyong.com/java/convert-png-to-jpeg-image-file-in-java Commented Sep 29, 2015 at 18:19
  • @Pshemo I actually saw that link but I don't have any jpg file, I only have a byte array. Commented Sep 29, 2015 at 18:21
  • @MarcB I think you are right, please correct me if I am wrong, the png data is after base64,, correct? Commented Sep 29, 2015 at 18:22
  • yes. the iVB... stuff is the base64 data. no idea if java has a data uri parsing library (probably does), but if nothing else, you can simply use a string operation to extract the b64 data and then load that into a b64-decoder Commented Sep 29, 2015 at 18:23

2 Answers 2

5

You can use ImageIO as interpreter to convert images in java. It allows to read image data, as same as write image data into specified format. It requires an InputStream to read image data. since you have a byte[] you can create a ByteArrayInputStream easily.

ImageIO requires an OutputStream to write byte data. And also ByteArrayOutputStream allows to extract it's byte[]. So you can write image bytes into a ByteArrayOutputStream and return its's bytes.

Consider following example

public byte[] pngBytesToJpgBytes(byte[] pngBytes) throws IOException {
    //create InputStream for ImageIO using png byte[]
    ByteArrayInputStream bais = new ByteArrayInputStream(pngBytes);
    //read png bytes as an image
    BufferedImage bufferedImage = ImageIO.read(bais);
    //create OutputStream to write prepaired jpg bytes
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //write image as jpg bytes
    ImageIO.write(bufferedImage, "JPG", baos);
    //convert OutputStream to a byte[]
    return baos.toByteArray();
}
Sign up to request clarification or add additional context in comments.

1 Comment

ImageIO.write(source, format, output) makes things a lot easier +1
2

I am not sure if that is most efficient way, but you could:

1) convert data from that string to bytes representing image, like:

String dataUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAEHUlEQVQ4TzWUW49VRRCFv6rq3ufMmTNn...";
String header = "data:image/png;base64";

String encodedImage = dataUrl.substring(header.length()+1); //+1 to include comma 
byte[] imageData = Base64.getDecoder().decode(encodedImage); //decode bytes

2) convert that bytes to BufferedImage holding PNG image

BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageData));

3) then based on http://www.mkyong.com/java/convert-png-to-jpeg-image-file-in-java/ you could create separate BufferedImage and fill it using JPG fromat

 // create a blank, RGB, same width and height, and a white background
 BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
        bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
 newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

(this step can be simplified with ImageIO.write(source, format, output) as shown in @Channa Jayamuni answer)

4) finally we can write these bytes to separate byte array with little help of ByteArrayOutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(newBufferedImage, "jpg",baos);
byte[] byteArray = baos.toByteArray();

1 Comment

I am actually on Java 7 so I replaced the Base64.getDecoder().decode(encodedImage); with DatatypeConverter.parseBase64Binary(encodedImage); and it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.