public static int[] cut(String srcImageFile, String descDir, int x,int y) {
int srcWidthsum = 0 ;
int srcHeightsum = 0;
try {
Image img;
ImageFilter cropFilter;
String dir = null; // 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
srcWidthsum = bi.getWidth() ; // 源图宽度
srcHeightsum =bi.getHeight(); // 源图高度
Image image = (Image)bi;
int srcWidth = srcWidthsum/y; // 切片宽度
int srcHeight =srcHeightsum/x; // 切片高度
int cols = x; // 切片横向数量
int rows = y; // 切片纵向数量 // 循环建立切片 for (int i = 0; i < rows; i++)
{ for (int j = 0; j < cols; j++)
{ // 四个参数分别为图像起点坐标和宽高 // 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * srcWidth, i * srcHeight, srcWidth, srcHeight); img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
// 输出为文件 dir = descDir + i + "_" + j + ".jpg"; File f = new File(dir);
ImageIO.write(tag, "JPEG",f);
System.out.println("第"+i+"次"+"第"+j+"次宽坐标"+j * srcWidth);
System.out.println("第"+i+"次"+"第"+j+"次纵坐标"+i * srcHeight);
}
}
} catch (Exception e) { e.printStackTrace(); }
int[] a ={srcWidthsum,srcHeightsum};
return a; }
int srcWidthsum = 0 ;
int srcHeightsum = 0;
try {
Image img;
ImageFilter cropFilter;
String dir = null; // 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
srcWidthsum = bi.getWidth() ; // 源图宽度
srcHeightsum =bi.getHeight(); // 源图高度
Image image = (Image)bi;
int srcWidth = srcWidthsum/y; // 切片宽度
int srcHeight =srcHeightsum/x; // 切片高度
int cols = x; // 切片横向数量
int rows = y; // 切片纵向数量 // 循环建立切片 for (int i = 0; i < rows; i++)
{ for (int j = 0; j < cols; j++)
{ // 四个参数分别为图像起点坐标和宽高 // 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * srcWidth, i * srcHeight, srcWidth, srcHeight); img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
// 输出为文件 dir = descDir + i + "_" + j + ".jpg"; File f = new File(dir);
ImageIO.write(tag, "JPEG",f);
System.out.println("第"+i+"次"+"第"+j+"次宽坐标"+j * srcWidth);
System.out.println("第"+i+"次"+"第"+j+"次纵坐标"+i * srcHeight);
}
}
} catch (Exception e) { e.printStackTrace(); }
int[] a ={srcWidthsum,srcHeightsum};
return a; }