Java Utililty Methods BufferedImage to Byte Array

List of utility methods to do BufferedImage to Byte Array

Description

The list of methods to do BufferedImage to Byte Array are organized into topic(s).

Method

byte[]image2Bytes(BufferedImage image)
image Bytes
return ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
byte[]image2Bytes(File f)
image Bytes
BufferedImage bi = ImageIO.read(f);
int imageType = bi.getType();
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(bi, grayImage);
return (byte[]) grayImage.getData().getDataElements(0, 0, width, height, null);
byte[]imageToByte(BufferedImage bi, String format)
image To Byte
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, format, baos);
return baos.toByteArray();
byte[]imageToByte(BufferedImage image)
image to byte
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "png", out);
try {
    return out.toByteArray();
} finally {
    if (out != null) {
        out.close();
byte[]imageToByte(BufferedImage img)
Conversion d'image vers Byte
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = null;
try {
    ImageIO.write(img, "jpg", baos);
    baos.flush();
    bytes = baos.toByteArray();
} catch (IOException e) {
    e.printStackTrace();
...
byte[]imageToByteArray(BufferedImage o)
Ein byte-Array (BLOB) aus einem Image erzeugen
if (o != null) {
    BufferedImage image = (BufferedImage) o;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        boolean converted = ImageIO.write(image, "jpeg", baos);
        if (!converted) {
        byte[] b = baos.toByteArray();
...
byte[]imageToBytes(BufferedImage image, String encoding)
Converts an image to byte buffer representing PNG (bytes as they would exist on disk)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, encoding, baos);
return baos.toByteArray();
byte[]imageToBytes(BufferedImage image, String imageFormat)
image To Bytes
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageWriter writer = ImageIO.getImageWritersByFormatName(imageFormat).next();
ImageWriteParam param = getParams(writer);
writer.setOutput(new MemoryCacheImageOutputStream(stream));
writer.write(null, new IIOImage(image, null, null), param);
stream.flush();
byte[] imageInByte = stream.toByteArray();
stream.close();
...
byte[]imageToBytes(BufferedImage img, String formatName)
Returns the image into an array of bytes.
byte[] imgBytes = null;
ByteArrayOutputStream stream = null;
try {
    stream = new ByteArrayOutputStream();
    ImageIO.write(img, formatName, stream);
    stream.flush();
    imgBytes = stream.toByteArray();
} catch (IOException e) {
...
byte[]toArrayByte(BufferedImage image)
to Array Byte
byte[] bytes = new byte[] {};
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    baos.flush();
    bytes = baos.toByteArray();
    baos.close();
} catch (IOException e) {
...