Java image scaling and cutting:
- PackageAction;
- ImportJava. AWT. graphics;
- ImportJava. AWT. image;
- ImportJava. AWT. toolkit;
- ImportJava. AWT. image. bufferedimage;
- ImportJava. AWT. image. cropimagefilter;
- ImportJava. AWT. image. filteredimagesource;
- ImportJava. AWT. image. imagefilter;
- ImportJava. Io. file;
- ImportJava. Io. fileoutputstream;
- ImportJava. Io. ioexception;
- ImportJavax. ImageIO. ImageIO;
- ImportCom.sun.image.codec.jpeg. Unzip codec;
- ImportCom.sun.image.codec.jpeg. Specify imageencoder;
- Public ClassImageprocess {
- /**
- * Scale the image
- *
- * @ Param srcimgfilename
- * @ Throws ioexception
- */
- Public VoidZoomimage (string srcimgfilename)ThrowsIoexception
{
- // Read the file
- File _ file =NewFile (srcimgfilename );
- // Construct an image object
- Bufferedimage src = javax. ImageIO. ImageIO. Read (_ file );
- IntWidth = SRC. getwidth ();
- IntHeight = SRC. getheight ();
- // The length of the side is reduced to 1/2.
- Bufferedimage tag =NewBufferedimage (width/2, height/2,
Bufferedimage. type_int_rgb );
- // Draw the reduced Graph
- Tag. getgraphics (). drawimage (SRC, 0, 0, width/2,
Height/2,Null);
- Fileoutputstream out =NewFileoutputstream ("D: \ test1 \ targetIMG1-4.jpg ");
- Required imageencoder encoder = required codec. createjpegencoder (out );
- Encoder. encode (TAG );
- Out. Close ();
- // The side length is doubled.
- Tag =NewBufferedimage (width * 2, height * 2,
Bufferedimage. type_int_rgb );
- Tag. getgraphics (). drawimage (SRC, 0, 0, width * 2,
Height * 2,Null);
- Out =NewFileoutputstream ("D: \ test1 \ targetimgx2.jpg ");
- Encoder = export codec. createjpegencoder (out );
- Encoder. encode (TAG );
- Out. Close ();
- }
- /**
- * Divide an image into nine parts.
- *
- * @ Param srcimagefile
- * @ Throws ioexception
- */
- Public VoidCut (string srcimagefile)ThrowsIoexception
{
- Image IMG;
- Imagefilter cropfilter;
- String dir =Null;
- // Read the source Image
- Bufferedimage src = ImageIO. Read (NewFile (srcimagefile ));
- IntDestwidth = SRC. getwidth ()/3;
- IntDestheight = SRC. getheight ()/3;
- // Loop
- For(IntI = 0;
I <3; I ++ ){
- For(IntJ = 0;
J <3; j ++ ){
- // The four parameters are the coordinates of the image start point and width and height.
- Cropfilter =NewCropimagefilter (J * destwidth, I * destheight, destwidth, destheight );
- IMG = toolkit. getdefatooltoolkit (). createimage (NewFilteredimagesource (SRC. getsource (), cropfilter ));
- Bufferedimage tag =NewBufferedimage (destwidth, destheight, bufferedimage. type_int_rgb );
- Graphics G = tag. getgraphics ();
- G. drawimage (IMG, 0, 0,Null);//
Draw a thumbnail
- G. Dispose ();
- // Output as a file
- Dir = "D: \ test1 \ cut_image _" + I + "_" + J + ". jpg ";
- File F =NewFile (DIR );
- ImageIO. Write (TAG, "Jpeg", F );
- }
- }
- }
- Public Static VoidMain (string []
ARGs)ThrowsIoexception {
- String imgfilename = "D: \ test \ test.png ";
- Imageprocess izoom =NewImageprocess ();
- Izoom. zoomimage (imgfilename );
- Izoom. Cut (imgfilename );
- }
- }