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;
/** * Operation picture File class. * @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", BAOs);
bytes = Baos.tobytearray (); The catch (IOException e) {e.Printstacktrace ();
finally {if (BAOs!= null) {try {baos.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}} return bytes;
/** * Output picture file. * @param bytes Picture data * @param filename output file name * @param type output File type * @throws ioexception/public void Outimage
Te[] Bytes, string fileName, String type) {//Set 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 ();
}
}
}