Processing images in java-scaling, rotating, and mosaic Images

Source: Internet
Author: User

Processing images in java-scaling, rotating, and mosaic Images

This is a java image processing class encapsulated by some data on the Internet. It supports image scaling, rotation, and mosaic. (Reprinted please indicate the source: http://blog.csdn.net/u012116457)
Not to mention, the above Code:

Package deal; import java. awt. color; import java. awt. graphics; import java. awt. graphics2D; import java. awt. image; import java. awt. geom. affineTransform; import java. awt. image. affineTransformOp; import java. awt. image. bufferedImage; import java. io. file; import javax. imageio. imageIO;/*** image processing class. ** @ author nagsh **/public class ImageDeal {String openUrl; // the original image open path String saveUrl; // The New Image save path String saveName; // New image name String suffix; // The new image type only supports gif, jpg, and png public ImageDeal (String openUrl, String saveUrl, String saveName, String suffix) {this. openUrl = openUrl; this. saveName = saveName; this. saveUrl = saveUrl; this. suffix = suffix;}/*** resize the image. ** @ param width * required width * @ param height * required height * @ throws Exception */public void zoom (int width, int height) throws Exception {double sx = 0.0; double sy = 0.0; File file = new File (openUrl); if (! File. isFile () {throw new Exception ("ImageDeal >>>" + file + "is not an image file! ");} BufferedImage bi = ImageIO. read (file); // read the image. // calculate the x-axis Y-axis scaling ratio. If necessary, perform proportional scaling, before calling the function, make sure that the width and height values of the parameter are the same as the ratio of sx = (double) width/bi. getWidth (); sy = (double) height/bi. getHeight (); AffineTransformOp op = new AffineTransformOp (AffineTransform. getScaleInstance (sx, sy), null); File sf = new File (saveUrl, saveName + ". "+ suffix); Image zoomImage = op. filter (bi, null); try {ImageIO. write (BufferedIm Age) zoomImage, suffix, sf); // Save the image} catch (Exception e) {e. printStackTrace () ;}/ *** rotate ** @ param degree * Rotation Angle * @ throws Exception */public void spin (int degree) throws Exception {int swidth = 0; // The width of the rotated int sheight = 0; // The height of the rotated int x; // the abscissa int y of the origin; // The ordinate File file of the origin point = new File (openUrl ); if (! File. isFile () {throw new Exception ("ImageDeal >>>" + file + "is not an image file! ");} BufferedImage bi = ImageIO. read (file); // read the image // processing angle -- determine the rotation radians degree = degree % 360; if (degree <0) degree = 360 + degree; // convert the angle to double theta = Math. toRadians (degree); // converts degrees to radians // determines the width and height of the rotated if (degree = 180 | degree = 0 | degree = 360) {swidth = bi. getWidth (); sheight = bi. getHeight ();} else if (degree = 90 | degree = 270) {sheight = bi. getWidth (); swidth = bi. getHei Ght ();} 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 the origin coordinate y = (sheight/2)-(bi. getHeight ()/2); BufferedImage spinImage = new BufferedImage (swidth, sheight, bi. getType (); // set the image background color Graphics2D gs = (Graphics2D) spinImage. getGraphics (); gs. setColor (Color. white); gs. fillRect (0, 0, swidth, sheight); // draws the background AffineTransform at = new AffineTransform ();. rotate (theta, swidth/2, sheight/2); // rotate the image. translate (x, y); AffineTransformOp op = new AffineTransformOp (at, AffineTransformOp. TYPE_BICUBIC); spinImage = op. filter (bi, spinImage); File sf = new File (saveUrl, saveName + ". "+ suf Fix); ImageIO. write (spinImage, suffix, sf); // Save the image}/*** mosaic. * @ param size: mosaic size, that is, the length and width of each rectangle * @ return * @ throws Exception */public boolean mosaic (int size) throws Exception {File file = new File (openUrl ); if (! File. isFile () {throw new Exception ("ImageDeal >>>" + file + "is not an image file! ");} BufferedImage bi = ImageIO. read (file); // read the image BufferedImage spinImage = new BufferedImage (bi. getWidth (), bi. getHeight (), bi. TYPE_INT_RGB); if (bi. getWidth () <size | bi. getHeight () <size | size <= 0) {// return false if the size of the mosaic lattice is too large or too small;} int xcount = 0; // Number of direction plots int ycount = 0; // number of records drawn in the y direction 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;} int x = 0; // coordinate int y = 0; // draw Mosaic (draw rectangle and fill color) Graphics gs = spinImage. getGraphics (); for (int I = 0; I <xcount; I ++) {for (int j = 0; j <ycount; j ++) {// mosaic rectangle lattice size int mwidth = size; int mheight = size; if (I = xcount-1) {// The last one is special horizontally, it may be less than one size mwidth = bi. getWidth ()-x;} if (j = ycount-1) {// Similarly, mheight = bi. getHeight ()-y;} // obtain the RGB value of the center pixel from the rectangle. int centerX = x; int centerY = 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 = new Color (bi. getRGB (centerX, centerY); gs. setColor (color); gs. fillRect (x, y, mwidth, mheight); y = y + size; // calculate the y coordinate of the next rectangle} y = 0; // restore y coordinate x = x + size; // calculate x coordinate} gs. dispose (); File sf = new File (saveUrl, saveName + ". "+ suffix); ImageIO. write (spinImage, suffix, sf); // Save the image return true;} public static void main (String [] args) throws Exception {ImageDeal imageDeal = new ImageDeal ("e: // 1.jpg", "e: //", "2", "jpg"); // test scaling/* imageDeal. zoom (200,300); * // test rotation/* imageDeal. spin (90); * // test mosaic/* imageDeal. mosaic (4 );*/}}

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.