对图片进行缩放、切割、格式转换、彩色转黑白

1.要导入的jar包


2.对图片进行缩放

/**
* 缩放图片

* @param srcImageFile
*            源图像地址
* @param reslut
*            缩放后的图像地址
* @param scale
*            缩放比例
* @param flag
*            缩放选择, ture 放大;false 缩小

*/

public static void scale(String srcImageFile, String result, int scale, boolean flag) {
File file = new File(srcImageFile); // 根据图片地址获取图片文件
try {
BufferedImage imageBuffer = ImageIO.read(file); // 读入文件
int width = imageBuffer.getWidth(); // 获取图片宽度
int height = imageBuffer.getHeight(); // 获取图片高度
if (flag) { // 放大
width = width * scale;
height = height * scale;
} else { // 缩小
width = width / scale;
height = height / scale;
}
Image image = imageBuffer.getScaledInstance(width, height, Image.SCALE_DEFAULT);// 参数是:宽、高、重定义图像的算法类型
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null);// 参数是:Image对象、横坐标、纵坐标、当更多的图像被转换时要被通知的对象
g.dispose();// 释放资源
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流


} catch (IOException e) {
e.printStackTrace();
}

}


3.对图片进行切割

/**
* 图像切割

* @param srcImageFile
*            源图像地址
* @param descDir
*            切片目标文件夹
* @param destWidth
*            目标切片宽度
* @param destHeight
*            目标切片高度
*/
public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight) {
Image img;
ImageFilter cropFilter;
File file = new File(srcImageFile);// 根据图片地址创建图片文件
try {
BufferedImage bi = ImageIO.read(file);
int srcWidth = bi.getWidth();// 获取源图片宽度
int srcHeight = bi.getHeight();// 获取源图片高度
if (srcWidth > destWidth && srcHeight > destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
/*
* destWidth = 200; // 切片宽度 destHeight = 150; // 切片高度
*/
int cols = 0;// 切片横向数量
int rows = 0;// 切片纵向数量
// 计算切面的横向数量
if (srcWidth % destWidth == 0) {
cols = srcWidth / destWidth;
} else {
cols = (int) (Math.floor(srcWidth / destWidth) + 1);
}
// 计算切面的纵向数量
if (srcHeight % destHeight == 0) {
rows = srcHeight / destHeight;
} else {
rows = (int) (Math.floor(srcHeight / destHeight) + 1);
}
// 循环建立切片
// 改进方法,是否可以利用多线程来提高切割的数度
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 * 200, i * 150, destWidth, destHeight);
img = Toolkit.getDefaultToolkit()
.createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(descDir + "pre_map_" + i + "_" + j + ".jpg"));
}
}
}
} catch (IOException e) {
e.printStackTrace();

}


4.将图片进行格式转换

/**
* 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)

* @param source
*            具体源文件路径(路径/xxx.png 等)
* @param result
*            转换后文件路径和名称(路径/xxx.jpg 等)
*/
public static void convert(String source, String result) {
try {
File f = new File(source);
f.canRead();
f.canWrite();
BufferedImage src = ImageIO.read(f);
ImageIO.write(src, "JPG", new File(result));
} catch (Exception e) {
e.printStackTrace();
}
}

5.将彩色图片变成黑白

/**
* 彩色转为黑白

* @param source
*            具体源文件路径(路径/xxx.png 等)
* @param result
*            转换后文件路径和名称(路径/xxx.jpg 等)
*/
public static void gray(String source, String result) {
try {
BufferedImage src = ImageIO.read(new File(source));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, "JPEG", new File(result));
} catch (IOException e) {
e.printStackTrace();
}
}


猜你喜欢

转载自blog.csdn.net/Vampire_Vang/article/details/80089826