java圖片裁剪

來源:互聯網
上載者:User

這個切圖處理類配合前台js,可以做到圖片上傳時,切圖儲存的效果。

package com.util.image;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;/** * 用於剪下圖片的java類 . *  * @author  *  */public class OperateImage{/** * 源圖片路徑 */private String srcpath;/** * 剪下圖片存放路徑名稱 */private String subpath;/** * 剪下點的x座標 . */private int x;/** * 剪下點的y座標 . */private int y;/** * 要剪下圖片的寬度 . */private int width;/** * 要剪下圖片的高度 . */private int height;/** * 無參構造方法 . */public OperateImage(){}/** * 構造方法,傳入要切圖片的左上方座標和要切圖片的寬和高 . *  * @param x *            . * @param y *            . * @param width *            . * @param height *            . */public OperateImage(int x, int y, int width, int height){this.x = x;this.y = y;this.width = width;this.height = height;}/** * 程式的主方法 . *  * @param args *            . * @throws Exception . */public static void main(String[] args) throws Exception{// 指定將要被剪下的圖片路徑String name = "C:\\srcimage\\test.BMP";// 設定要剪下地區的左上方座標和要剪下圖片的寬和高 .OperateImage o = new OperateImage(160, 32, 16, 16); // (x, y, width, height)// 設定源圖片路徑 .o.setSrcpath(name);// 設定要輸出圖片的路徑和新圖片名 .o.setSubpath("C:\\subimage\\50.bmp");// 調用切圖方法 .o.cut();}/** * 對圖片裁剪,並把裁剪完的新圖片進行儲存 . */public void cut() throws IOException{// 檔案輸入資料流對象 is .FileInputStream is = null;// 圖片檔案輸入資料流對象 iis .ImageInputStream iis = null;try{// 據圖片檔案路徑讀取圖片檔案 .is = new FileInputStream(srcpath);/* * 返回包含所有當前登入 ImageReader 的 Iterator,這些 ImageReader 聲稱能夠解碼指定格式。 * 參數:formatName - 包含非正式格式名稱 .(例如 "jpeg" 或 "tiff")等 。 */Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("bmp");ImageReader reader = it.next();// 將檔案流轉為圖片流 .iis = ImageIO.createImageInputStream(is);/* * <p>iis:讀取源.true:只向前搜尋 </p>.將它標記為 ‘只向前搜尋’。 * 此設定意味著包含在輸入源中的映像將只按順序讀取,可能允許 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, "bmp", new File(subpath));}// 程式最後執行關閉流檔案流和圖片流 .finally{if (is != null)is.close();if (iis != null)iis.close();}}    /**     *      * @return height .     */public int getHeight(){return height;}    /**     *      * @param height .     */public void setHeight(int height){this.height = height;}    /**     *      * @return srcpath .     */public String getSrcpath(){return srcpath;}    /**     *      * @param srcpath .      */public void setSrcpath(String srcpath){this.srcpath = srcpath;}    /**     *      * @return subpath .     */public String getSubpath(){return subpath;}    /**     *      * @param subpath .     */public void setSubpath(String subpath){this.subpath = subpath;}    /**     *      * @return width .     */public int getWidth(){return width;}    /**     *      * @param width .     */public void setWidth(int width){this.width = width;}    /**     *       * @return x .     */public int getX(){return x;}    /**     *      * @param x .     */public void setX(int x){this.x = x;}    /**     *      * @return .     */public int getY(){return y;}    /**     *      * @param y .     */public void setY(int y){this.y = y;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.