原链接:https://blog.csdn.net/hgyu_962464/article/details/79278222 在原链接上进行了调整,直接批量读取特定目录下照片,进行裁剪,可直接运行。 import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import static javafx.scene.input.KeyCode.F; import static javafx.scene.input.KeyCode.SOFTKEY_0; public class CutImageUtil { public static void main(String[] args) { List<File> fileList = new ArrayList(); fileList = CutImageUtil.cutImageToFile("E:\\Program\\testleetcode\\source", "E:\\Program\\testleetcode\\target", 4); for (File file : fileList) { System.out.println(file.getAbsolutePath()); } } /** * 切割图片 * * @param sourceFilePath 源文件路径 * @param sourceFilePath 保存文件路径 * @param index 水平方向等分切割数 * @return List<File> File数组 */ public static List<File> cutImageToFile(String sourceFilePath, String targetDir, int index) { File file = new File(sourceFilePath); if (file.exists()) { return cutImageToFile(file, targetDir, index); } else { return null; } } /** * 切割图片 * * @param sourceFile 源文件 * @param index 水平方向等分切割数 * @return List<String> base64编码数组 */ public static List<String> cutImageToBase64(File sourceFile, int index) { List<String> list = new ArrayList<String>(); int suffixIndex = sourceFile.getName().lastIndexOf("."); String suffix = sourceFile.getName().substring(suffixIndex + 1); try { BufferedImage source = ImageIO.read(sourceFile); int width = source.getWidth(); // 图片宽度 int height = source.getHeight(); // 图片高度 if (index > 1) { int cWidth = width / index; // 切片宽度 BufferedImage image = null; for (int i = 0; i < index; i++) { // x坐标,y坐标,宽度,高度 BASE64Encoder encoder = new BASE64Encoder(); int cw = i * cWidth; ByteArrayOutputStream baos = new ByteArrayOutputStream(); image = source.getSubimage(cw, 0, cWidth, height); ImageIO.write(image, "PNG", baos); byte[] bytes = baos.toByteArray(); list.add("data:image/" + suffix + ";base64," + encoder.encodeBuffer(bytes)); baos.close(); } } } catch (IOException e) { e.printStackTrace(); } return list; } /** * 切割图片 * * @param sourceFile 源文件 * @param index 水平方向等分切割数 * @return List<String> base64编码数组 */ public static List<File> cutImageToFile(File sourceFile, String targetDir, int index) { List<File> list = new ArrayList<File>(); File[] fileList = sourceFile.listFiles(); for(File file : fileList) { int suffixIndex = file.getName().lastIndexOf("."); String suffix = file.getName().substring(suffixIndex + 1); String name = file.getName().substring(0, suffixIndex); try { BufferedImage source = ImageIO.read(file); int width = source.getWidth(); // 图片宽度 int height = source.getHeight(); // 图片高度 if (index > 1) { int cheight = height / index; // 切片高度 BufferedImage image = null; File file1 = new File(targetDir); if (!file.exists()) { // 存储目录不存在,则创建目录 file.mkdirs(); } for (int i = 0; i < index; i++) { // x坐标,y坐标,宽度,高度 int ch = i * cheight; image = source.getSubimage(0, ch, width, cheight); String fileName = targetDir + "/" + name + "_" + i + "." + suffix; file = new File(fileName); ImageIO.write(image, "PNG", file); list.add(file); } } } catch (IOException e) { e.printStackTrace(); } } return list; } }
图片裁剪:读取特定目录下所有图片,等分裁剪
最新推荐文章于 2023-09-05 11:15:00 发布