Java Picture Processing

Source: Internet
Author: User

In Java, image I/O (that is, read pictures and write pictures, does not involve complex image processing) A class contains static convenient positioning methods Imagereaders and Imagewriters, and simple encoding and decoding.

    • Basic code

      /*File f1 = new File("Z:\\java\\1.png");    BufferedImage image = ImageIO.read(f1);    File f2 = new File("Z:\\java\\2.tiff");    ImageIO.write(image, "png", f2);*///对应输入图片的格式
    • Parameterized code

    @Test//Get Input Output Picture format public void F1 () {String readformats[] = Imageio.getreaderformatnames ();        String writeformats[] = Imageio.getwriterformatnames ();        System.out.println (Arrays.aslist (readformats));    System.out.println (Arrays.aslist (writeformats)); } @Test public void F2 () {iterator<imagereader> Iterator = imageio.getimagereadersbyformatname ("pn    G ");  }/** * Crop the picture and save the new picture * @param Srcpath Read the source picture path * @param topath write picture path * @param x Cut start point x coordinate * @param y-Cut starting point y-coordinate * @param width clipping width * @param height cut height * @param readimageformat Read picture format * @param            Writeimageformat Write Picture format * @throws IOException */public void Cropimage (String srcpath,string Topath,           int x,int y,int width,int height, String readimageformat,string writeimageformat) throws ioexception{        FileInputStream FIS = null;        Imageinputstream IIS =null; try{//Read picture file FIS = new FileInputStream (Srcpath);             Iterator<imagereader> it = imageio.getimagereadersbyformatname (Readimageformat);             ImageReader reader = (imagereader) it.next ();              Get Picture Stream IIS = Imageio.createimageinputstream (FIS);            Reader.setinput (iis,true);             Imagereadparam param = Reader.getdefaultreadparam ();             Defines a rectangle Rectangle rect = new Rectangle (x, y, width, height);             Provides a bufferedimage that is used as the target for decoding pixel data.            Param.setsourceregion (rect);                            BufferedImage bi = reader.read (0,param);             Save new picture Imageio.write (BI, Writeimageformat, new File (Topath));                   }finally{if (fis!=null) fis.close ();         if (iis!=null) iis.close ();     }     }

Java picture processing

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.