Here you can find the source of imageToPNG(BufferedImage image)
Parameter | Description |
---|---|
image | a java.awt.image.BufferedImage object. |
public static byte[] imageToPNG(BufferedImage image)
//package com.java2s; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class Main { /**// www . j a va 2 s .c om * Converts an image to a PNG stored in a byte array. * * @param image a {@link java.awt.image.BufferedImage} object. * @return a byte array with the PNG data */ public static byte[] imageToPNG(BufferedImage image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { BufferedImage buffer = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); buffer.createGraphics().drawImage(image, 0, 0, null); ImageIO.write(buffer, "PNG", baos); baos.close(); } catch (IOException e) { // todo: log this instead e.printStackTrace(); } return baos.toByteArray(); } }