Read and write picture file source _ read and write picture file

Source: Internet
Author: User
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 ();
 }
	}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.