public class slice {
static int width=0, height=0;
static int startwidth=0, startheight=0;
public static void main(String args[]) {
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
//设置插件类型
ImageReader reader = (ImageReader)readers.next();
Object source=new File("test.jpg");//待读取文件
try {
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);//绑定输入
width=reader.getWidth(0);//图像宽度
height=reader.getHeight(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageReadParam param = reader.getDefaultReadParam();
for(int h=0;h<height;h+=256){
for(int w=0;w<width;w+=256){
Rectangle rect = new Rectangle(w, h,256 , 256);//设置待选框位置及大小
param.setSourceRegion(rect);
BufferedImage bi;
try {
bi= reader.read(0, param);//获取待选框内容
File f = new File(".\\slice\\"+String.valueOf(h)+"_"+String.valueOf(w)+".png");
ImageIO.write(bi, "png", f);//写图像
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}}}