中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java圖片居中裁剪代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

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

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坐標(biāo)
    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);
            /**
             * 返回包含所有當(dāng)前已注冊 ImageReader 的 Iterator,這些 ImageReader
             * 聲稱能夠解碼指定格式。 參數(shù):formatName - 包含非正式格式名稱 .
             * (例如 "jpeg" 或 "tiff")等 。
             */
            Iterator<ImageReader> it = ImageIO
                    .getImageReadersByFormatName("jpg");
            ImageReader reader = it.next();
            // 獲取圖片流
            iis = ImageIO.createImageInputStream(is);
            /**
             * iis:讀取源。true:只向前搜索
             * 將它標(biāo)記為 ‘只向前搜索’。
             * 此設(shè)置意味著包含在輸入源中的圖像將只按順序讀取,可能允許 reader
             * 避免緩存包含與以前已經(jīng)讀取的圖像關(guān)聯(lián)的數(shù)據(jù)的那些輸入部分。
             */
            reader.setInput(iis, true);
            /**
             * <p>
             * 描述如何對流進行解碼的類
             * <p>
             * 用于指定如何在輸入時從 Java Image I/O
             * 框架的上下文中的流轉(zhuǎn)換一幅圖像或一組圖像。用于特定圖像格式的插件
             * 將從其 ImageReader 實現(xiàn)的 getDefaultReadParam 方法中返回
             * ImageReadParam 的實例。
             */
            ImageReadParam param = reader.getDefaultReadParam();
            /**
             * 圖片裁剪區(qū)域。Rectangle 指定了坐標(biāo)空間中的一個區(qū)域,通過 Rectangle 對象
             * 的左上頂點的坐標(biāo)(x,y)、寬度和高度可以定義這個區(qū)域。
             */
            Rectangle rect = new Rectangle(x, y, width, height);
            // 提供一個 BufferedImage,將其用作解碼像素數(shù)據(jù)的目標(biāo)。
            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();
    }
}

標(biāo)簽: 搜索

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Python在windows下模擬按鍵和鼠標(biāo)點擊代碼

下一篇:python登陸網(wǎng)頁并處理網(wǎng)站session和cookie