本文是作者結合網上的一些資料封裝的java圖片處理類,支持圖片的縮放,旋轉,馬賽克化。
不多說,上代碼:
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;/** * 圖像處理類. * * @author nagsh * */public class ImageDeal { String openUrl; // 原始圖片打開路徑String saveUrl; // 新圖保存路徑String saveName; // 新圖名稱String suffix; // 新圖類型只支持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; } /** * 圖片縮放. * * @param width * 需要的寬度* @param 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 + " 不是一個圖片文件!"); } BufferedImage bi = ImageIO.read(file); // 讀取該圖片// 計算x軸y軸縮放比例--如需等比例縮放,在調用之前確保參數width和height是等比例變化的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((BufferedImage) zoomImage, suffix, sf); // 保存圖片} catch (Exception e) { e.printStackTrace(); } } /** * 旋轉* * @param degree * 旋轉角度* @throws Exception */ public void spin(int degree) throws Exception { int swidth = 0; // 旋轉後的寬度int sheight = 0; // 旋轉後的高度int x; // 原點橫坐標int y; // 原點縱坐標File file = new File(openUrl); if (!file.isFile()) { throw new Exception("ImageDeal>>>" + file + " 不是一個圖片文件!"); } BufferedImage bi = ImageIO.read(file); // 讀取該圖片// 處理角度--確定旋轉弧度degree = degree % 360; if (degree < 0) degree = 360 + degree;// 將角度轉換到0-360度之間double theta = Math.toRadians(degree);// 將角度轉為弧度// 確定旋轉後的寬和高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() * bi.getHeight())); } x = (swidth / 2) - (bi.getWidth() / 2);// 確定原點坐標y = (sheight / 2) - (bi.getHeight() / 2); BufferedImage spinImage = new BufferedImage(swidth, sheight, bi.getType()); // 設置圖片背景顏色Graphics2D gs = (Graphics2D) spinImage.getGraphics(); gs.setColor(Color.white); gs.fillRect(0, 0, swidth, sheight);// 以給定顏色繪製旋轉後圖片的背景AffineTransform at = new AffineTransform(); at.rotate(theta, swidth / 2, sheight / 2);// 旋轉圖像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); // 保存圖片} /** * 馬賽克化. * @param size 馬賽克尺寸,即每個矩形的長寬* @return * @throws Exception */ public boolean mosaic(int size) throws Exception { File file = new File(openUrl); if (!file.isFile()) { throw new Exception("ImageDeal>>>" + file + " 不是一個圖片文件!"); } BufferedImage bi = ImageIO.read(file); // 讀取該圖片BufferedImage spinImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.TYPE_INT_RGB); if (bi.getWidth() < size || bi.getHeight() < size || size <= 0) { // 馬賽克格尺寸太大或太小return false; } int xcount = 0; // 方向繪製個數int ycount = 0; // y方向繪製個數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; //坐標int y = 0; // 繪製馬賽克(繪製矩形並填充顏色) Graphics gs = spinImage.getGraphics(); for (int i = 0; i < xcount; i++) { for (int j = 0; j < ycount; j++) { //馬賽克矩形格大小int mwidth = size; int mheight = size; if(i==xcount-1){ //橫向最後一個比較特殊,可能不夠一個size mwidth = bi.getWidth()-x; } if(j == ycount-1){ //同理mheight =bi.getHeight()-y; } // 矩形顏色取中心像素點RGB值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;// 計算下一個矩形的y坐標} y = 0;// 還原y坐標x = x + size;// 計算x坐標} gs.dispose(); File sf = new File(saveUrl, saveName + "." + suffix); ImageIO.write(spinImage, suffix, sf); // 保存圖片return true; } public static void main(String[] args) throws Exception { ImageDeal imageDeal = new ImageDeal("e://1.jpg", "e://", "2", "jpg"); // 測試縮放/* imageDeal.zoom(200, 300); */ // 測試旋轉/* imageDeal.spin(90); */ //測試馬賽克/*imageDeal.mosaic(4);*/ }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。