本文實例講述了java實現(xiàn)的圖片裁剪功能。分享給大家供大家參考,具體如下:
站在用戶的角度思考問題,與客戶深入溝通,找到漠河網(wǎng)站設(shè)計與漠河網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋漠河地區(qū)。PicCut.java:
package Tsets; 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 PicCut { public void cut(int x,int y,int width,int height,String srcpath,String subpath) throws IOException {//裁剪方法 FileInputStream is = null; ImageInputStream iis = null; try { is = new FileInputStream(srcpath); //讀取原始圖片 Iteratorit = ImageIO.getImageReadersByFormatName("jpg"); //ImageReader聲稱能夠解碼指定格式 ImageReader reader = it.next(); iis = ImageIO.createImageInputStream(is); //獲取圖片流 reader.setInput(iis, true); //將iis標記為true(只向前搜索)意味著包含在輸入源中的圖像將只按順序讀取 ImageReadParam param = reader.getDefaultReadParam(); //指定如何在輸入時從 Java Image I/O框架的上下文中的流轉(zhuǎn)換一幅圖像或一組圖像 Rectangle rect = new Rectangle(x, y, width, height); //定義空間中的一個區(qū)域 param.setSourceRegion(rect); //提供一個 BufferedImage,將其用作解碼像素數(shù)據(jù)的目標。 BufferedImage bi = reader.read(0, param); //讀取索引imageIndex指定的對象 ImageIO.write(bi, "jpg", new File(subpath)); //保存新圖片 } finally { if (is != null) is.close(); if (iis != null) iis.close(); } } public static void main(String[] args) throws Exception { PicCut pc = new PicCut(); pc.cut(20, 20, 100, 100,"D:\\1.jpg","D:\\11.jpg"); System.out.println("ok"); } }