parent
5180e5b28a
commit
91827af36f
@ -1,16 +1,4 @@
|
||||
[
|
||||
"古井贡5年",
|
||||
"g8",
|
||||
"g8y",
|
||||
"g16",
|
||||
"g20",
|
||||
"g5(2)",
|
||||
"gxl",
|
||||
"g7",
|
||||
"v6",
|
||||
"gjyy",
|
||||
"lcg",
|
||||
"v6",
|
||||
"v60",
|
||||
"D8"
|
||||
"正常",
|
||||
"倾斜"
|
||||
]
|
||||
@ -0,0 +1,10 @@
|
||||
package com.zhehekeji.web.entity.yolo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ClassifyEntity {
|
||||
private Integer index;
|
||||
private String name;
|
||||
private double confidence;
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
package com.zhehekeji.web.entity.yolo;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ImageTool {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 输入图片路径
|
||||
String inputImagePath = "D:\\PycharmProjects\\yolo\\pyTest\\train\\1\\192.168.2.64_01_2025090413515066.jpeg";
|
||||
// 输出图片路径
|
||||
String outputImagePath = "D:\\test.jpg";
|
||||
|
||||
// 裁剪参数
|
||||
int width = 200;
|
||||
int height = 200;
|
||||
int x = 100;
|
||||
int y = 100;
|
||||
|
||||
// 调用裁剪方法
|
||||
boolean success = cropImage(inputImagePath, outputImagePath, width, height, x, y);
|
||||
if (success) {
|
||||
System.out.println("图片裁剪成功并保存到 " + outputImagePath);
|
||||
} else {
|
||||
System.out.println("图片裁剪失败");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("处理图片时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
public static boolean cropImage(String inputImagePath, String outputImagePath,
|
||||
int width, Integer[] space) throws IOException {
|
||||
|
||||
return cropImage(inputImagePath, outputImagePath, width, width, space[0], space[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 裁剪图片并保存到指定位置
|
||||
*
|
||||
* @param inputImagePath 输入图片地址
|
||||
* @param outputImagePath 输出图片地址
|
||||
* @param width 裁剪宽度
|
||||
* @param height 裁剪高度
|
||||
* @param x 裁剪区域左上角x坐标
|
||||
* @param y 裁剪区域左上角y坐标
|
||||
* @return 是否裁剪成功
|
||||
* @throws IOException 当读取或写入图片失败时抛出异常
|
||||
*/
|
||||
public static boolean cropImage(String inputImagePath, String outputImagePath,
|
||||
int width, int height, int x, int y) throws IOException {
|
||||
// 读取原图片
|
||||
File inputFile = new File(inputImagePath);
|
||||
BufferedImage originalImage = ImageIO.read(inputFile);
|
||||
|
||||
// 检查边界有效性
|
||||
if (x < 0 || y < 0 || x + width > originalImage.getWidth() || y + height > originalImage.getHeight()) {
|
||||
throw new IllegalArgumentException("裁剪区域超出图片边界");
|
||||
}
|
||||
|
||||
// 创建裁剪后的图片
|
||||
BufferedImage croppedImage = originalImage.getSubimage(x, y, width, height);
|
||||
|
||||
// 获取输出文件的扩展名
|
||||
String format = "jpg";
|
||||
int dotIndex = outputImagePath.lastIndexOf('.');
|
||||
if (dotIndex > 0) {
|
||||
format = outputImagePath.substring(dotIndex + 1).toLowerCase();
|
||||
}
|
||||
|
||||
// 确保输出目录存在
|
||||
File outputFile = new File(outputImagePath);
|
||||
File parentDir = outputFile.getParentFile();
|
||||
if (parentDir != null && !parentDir.exists()) {
|
||||
parentDir.mkdirs();
|
||||
}
|
||||
|
||||
// 保存裁剪后的图片
|
||||
return ImageIO.write(croppedImage, format, outputFile);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue