package debugcut;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class debugCut
{
public static void main(String[] args) throws IOException
{
String targetDir = "E:\\";
String srcDir = "E:\\qqqq.jpg";
int width = 600;
int height = 600;
BufferedImage source = ImageIO.read(new File(srcDir));
int sWidth = source.getWidth(); // 图片宽度
int sHeight = source.getHeight(); // 图片高度
if (sWidth > width && sHeight > height)
{
int cols = 0; // 横向切片总数
int rows = 0; // 纵向切片总数
int eWidth = 0; // 末端切片宽度
int 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;
}
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);
// x坐标,y坐标,宽度,高度
image = source.getSubimage(j * width, i * height, cWidth,cHeight);
String dir = targetDir + "cut_" + i + "_" + j + ".jpg";
ImageIO.write(image, "JPEG", new File(dir));
}
}
}
System.out.print("Done!");
}
public static int getWidth(int index, int cols, int endWidth, int width)
{
if (index == cols - 1)
{
if (endWidth != 0)
{
return endWidth;
}
}
return width;
}
private static int getHeight(int index, int rows, int endHeight, int height)
{
if (index == rows - 1)
{
if (endHeight != 0)
{
return endHeight;
}
}
return height;
}
}
------解决方案--------------------
if (sWidth > width && sHeight > height)
看看是不是你图片没有满足这个需求
也就是没有切割的原因