图片切割
package com.tb.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; public class CutImage { /** * @param args */ public static void main(String[] args) { //1. // for(int i=0;i<=220;i=i+30) // { // //CutImage.cut(0, i, 300, 30, "f:/1.jpg", "f:/new"+i/30+".jpg"); // } //2. // CutImage.cut(60, 90, 300, 30, "f:/1.jpg", "f:/103.jpg"); //3. for(int i=0;i<=6000;i=i+300) { //CutImage.cut(0, i, 300, 30, "f:/1.jpg", "f:/new"+i/30+".jpg"); CutImage.cut(0, i, 600, 300, "D:/Program Files/apache-tomcat-7.0.52/webapps/taobaoCsv/big/big.jpg", "D:/Program Files/apache-tomcat-7.0.52/webapps/taobaoCsv/cutimages/cut"+i/300+".jpg"); } } /** * 图片裁切 * @param x1 选择区域左上角的x坐标 * @param y1 选择区域左上角的y坐标 * @param width 选择区域的宽度 * @param height 选择区域的高度 * @param sourcePath 源图片路径 * @param descpath 裁切后图片的保存路径 */ public static void cut(int x1, int y1, int width, int height, String sourcePath, String descpath) { FileInputStream is = null; ImageInputStream iis = null; try { // 读取图片文件 is = new FileInputStream(sourcePath); String fileSuffix = sourcePath.substring(sourcePath .lastIndexOf(".") + 1); Iterator<ImageReader> it = ImageIO .getImageReadersByFormatName(fileSuffix); ImageReader reader = it.next(); iis = ImageIO.createImageInputStream(is); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x1, y1, width, height); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, fileSuffix, new File(descpath)); } catch (Exception ex) { ex.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } if (iis != null) { try { iis.close(); } catch (IOException e) { e.printStackTrace(); } iis = null; } } }
逃避不一定躲得过,面对不一定最难过