Read and write image file source

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;/** * 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

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.