我正在学习如何在java中使用openCV,同时也在学习如何使用NixOS。
我有以下的flake:
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
javaVersion = 19;
overlays = [
(self: super: rec {
jdk = super."jdk${toString javaVersion}";
maven = super.maven.override {
inherit jdk;
};
})
];
pkgs = import nixpkgs {inherit overlays system;};
in {
devShell = let
generateEditorConfig = pkgs.writeShellScriptBin "generateEditorConfig" ''
if [ ! -f .editorconfig ]; then
echo "root = true" > .editorconfig
echo "" >> .editorconfig
echo "[*]" >> .editorconfig
echo "end_of_line = lf" >> .editorconfig
echo "insert_final_newline = true" >> .editorconfig
echo "indent_style = tab" >> .editorconfig
echo "tab_width = 4" >> .editorconfig
echo "charset = utf-8" >> .editorconfig
echo "" >> .editorconfig
echo "[*.{yaml,yml}]" >> .editorconfig
echo "indent_style = space" >> .editorconfig
echo "indent_size = 2" >> .editorconfig
echo "" >> .editorconfig
echo "[*.{md,nix}]" >> .editorconfig
echo "indent_style = space" >> .editorconfig
echo "indent_size = 2" >> .editorconfig
fi
'';
in
pkgs.mkShell {
name = "java";
buildInputs = with pkgs; [
jdk
maven
tomcat9
opencv
];
shellHook = ''
export JAVA_HOME=${pkgs.jdk}
${generateEditorConfig}/bin/generateEditorConfig
'';
};
});
}
Java文件:src/main/java/NerfThis.java
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.objdetect.Objdetect;
import nu.pattern.OpenCV;
public class NerfThis {
public static void main(String[] args) {
System.out.println("Test");
OpenCV.loadShared();
String sourceImagePath = "src/main/resources/face.jpeg";
String targetImagePath = "src/main/resources/result.jpeg";
Mat loadedImage = loadImage(sourceImagePath);
int minFaceSize = Math.round(loadedImage.rows() * 0.1f);
MatOfRect facesDetected = new MatOfRect();
CascadeClassifier cascadeClassifier = new CascadeClassifier();
cascadeClassifier.load("./src/main/resources/haarcascades/haarcascade_frontalface_alt.xml");
cascadeClassifier.detectMultiScale(loadedImage,
facesDetected,
1.1,
3,
Objdetect.CASCADE_SCALE_IMAGE,
new Size(minFaceSize, minFaceSize),
new Size()
);
Rect[] facesArray = facesDetected.toArray();
for(Rect face : facesArray) {
Imgproc.rectangle(loadedImage, face.tl(), face.br(), new Scalar(0, 0, 255), 3);
}
saveImage(loadedImage, targetImagePath);
}
public static Mat loadImage(String imagePath) {
Imgcodecs imageCodecs = new Imgcodecs();
return imageCodecs.imread(imagePath);
}
public static void saveImage(Mat imageMatrix, String targetPath) {
Imgcodecs imgcodecs = new Imgcodecs();
imgcodecs.imwrite(targetPath, imageMatrix);
}
}
我的pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sigubrat-nerfThis</groupId>
<artifactId>NerfThis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>NerfThis</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>4.7.0-0</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>NerfThis</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我可以使用Maven很好地构建它,但是当我尝试运行jar时,我得到以下错误:
java -jar target/NerfThis-1.0-SNAPSHOT.jar
Error: Unable to initialize main class NerfThis
Caused by: java.lang.NoClassDefFoundError: org/opencv/core/Mat
我已经调试了几个小时,现在不能理解为什么它的挣扎,以找到定义垫。
1条答案
按热度按时间mqkwyuun1#
发现问题:我的shade-plugin没有运行,因为它在pluginManagement部分。把它移出来,并完全删除了解决它的pluginManagement。