Java BufferedImage to Byte Array getBytesFromImage(String fileIn, String fileOut)

Here you can find the source of getBytesFromImage(String fileIn, String fileOut)

Description

get Bytes From Image

License

Open Source License

Declaration

public static byte[] getBytesFromImage(String fileIn, String fileOut) 

Method Source Code

//package com.java2s;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;

import javax.imageio.ImageIO;

public class Main {
    public static byte[] getBytesFromImage(String fileIn, String fileOut) {
        byte[] photo = null;
        try {//from   w  w w  .  jav a2 s  . co m

            File file = new File(fileIn);
            BufferedImage imagem = ImageIO.read(file);
            int new_w = 320, new_h = 240;
            BufferedImage new_img = new BufferedImage(new_w, new_h, BufferedImage.SCALE_DEFAULT);
            Graphics2D g = new_img.createGraphics();
            g.drawImage(imagem, 0, 0, new_w, new_h, null);
            File out = new File(fileOut);
            ImageIO.write(new_img, "JPG", out);

            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            FileInputStream fi = new FileInputStream(fileOut);
            byte buffer[] = new byte[fi.available()];
            int bytesRead = 0;
            while ((bytesRead = fi.read(buffer)) != -1) {
                arrayOutputStream.write(buffer, 0, bytesRead);
            }
            arrayOutputStream.close();

            photo = arrayOutputStream.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return photo;
    }

    public static byte[] getBytesFromImage(String fileIn, String fileOut, int height, int width) {
        byte[] photo = null;
        try {

            File file = new File(fileIn);
            BufferedImage imagem = ImageIO.read(file);
            int new_w = width, new_h = height;
            BufferedImage new_img = new BufferedImage(new_w, new_h, BufferedImage.SCALE_DEFAULT);
            Graphics2D g = new_img.createGraphics();
            g.drawImage(imagem, 0, 0, new_w, new_h, null);
            File out = new File(fileOut);
            ImageIO.write(new_img, "JPG", out);

            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            FileInputStream fi = new FileInputStream(fileOut);
            byte buffer[] = new byte[fi.available()];
            int bytesRead = 0;
            while ((bytesRead = fi.read(buffer)) != -1) {
                arrayOutputStream.write(buffer, 0, bytesRead);
            }
            arrayOutputStream.close();

            photo = arrayOutputStream.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return photo;
    }
}

Related

  1. getByteBufferToImage(ByteBuffer buffer, int width, int height)
  2. getByteImage(BufferedImage imagem)
  3. getBytes(BufferedImage image)
  4. getBytes(final InputStream is, final int bufferSize)
  5. getBytesFromBufferedImage(BufferedImage bi, String format)
  6. getBytesPerPixel(int bufferedImageType)
  7. image2Bytes(BufferedImage image)
  8. image2Bytes(File f)
  9. imageToByte(BufferedImage bi, String format)