1.将包放在项目下的resource/lib/下
pom 引入:
HandleImgUtils.java文件: 我项目传的是 base64 大家可以拿本地图片测试
/**
* 检测图片颜色是否彩色
* @param baseResult base64 图片
* @return
* @throws IOException
*/
public static boolean imgColorCheck(String baseResult) throws IOException {
Mat srcImage = Java2MatTools.base642Mat(baseResult);
//File file = new File("D:\\20210322095515411.jpg");
//Mat srcImage = Imgcodecs.imread(file.getAbsolutePath());
Mat dstImage = new Mat();
org.opencv.imgproc.Imgproc.cvtColor(srcImage, dstImage, Imgproc.COLOR_BGR2HSV);
int i = 0;
int j = 0;
if (i == dstImage.rows() && j == dstImage.cols()) {
return false;
}
return true;
}
/**
* 检测图片亮度
* @param baseResult base64 图片
* @return cast 计算出的偏差值,小于1表示比较正常,大于1表示存在亮度异常;当cast异常时,da大于0表示过亮,da小于0表示过暗
* @throws IOException
*/
public static Integer imgBrightCheck(String baseResult) throws IOException {
Mat srcImage = Java2MatTools.base642Mat(baseResult);
Mat dstImage = new Mat();
// 将RGB图转为灰度图
Imgproc.cvtColor(srcImage, dstImage, Imgproc.COLOR_BGR2GRAY);
float a = 0;
int Hist[] = new int[256];
for (int i = 0; i < 256; i++) {
Hist[i] = 0;
}
for (int i = 0; i < dstImage.rows(); i++) {
for (int j = 0; j < dstImage.cols(); j++) {
//在计算过程中,考虑128为亮度均值点
a += (float) (dstImage.get(i, j)[0] - 128);
int x = (int) dstImage.get(i, j)[0];
Hist[x]++;
}
}
float da = a / (float) (dstImage.rows() * dstImage.cols());
float D = Math.abs(da);
float Ma = 0;
for (int i = 0; i < 256; i++) {
Ma += Math.abs(i - 128 - da) * Hist[i];
}
Ma /= (float) ((dstImage.rows() * dstImage.cols()));
float M = Math.abs(Ma);
float K = D / M;
float cast = K;
if (cast >= 1) {
if (da > 0) {
return 2; //过亮
} else {
return 1; //过暗
}
} else {
return 0; //正常
}
}
/**
* 检测图片清晰度 标准差越大说明图像质量越好
* @param baseResult base64 图片
* @param fileName
* @return
* @throws IOException
*/
public static boolean imgQxdCheck(String baseResult, String fileName) throws IOException {
byte[] bytes = Base64.decode(baseResult);
String path = "D:\\";
FileOutputStream out = new FileOutputStream(path + fileName);
out.write(bytes);
out.flush();
out.close();
opencv_core.Mat srcImage = opencv_imgcodecs.imread(path + fileName);
opencv_core.Mat dstImage = new opencv_core.Mat();
//转化为灰度图
opencv_imgproc.cvtColor(srcImage, dstImage, opencv_imgproc.COLOR_BGR2GRAY);
//在gray目录下生成灰度图片
opencv_imgcodecs.imwrite(path + "gray-" + fileName, dstImage);
opencv_core.Mat laplacianDstImage = new opencv_core.Mat();
//阈值太低会导致正常图片被误断为模糊图片,阈值太高会导致模糊图片被误判为正常图片
opencv_imgproc.Laplacian(dstImage, laplacianDstImage, opencv_core.CV_64F);
//在laplacian目录下升成经过拉普拉斯掩模做卷积运算的图片
opencv_imgcodecs.imwrite(path + "lap-" + fileName, laplacianDstImage);
//矩阵标准差
opencv_core.Mat stddev = new opencv_core.Mat();
//求矩阵的均值与标准差
opencv_core.meanStdDev(laplacianDstImage, new opencv_core.Mat(), stddev);
return stddev.createIndexer().getDouble() > 10;
}
调用 上面方法的的文件:我只贴了所需代码
@Override
//@Async
public String archivesCheck(String archivesCode) {
InputStream inputStream = minioTemplate.getObject(archivesScanFile.getBucketName(), archivesScanFile.getArchivesScanFileName());
String baseResult = FileUtils.getBase64FromInputStream(inputStream);
boolean imgColor = true;
Integer imgLight = 0;
boolean imgQxd = true;
// 加载dll 文件
String path = Resources.getResource("lib").getPath();
System.load(path+ "\\opencv_java410.dll");
try {
imgColor = HandleImgUtils.imgColorCheck(baseResult);
imgLight = HandleImgUtils.imgBrightCheck(baseResult);
imgQxd = HandleImgUtils.imgQxdCheck(baseResult, original);
} catch (IOException e) {
e.printStackTrace();
}
if (!imgColor) {
return original + " : 图片是黑白的";
}
if (imgLight == 2) {
return original + " : 图片过亮";
}
if (imgLight == 1) {
return original + " : 图片过暗";
}
if (!imgQxd) {
return original + " : 图片清晰度过低";
}
}
return null;
//@TODO 调业务系统接口 发送消息
}