先來看看效果:
測試一:
原圖:
效果圖:
測試二:
原圖:
效果圖:
代碼部分:
1 /** 2 * 3 */ 4 package com.b510; 5 6 import java.awt.Rectangle; 7 import java.awt.image.BufferedImage; 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.IOException; 11 import java.util.Date; 12 import java.util.Iterator; 13 14 import javax.imageio.ImageIO; 15 import javax.imageio.ImageReadParam; 16 import javax.imageio.ImageReader; 17 import javax.imageio.stream.ImageInputStream; 18 19 /** 20 * @date 2012-11-26 21 * @author xhw 22 * 23 */ 24 public class ImageCut { 25 26 /** 27 * 源圖片路徑名稱如:c:\1.jpg 28 */ 29 private String srcpath = "e:/poool.jpg"; 30 /** 31 * 剪切圖片存放路徑名稱.如:c:\2.jpg 32 */ 33 private String subpath = "e:/pool_end"; 34 /** 35 * jpg圖片格式 36 */ 37 private static final String IMAGE_FORM_OF_JPG = "jpg"; 38 /** 39 * png圖片格式 40 */ 41 private static final String IMAGE_FORM_OF_PNG = "png"; 42 /** 43 * 剪切點x坐標 44 */ 45 private int x; 46 47 /** 48 * 剪切點y坐標 49 */ 50 private int y; 51 52 /** 53 * 剪切點寬度 54 */ 55 private int width; 56 57 /** 58 * 剪切點高度 59 */ 60 private int height; 61 62 public ImageCut() { 63 64 } 65 66 public ImageCut(int x, int y, int width, int height) { 67 this.x = x; 68 this.y = y; 69 this.width = width; 70 this.height = height; 71 } 72 73 public static void main(String[] args) throws Exception { 74 ImageCut imageCut = new ImageCut(134, 0, 366, 366); 75 imageCut.cut(imageCut.getSrcpath(), imageCut.getSubpath()); 76 } 77 78 /** 79 * 返回包含所有當前已注冊 ImageReader 的 Iterator,這些 ImageReader 聲稱能夠解碼指定格式。 80 * 參數:formatName - 包含非正式格式名稱 .(例如 "jpeg" 或 "tiff")等 。 81 * 82 * @param postFix 83 * 文件的后綴名 84 * @return 85 */ 86 public Iterator<ImageReader> getImageReadersByFormatName(String postFix) { 87 switch (postFix) { 88 case IMAGE_FORM_OF_JPG: 89 return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_JPG); 90 case IMAGE_FORM_OF_PNG: 91 return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_PNG); 92 default: 93 return ImageIO.getImageReadersByFormatName(IMAGE_FORM_OF_JPG); 94 } 95 } 96 97 /** 98 * 對圖片裁剪,並把裁剪完蛋新圖片保存 。 99 * @param srcpath 源圖片路徑 100 * @param subpath 剪切圖片存放路徑 101 * @throws IOException 102 */ 103 public void cut(String srcpath, String subpath) throws IOException { 104 FileInputStream is = null; 105 ImageInputStream iis = null; 106 try { 107 // 讀取圖片文件 108 is = new FileInputStream(srcpath); 109 110 // 獲取文件的后綴名 111 String postFix = getPostfix(srcpath); 112 System.out.println("圖片格式為:" + postFix); 113 /* 114 * 返回包含所有當前已注冊 ImageReader 的 Iterator,這些 ImageReader 聲稱能夠解碼指定格式。 115 * 參數:formatName - 包含非正式格式名稱 .(例如 "jpeg" 或 "tiff")等 。 116 */ 117 Iterator<ImageReader> it = getImageReadersByFormatName(postFix); 118 119 ImageReader reader = it.next(); 120 // 獲取圖片流 121 iis = ImageIO.createImageInputStream(is); 122 123 /* 124 * <p>iis:讀取源.true:只向前搜索 </p>.將它標記為 ‘只向前搜索’。 125 * 此設置意味着包含在輸入源中的圖像將只按順序讀取,可能允許 reader 避免緩存包含與以前已經讀取的圖像關聯的數據的那些輸入部分。 126 */ 127 reader.setInput(iis, true); 128 129 /* 130 * <p>描述如何對流進行解碼的類<p>.用於指定如何在輸入時從 Java Image I/O 131 * 框架的上下文中的流轉換一幅圖像或一組圖像。用於特定圖像格式的插件 將從其 ImageReader 實現的 132 * getDefaultReadParam 方法中返回 ImageReadParam 的實例。 133 */ 134 ImageReadParam param = reader.getDefaultReadParam(); 135 136 /* 137 * 圖片裁剪區域。Rectangle 指定了坐標空間中的一個區域,通過 Rectangle 對象 138 * 的左上頂點的坐標(x,y)、寬度和高度可以定義這個區域。 139 */ 140 Rectangle rect = new Rectangle(x, y, width, height); 141 142 // 提供一個 BufferedImage,將其用作解碼像素數據的目標。 143 param.setSourceRegion(rect); 144 /* 145 * 使用所提供的 ImageReadParam 讀取通過索引 imageIndex 指定的對象,並將 它作為一個完整的 146 * BufferedImage 返回。 147 */ 148 BufferedImage bi = reader.read(0, param); 149 150 // 保存新圖片 151 ImageIO.write(bi, postFix, new File(subpath + "_" + new Date().getTime() + "." + postFix)); 152 } finally { 153 if (is != null) 154 is.close(); 155 if (iis != null) 156 iis.close(); 157 } 158 159 } 160 161 /** 162 * 獲取inputFilePath的后綴名,如:"e:/test.pptx"的后綴名為:"pptx"<br> 163 * 164 * @param inputFilePath 165 * @return 166 */ 167 public String getPostfix(String inputFilePath) { 168 return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); 169 } 170 171 public int getHeight() { 172 return height; 173 } 174 175 public void setHeight(int height) { 176 this.height = height; 177 } 178 179 public String getSrcpath() { 180 return srcpath; 181 } 182 183 public void setSrcpath(String srcpath) { 184 this.srcpath = srcpath; 185 } 186 187 public String getSubpath() { 188 return subpath; 189 } 190 191 public void setSubpath(String subpath) { 192 this.subpath = subpath; 193 } 194 195 public int getWidth() { 196 return width; 197 } 198 199 public void setWidth(int width) { 200 this.width = width; 201 } 202 203 public int getX() { 204 return x; 205 } 206 207 public void setX(int x) { 208 this.x = x; 209 } 210 211 public int getY() { 212 return y; 213 } 214 215 public void setY(int y) { 216 this.y = y; 217 } 218 219 }