Java BufferedImage to Byte Array imageToByte(BufferedImage img)

Here you can find the source of imageToByte(BufferedImage img)

Description

Conversion d'image vers Byte

License

Open Source License

Parameter

Parameter Description
img a parameter

Declaration

public static byte[] imageToByte(BufferedImage img) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main {
    /**/*from w w  w .j  a  va 2 s .  c om*/
     * Conversion d'image vers Byte
     *
     * @param img
     * @return
     */
    public static byte[] imageToByte(BufferedImage img) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = null;
        try {
            ImageIO.write(img, "jpg", baos);
            baos.flush();
            bytes = baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }
}

Related

  1. getBytesPerPixel(int bufferedImageType)
  2. image2Bytes(BufferedImage image)
  3. image2Bytes(File f)
  4. imageToByte(BufferedImage bi, String format)
  5. imageToByte(BufferedImage image)
  6. imageToByteArray(BufferedImage o)
  7. imageToBytes(BufferedImage image, String encoding)
  8. imageToBytes(BufferedImage image, String imageFormat)
  9. imageToBytes(BufferedImage img, String formatName)