import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
@Service
public class ImageService {
private static List<File> fileList = new ArrayList<File>();
/* public static void main(String[] args) {
System.out.println("输入需要去水印的图片所在的根目录回车(支持递归子目录):");
Scanner input = new Scanner(System.in);
String dir = input.nextLine().trim();
System.out.println("输入转换后的存储目录:");
String saveDir = input.nextLine().trim();
System.out.println("输入y开始");
String comfrm = input.nextLine().trim();
if (comfrm.equals("y")) {
convertAllImages(dir, saveDir);
} else {
System.out.println("您输入的不是y程序,程序退出");
}
}
*/
/**
* 遍历文件
* @param dir
* @param saveDir
*/
public static Boolean convertAllImages(String dir, String saveDir) {
File dirFile = new File(dir);
File saveDirFile = new File(saveDir);
if(!saveDirFile.exists()){
saveDirFile.mkdir();
}
dir = dirFile.getAbsolutePath();
saveDir = saveDirFile.getAbsolutePath();
loadImages(new File(dir));
for (File file : fileList) {
String filePath = file.getAbsolutePath();
String dstPath = saveDir + filePath.substring(filePath.indexOf(dir) + dir.length(), filePath.length());
// System.out.println("filePath: " + filePath);
// System.out.println("dstPath:"+dstPath);
try {
//剪切图片
ImageService.cutImage(filePath,dstPath);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 获取文件名
* @param f
*/
public static void loadImages(File f) {
if (f != null) {
if (f.isDirectory()) {
File[] fileArray = f.listFiles();
if (fileArray != null) {
for (int i = 0; i < fileArray.length; i++) {
//递归调用
loadImages(fileArray[i]);
}
}
} else {
String name = f.getName();
if (name.endsWith("png") || name.endsWith("jpg")) {
fileList.add(f);
}
}
}
}
/** 裁剪图片
* * @param x 剪切横向起始位置
* * @param y 剪切纵向起始位置
* * @param width 剪切宽度
* * @param height 剪切宽度
* @param inputFile 输入文件
* @param outputFile 输出文件
* @throws IOException
*/
public static void cutImage(String inputFile,
String outputFile) throws IOException {
String type = "jpg";
// 裁剪的位置
int x = 0;
int y = 0;
int width = 0;
int height = 0;
// 文件地址
File file = new File(inputFile);
InputStream input = new FileInputStream(file);
BufferedImage sourceImg = ImageIO.read(new FileInputStream(file));
// System.out.println(String.format("Size: %.1f KB", file.length()/1024.0));
// System.out.println("Width: " + sourceImg.getWidth());
// System.out.println("Height: " + sourceImg.getHeight());
width=sourceImg.getWidth();
height=sourceImg.getHeight()-80;
ImageInputStream imageStream = null;
try {
// 图片类型 默认 jpg
String imageType = (null == type || "".equals(type)) ? "jpg" : type;
// 将图片转化为 imageReader
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageType);
ImageReader reader = readers.next();
// 读入图片
imageStream = ImageIO.createImageInputStream(input);
reader.setInput(imageStream, true);
// 参数
ImageReadParam param = reader.getDefaultReadParam();
// 图片裁剪范围
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
// 裁剪出图片
BufferedImage bi = reader.read(0, param);
// 输出达到文件夹
ImageIO.write(bi, imageType, new File(outputFile));
} catch (Exception e) {
} finally {
// 关闭stream
// imageStream.close();
}
}
}
网友评论