This article is a Java image processing class encapsulated by the author in combination with some information on the Internet, supporting the scaling, rotation and mosaicization of images.
Without further ado, the 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; // The new image name String suffix; // The new image 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 scaling. * * @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 + " Not an image file!"); } BufferedImage bi = ImageIO.read(file); // Read the image// Calculate the x-axis y-axis scaling ratio-If equal-scale scaling is required, make sure that the parameters width and height are equal-scale changes before calling = (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((BufferedImage) zoomImage, suffix, sf); // Save the image} catch (Exception e) { e.printStackTrace(); } } /** * Rotate* * @param degree * Rotate angle* @throws Exception */ public void spin(int degree) throws Exception { int swidth = 0; // width after rotation int sheight = 0; // height after rotation int x; // origin horizontal coordinate int y; // origin vertical coordinate File file = new File(openUrl); if (!file.isFile()) { throw new Exception("ImageDeal>>>" + file + " Not an image file!"); } BufferedImage bi = ImageIO.read(file); // Read the image// Process angle-determine rotation degree = degree % 360; if (degree < 0) degree = 360 + degree;// Convert the angle to between 0-360 degrees double theta = Math.toRadians(degree);// Convert the angle to radians// Determine the width and height after rotation if (degree == 180 || degree == 0 || degree == 360) { swidth = bi.getWidth(); sheight = bi.getHeight(); } else if (degree == 90 || degree == 270) { 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())); } x = (swidth / 2) - (bi.getWidth() / 2);// Determine the origin coordinate y = (shight / 2) - (bi.getHeight() / 2); BufferedImage spinImage = new BufferedImage(swidth, sheight, bi.getType()); // Set the background color of the image Graphics2D gs = (Graphics2D) spinImage.getGraphics(); gs.setColor(Color.white); gs.fillRect(0, 0, swidth, sheight); // Draw the background of the rotated image in a given color AffineTransform at = new AffineTransform(); at.rotate(theta, swidth / 2, sheight / 2);// Rotate image at.translate(x, y); AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC); spinImage = op.filter(bi, spinImage); File sf = new File(saveUrl, saveName + "." + suffix); ImageIO.write(spinImage, suffix, sf); // Save the image} /** * Mosaicization. * @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 + " 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) { // The size of the mosaic grid is too large or too small returns false; } int xcount = 0; // The number of direction is drawn int ycount = 0; // The number of direction is drawn in ycount 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; //Coordinates int y = 0; //Draw mosaics (draw rectangles and fill colors) Graphics gs = spinImage.getGraphics(); for (int i = 0; i < xcount; i++) { for (int j = 0; j < ycount; j++) { //Mosaic rectangle size int mwidth = size; int mheight = size; if(i==xcount-1){ //The last one in the horizontal direction is special, and may not be enough for one size mwidth = bi.getWidth()-x; } if(j == ycount-1){ //Similarly mheight =bi.getHeight()-y; } // The RGB value of the rectangle color takes the center pixel 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 coordinates of the next rectangle} y = 0;// Restore the y coordinates x = x + size;// Calculate the x coordinates} 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);*/ }} The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.