Java圖片居中裁剪代碼

cwf8 9年前發布 | 3K 次閱讀 Java

對圖片進行自動裁剪,保證中間部分不被剪掉

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class OperateImage {
    // ===源圖片路徑名稱如:c:\1.jpg
    private String srcpath;
    // ===剪切圖片存放路徑名稱。如:c:\2.jpg
    private String subpath;
    // ===剪切點x坐標
    private int x;
    private int y;
    // ===剪切點寬度
    private int width;
    private int height;

public OperateImage() {
}

public OperateImage(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

/**
 * 對圖片裁剪,并把裁剪完的新圖片保存 。
 */
public void cut() throws IOException {
    FileInputStream is = null;
    ImageInputStream iis = null;
    try {
        // 讀取圖片文件
        is = new FileInputStream(srcpath);
        /**
         * 返回包含所有當前已注冊 ImageReader 的 Iterator,這些 ImageReader
         * 聲稱能夠解碼指定格式。 參數:formatName - 包含非正式格式名稱 .
         * (例如 "jpeg" 或 "tiff")等 。
         */
        Iterator<ImageReader> it = ImageIO
                .getImageReadersByFormatName("jpg");
        ImageReader reader = it.next();
        // 獲取圖片流
        iis = ImageIO.createImageInputStream(is);
        /**
         * iis:讀取源。true:只向前搜索
         * 將它標記為 ‘只向前搜索’。
         * 此設置意味著包含在輸入源中的圖像將只按順序讀取,可能允許 reader
         * 避免緩存包含與以前已經讀取的圖像關聯的數據的那些輸入部分。
         */
        reader.setInput(iis, true);
        /**
         * <p>
         * 描述如何對流進行解碼的類
         * <p>
         * 用于指定如何在輸入時從 Java Image I/O
         * 框架的上下文中的流轉換一幅圖像或一組圖像。用于特定圖像格式的插件
         * 將從其 ImageReader 實現的 getDefaultReadParam 方法中返回
         * ImageReadParam 的實例。
         */
        ImageReadParam param = reader.getDefaultReadParam();
        /**
         * 圖片裁剪區域。Rectangle 指定了坐標空間中的一個區域,通過 Rectangle 對象
         * 的左上頂點的坐標(x,y)、寬度和高度可以定義這個區域。
         */
        Rectangle rect = new Rectangle(x, y, width, height);
        // 提供一個 BufferedImage,將其用作解碼像素數據的目標。
        param.setSourceRegion(rect);
        /**
         * 使用所提供的 ImageReadParam 讀取通過索引 imageIndex 指定的對象,并將
         * 它作為一個完整的 BufferedImage 返回。
         */
        BufferedImage bi = reader.read(0, param);
        // 保存新圖片
        ImageIO.write(bi, "jpg", new File(subpath));
    } finally {
        if (is != null)
            is.close();
        if (iis != null)
            iis.close();
    }
}

public int getHeight() {
    return height;
}

public void setHeight(int height) {
    this.height = height;
}

public String getSrcpath() {
    return srcpath;
}

public void setSrcpath(String srcpath) {
    this.srcpath = srcpath;
}

public String getSubpath() {
    return subpath;
}

public void setSubpath(String subpath) {
    this.subpath = subpath;
}

public int getWidth() {
    return width;
}

public void setWidth(int width) {
    this.width = width;
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public static void main(String[] args) throws Exception {
    String name = "D:\\ZHANG.jpg";
    OperateImage o = new OperateImage(180, 180, 180, 180);
    o.setSrcpath(name);
    o.setSubpath("F:\\6669.jpg");
    o.cut();
}

}</pre>

 本文由用戶 cwf8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!