java切割图片

package org.apache.jackrabbit.firsthops;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.imageio.ImageIO;


import org.apache.jackrabbit.firsthops.DTO.ImageDto;


public class CutImage {




public ImageDto cut(File sourcefile, String targetDir, int width, int height)
throws Exception {


List<File> list = new ArrayList<File>();
BufferedImage source = ImageIO.read(sourcefile);
int sWidth = source.getWidth();
int sHeight = source.getHeight();
if (sWidth > width && sHeight > height) {
int cols = 0, rows = 0, eWidth = 0, eHeight = 0;
if (sWidth % width == 0) {
cols = sWidth / width;
} else {
eWidth = sWidth % width;
cols = sWidth / width + 1;
}
if (sHeight % height == 0) {
rows = sHeight / height;
} else {
eHeight = sHeight % height;
rows = sHeight / height + 1;
}
String fileName = null;
File file = new File(targetDir);
if (!file.exists()) {
file.mkdirs();
}
BufferedImage image = null;
int cWidth = 0;
int cHeight = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cWidth = getWidth(j, cols, eWidth, width);
cHeight = getHeight(i, rows, eHeight, height);
image = source.getSubimage(j * width, i * height, cWidth,
cHeight);
fileName = targetDir + "/map_" + i + "_" + j + ".jpg";
file = new File(fileName);
ImageIO.write(image, "JPEG", file);
list.add(file);
}
}
}
return new ImageDto(sWidth, sHeight, list);  
}


public ImageDto cut(String source, String targetDir, int width, int height)
throws Exception {
return cut(new File(source), targetDir, width, height);
}


private int getWidth(int index, int cols, int endWidth, int width) {
if (index == cols - 1) {
if (endWidth != 0) {
return endWidth;
}
}
return width;
}


private int getHeight(int index, int rows, int endHeight, int height) {
if (index == rows - 1) {
if (endHeight != 0) {
return endHeight;
}
}
return height;
}

public static void main(String[] args) throws Exception {
String path = "C:\\Documents and Settings\\All Users\\Documents\\Blue hills.jpg";
String saveTo = "C:\\Documents and Settings\\All Users\\Documents\\a";
File imgFile = new File(path);
CutImage cutimage = new CutImage();
cutimage.cut(imgFile,saveTo,300,300);

}


}


package org.apache.jackrabbit.firsthops.DTO;


import java.io.File;
import java.io.Serializable;
import java.util.List;


public class ImageDto implements Serializable {


private static final long serialVersionUID = 1L;
private Integer width;
private Integer height;
private List<File> listFile;
public ImageDto(){}
public ImageDto(Integer width,Integer height,List<File> listFile){
this.width = width;
this.height = height;
this.listFile = listFile;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public List<File> getListFile() {
return listFile;
}
public void setListFile(List<File> listFile) {
this.listFile = listFile;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值