java處理圖片--圖片的縮放,旋轉和馬賽克化
這是我自己結合網上的一些資料封裝的java圖片處理類,支持圖片的縮放,旋轉,馬賽克化。(轉載請注明出處:
http://blog.csdn.net/u012116457)
不多說,上代碼:
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);/ }
}</pre>