Java BufferedImage to Byte Array image2Bytes(File f)

Here you can find the source of image2Bytes(File f)

Description

image Bytes

License

Apache License

Declaration

public static byte[] image2Bytes(File f) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.imageio.ImageIO;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;

public class Main {
    public static byte[] image2Bytes(File f) throws Exception {
        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);
    }/*from   w ww  .  j  ava 2  s . c  om*/
}

Related

  1. getBytes(final InputStream is, final int bufferSize)
  2. getBytesFromBufferedImage(BufferedImage bi, String format)
  3. getBytesFromImage(String fileIn, String fileOut)
  4. getBytesPerPixel(int bufferedImageType)
  5. image2Bytes(BufferedImage image)
  6. imageToByte(BufferedImage bi, String format)
  7. imageToByte(BufferedImage image)
  8. imageToByte(BufferedImage img)
  9. imageToByteArray(BufferedImage o)