文章底部有所需的jar包。
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 CutPicture {
public static void main(String[] args) throws IOException {
String orsrc="F:\\The originar file\\00000001.tif";
BufferedImage sourceImg=ImageIO.read(new File(orsrc));
//System.out.println(sourceImg);
int width=sourceImg.getWidth();
int height=sourceImg.getHeight();
System.out.println(width+"\n"+height);
orsrc=orsrc.replaceAll("\\\\", "/");
int lastindex= orsrc.lastIndexOf("/");
String srcone= orsrc.substring(0,lastindex+1);
String srctwo= orsrc.substring(lastindex+1,orsrc.length());
String fusrc=srcone+"cope"+srctwo;
boolean blog= cutPic(orsrc,fusrc, 0, 0, width, height/10);
System.out.println(blog);
}
/**
* @param srcFile源文件
* @param outFile输出文件
* @param x坐标
* @param y坐标
* @param width宽度
* @param height高度
* @return
* @描述 —— 裁剪图片
*/
public static boolean cutPic(String srcFile, String outFile, int x, int y,
int width, int height) {
FileInputStream is = null;
ImageInputStream iis = null;
try {
// 如果源图片不存在
if (!new File(srcFile).exists()) {
return false;
}
// 读取图片文件
is = new FileInputStream(srcFile);
// 获取文件格式
String ext = srcFile.substring(srcFile.lastIndexOf(".") + 1);
// ImageReader声称能够解码指定格式
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(ext);
ImageReader reader = it.next();
// 获取图片流
iis = ImageIO.createImageInputStream(is);
// 输入源中的图像将只按顺序读取
reader.setInput(iis, true);
// 描述如何对流进行解码
ImageReadParam param = reader.getDefaultReadParam();
// 图片裁剪区域
Rectangle rect = new Rectangle(x, y, width, height);
// 提供一个 BufferedImage,将其用作解码像素数据的目标
param.setSourceRegion(rect);
// 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象
BufferedImage bi = reader.read(0, param);
// 保存新图片
File tempOutFile = new File(outFile);
if (!tempOutFile.exists()) {
tempOutFile.mkdirs();
}
ImageIO.write(bi, ext, new File(outFile));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (is != null) {
is.close();
}
if (iis != null) {
iis.close();
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
public void ss()throws Exception {
/*FileOutputStream out = new FileOutputStream("e:/a.png");
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",new File("e:/a.tif"),null);
ImageEncoder encoder = ImageCodec.createImageEncoder("png",out,null);
encoder.encode( decoder.decodeAsRenderedImage() );;
out.close();*/
}
}
<点击下载所需的jar包>