Java cropped picture saved to the specified location
/*** Picture Clipping Universal Interface * *@paramsrc source picture address, image format PNG *@paramdest Destination Image address *@paramx Image starting point x coordinate *@paramY Picture starting point y coordinate *@paramW Picture Width *@paramh Picture Height *@throwsIOException Exception Handling*/ Public voidCutimage (string src, String dest,intXintYintWinth) {Try{ //get the PNG image of the ImageReader iteratorIterator Iterator = imageio.getimagereadersbyformatname ("PNG"); //get ImageReader according to iteratorImageReader reader =(ImageReader) iterator.next (); //get source picture input streamInputStream in =NewFileInputStream (SRC); //get Imageinputstream stream based on source picture input streamImageinputstream IIS =Imageio.createimageinputstream (in); //load the Imageinputstream stream into the ImageReaderReader.setinput (IIS,true); //Picture Reading ParametersImagereadparam param =Reader.getdefaultreadparam (); Rectangle rect=NewRectangle (x, Y, W, h); //Parameter objects set a rectangle of a certain sizeparam.setsourceregion (rect); //ImageReader get bufferedimage based on parameter objectBufferedImage bi = reader.read (0, param); //writes a picture stream filtered by a parameter object to the destination fileImageio.write (BI, "PNG",NewFile (dest)); }Catch(IOException e) {System.err.println ("Crop picture failed"); } }
Source
Java cropping pictures