Java processing Pictures--zooming, rotating and mosaics of images

Source: Internet
Author: User

This is my own combination of some information on the Web package Java image processing class, support image scaling, rotation, mosaic. (Reproduced Please specify Source: http://blog.csdn.net/u012116457)
Not much to say, on the code:

 PackageDealImportJava.awt.Color;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;ImportJava.awt.Image;ImportJava.awt.geom.AffineTransform;ImportJava.awt.image.AffineTransformOp;ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJavax.imageio.ImageIO;/** * Image processing class. * * @author NAGSH * * * * Public  class imagedeal {String OpenUrl;//Original picture open pathString Saveurl;//New diagram save pathString Savename;//New map nameString suffix;//New diagram type only supports Gif,jpg,png     Public Imagedeal(String openUrl, String Saveurl, String savename, string suffix) { This. openUrl = OPENURL; This. savename = Savename; This. Saveurl = Saveurl; This. suffix = suffix; }/** * Image zoom. * * @param Width * required height * @param Heights * required * @throw S Exception * /     Public void Zoom(intWidthintHeightthrowsException {DoubleSX =0.0;DoubleSY =0.0; File File =NewFile (OPENURL);if(!file.isfile ()) {Throw NewException ("Imagedeal>>>"+ file +"Not a picture File!"); } bufferedimage bi = imageio.read (file);//Read the picture        //Calculates the x-axis y-axis scaling-scale, if necessary, to ensure that the parameter width and height are proportional to the change before callingSX = (Double) Width/bi.getwidth (); SY = (Double) Height/bi.getheight (); Affinetransformop op =NewAFFINETRANSFORMOP (affinetransform.getscaleinstance (SX, SY),NULL); File SF =NewFile (Saveurl, Savename +"."+ suffix); Image zoomimage = Op.filter (BI,NULL);Try{imageio.write (bufferedimage) zoomimage, suffix, sf);//Save picture}Catch(Exception e)        {E.printstacktrace (); }    }/** * Rotation * * @param degree * Rotation angle * @throws Exception * *     Public void Spin(intdegree)throwsException {intSwidth =0;//Width after rotation        intSheight =0;//Height after rotation        intX//Origin Point horizontal        intY//Origin ordinateFile File =NewFile (OPENURL);if(!file.isfile ()) {Throw NewException ("Imagedeal>>>"+ file +"Not a picture File!"); } bufferedimage bi = imageio.read (file);//Read the picture        //processing angle--determines the rotational radiandegree = degree% the;if(Degree <0) degree = the+ degree;//convert angle to 0-360 degrees        Doubletheta = Math.toradians (degree);//Convert angle to radians        //Determine the width and height after rotation        if(Degree = = the|| degree = =0|| degree = = the) {swidth = Bi.getwidth ();        Sheight = Bi.getheight (); }Else if(Degree = = -|| degree = = the) {sheight = Bi.getwidth ();        Swidth = Bi.getheight (); }Else{Swidth = (int) (Math.sqrt (Bi.getwidth () * bi.getwidth () + bi.getheight () * Bi.getheight ())); Sheight = (int) (Math.sqrt (Bi.getwidth () * bi.getwidth () + bi.getheight () * Bi.getheight ())); } x = (Swidth/2)-(Bi.getwidth ()/2);//Determine origin coordinatesy = (Sheight/2)-(Bi.getheight ()/2); BufferedImage Spinimage =NewBufferedImage (Swidth, Sheight, Bi.gettype ());//Set picture background colorgraphics2d GS = (graphics2d) spinimage.getgraphics ();        Gs.setcolor (Color.White); Gs.fillrect (0,0, Swidth, sheight);//Draw the background of the rotated picture in a given colorAffineTransform at =NewAffineTransform (); At.rotate (theta, Swidth/2, Sheight/2);//Rotate imagesAt.translate (x, y); Affinetransformop op =NewAffinetransformop (at, affinetransformop.type_bicubic);        Spinimage = Op.filter (bi, spinimage); File SF =NewFile (Saveurl, Savename +"."+ suffix); Imageio.write (spinimage, suffix, sf);//Save picture}/** * Mosaic. * @param size mosaic size, that is, the length of each rectangle * @return * @throws Exception */     Public Boolean Mosaic(intSizethrowsException {File File =NewFile (OPENURL);if(!file.isfile ()) {Throw NewException ("Imagedeal>>>"+ file +"Not a picture File!"); } bufferedimage bi = imageio.read (file);//Read the pictureBufferedImage Spinimage =NewBufferedImage (Bi.getwidth (), Bi.getheight (), Bi. TYPE_INT_RGB);if(Bi.getwidth () < size | | bi.getheight () < size | | size <=0) {//Mosaic grid size is too large or too small            return false; }intXcount =0;//Direction Draw number        intYcount =0;//Y-direction Draw number        if(Bi.getwidth ()% size = =0) {Xcount = Bi.getwidth ()/size; }Else{xcount = Bi.getwidth ()/size +1; }if(Bi.getheight ()% size = =0) {Ycount = Bi.getheight ()/size; }Else{ycount = Bi.getheight ()/size +1; }intx =0;//coordinates        inty =0;//Draw mosaics (draw rectangles and fill colors)Graphics GS = Spinimage.getgraphics (); for(inti =0; i < Xcount; i++) { for(intj =0; J < Ycount; J + +) {//Mosaic rectangular lattice size                 intMwidth = size;intMheight = size;if(i==xcount-1){//Landscape The last one is more special, probably not enough a sizeMwidth = Bi.getwidth ()-X; }if(j = = ycount-1){//similarlyMheight =bi.getheight ()-y; }//Rectangle color takes center pixel RGB value                intCenterX = x;intCenterY = y;if(Mwidth%2==0) {CenterX + = mwidth/2; }Else{CenterX + = (Mwidth-1) /2; }if(Mheight%2==0) {CenterY + = mheight/2; }Else{CenterY + = (Mheight-1) /2; } Color color =NewColor (Bi.getrgb (CenterX, centery));                Gs.setcolor (color);                Gs.fillrect (x, Y, Mwidth, mheight); y = y + size;//calculates the y-coordinate of the next rectangle} y =0;//restore y-coordinatex = x + size;//Calculate x-coordinate} gs.dispose (); File SF =NewFile (Saveurl, Savename +"."+ suffix); Imageio.write (spinimage, suffix, sf);//Save picture        return true; } Public Static void Main(string[] args)throwsException {Imagedeal Imagedeal =NewImagedeal ("E://1.jpg","e://","2","JPG");//Test scaling        /* Imagedeal.zoom ($); */        //test rotation        /* Imagedeal.spin (*);        //test mosaic        /*imagedeal.mosaic (4); * *}}

Java processing Pictures--zooming, rotating and mosaics of images

Related Article

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.