问题
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?
回答1:
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();
回答2:
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();
}
来源:https://stackoverflow.com/questions/32851036/converting-png-byte-array-to-jpeg-byte-array-in-java