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.
@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