Here you can find the source of getBytesFromImage(String fileIn, String fileOut)
public static byte[] getBytesFromImage(String fileIn, String fileOut)
//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; } }