天天看點

圖檔旋轉和馬賽克

package img;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

/**
 * 測試圖檔旋轉和馬賽克
 */
public class TurnAndMasaike {
    public static void main(String[] args) throws Exception {  
        File file= new File("D:\\test\\map.jpg"); 
        File destFile = new File("D:\\test\\ggfgfgfgs.jpg");
        /*測試旋轉*/
        spin(file, 90, destFile);
        /*測試馬賽克*/
        //mosaic(file, 10, destFile);
    }
    
    /**
     * 旋轉
     */
    public static void spin(File file, int degree, File destFile) throws Exception {
        int swidth = 0; // 旋轉後的寬度
        int sheight = 0; // 旋轉後的高度
        int x; // 原點橫坐标
        int y; // 原點縱坐标

        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);
        ImageIO.write(spinImage, "jpg", destFile);  
    }
    
    /**
     * 馬賽克化.
     * @param size  馬賽克尺寸,即每個矩形的長寬
     * @return
     * @throws Exception
     */
    public static boolean mosaic(File file, int size, File destFile) throws Exception {
        if (!file.isFile()) {
            throw new Exception("ImageDeal>>>" + file + " 不是一個圖檔檔案!");
        }
        BufferedImage bi = ImageIO.read(file); // 讀取該圖檔
        @SuppressWarnings("static-access")
        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; // x方向繪制個數
        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();
        ImageIO.write(spinImage, "jpg", destFile); // 儲存圖檔
        return true;
    }
}