Package Com.cqhope.read;import Java.awt.image.bufferedimage;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import java.io.file;import Java.io.ioexception;import javax.imageio.ImageIO;/** * Manipulate picture file classes. * @author Yanghaitao */public class Opimage {public static void main (string[] args) {opimage images = new Opimage (); String fileName = "c:/users/administrator/desktop/head/1407489615ttjb.jpg"; byte[] bytes = images.readimage (filename) ; System.out.print (bytes.length); Images.outimage (bytes, "c:/users/administrator/desktop/123.jpg", "jpg"); /** * Enter picture file. * @param fileName Picture file full path * @return return picture data */public byte[] readimage (String filename) {byte[] bytes = NULL; File F = new file (fileName); BufferedImage bi = null; Bytearrayoutputstream BAOs = null;try {bi = Imageio.read (f); BAOs = new Bytearrayoutputstream (); Imageio.write (bi, "JPG", b AOS); bytes = Baos.tobytearray ();} catch (IOException e) {e.printstacktrace ();} finally {if (BAOs! = null) {try {baos.close ();} catch (IOException e){//TODO auto-generated catch Blocke.printstacktrace ();}}} return bytes;} /** * Output picture file. * @param bytes Picture data * @param filename output filename * @param type output file types * @throws ioexception */public void outimage (byte[] bytes , string fileName, String type) {//sets the default file format. if (type = = NULL | | "". Equals (Type)) {type = "JPG";} try {bytearrayinputstream Bais = new Bytearrayinputstream (bytes); BufferedImage bi1 =imageio.read (Bais); File W2 = new file (fileName); Imageio.write (Bi1, type, w2);} catch (Exception e) {e.printstacktrace ();}}}
Read and write image file source