package com.bonus.sdk; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.HashSet; import java.util.Set; public class YoloSdk implements AutoCloseable { private long nativeHandle; // 指向 C++ YoloDetector 实例的指针 private static final Set loadedLibraries = new HashSet<>(); private static Path tempDir; // 用于存放所有本地库的唯一临时目录 /** * 静态初始化块,用于加载本地库。 */ static { try { // 1. 创建一个唯一的临时目录,用于存放所有DLL/SO文件 tempDir = Files.createTempDirectory("yolo_sdk_native_libs_"); // 2. 确保在JVM退出时删除此目录 tempDir.toFile().deleteOnExit(); // 3. 将所有库解压并加载到此目录 loadSdkLibrary(tempDir); } catch (IOException e) { throw new RuntimeException("CRITICAL: Failed to create temp directory for native libraries", e); } } /** * 负责解压和加载所有本地库。 * @param tempDir */ private static void loadSdkLibrary(Path tempDir) throws IOException { String osName = System.getProperty("os.name").toLowerCase(); String osArch = System.getProperty("os.arch").toLowerCase(); String libPathInJar; String sdkLibName; String[] dependencyLibs = {}; if (osName.contains("win") && osArch.contains("64")) { libPathInJar = "/lib/win-x64/"; // 依赖项必须按加载顺序列出 dependencyLibs = new String[]{ "onnxruntime.dll", "abseil_dll.dll", "libprotobuf.dll", "zlib1.dll", "opencv_core4.dll", "opencv_imgproc4.dll", "opencv_dnn4.dll" }; sdkLibName = "my_yolo_sdk.dll"; } else if ((osName.contains("nix") || osName.contains("nux")) && osArch.contains("64")) { libPathInJar = "/lib/linux-x86_64/"; // 确保这些是您Linux构建所需的 .so 依赖项 dependencyLibs = new String[]{ "libonnxruntime.so.1.23.2", // 示例,请使用您确切的文件名 "libopencv_core.so.4.6.0", "libopencv_imgproc.so.4.6.0", "libopencv_dnn.so.4.6.0" }; sdkLibName = "libmy_yolo_sdk.so"; } else { throw new UnsupportedOperationException("Unsupported OS/Arch: " + osName + "/" + osArch); } // --- 关键的修复逻辑 --- // 步骤 1: 首先,将所有库文件从JAR解压到同一个临时目录,保持原始文件名 for (String lib : dependencyLibs) { extractLibraryFromJar(libPathInJar + lib, tempDir); } // 解压主SDK库 extractLibraryFromJar(libPathInJar + sdkLibName, tempDir); // 步骤 2: 然后,按顺序从该临时目录加载库 // 这样,Windows/Linux加载器可以在同一目录中找到所有依赖项 try { for (String lib : dependencyLibs) { if (!loadedLibraries.contains(lib)) { System.load(tempDir.resolve(lib).toAbsolutePath().toString()); loadedLibraries.add(lib); } } if (!loadedLibraries.contains(sdkLibName)) { System.load(tempDir.resolve(sdkLibName).toAbsolutePath().toString()); loadedLibraries.add(sdkLibName); } } catch (UnsatisfiedLinkError e) { // 提供更详细的错误信息 System.err.println("--- NATIVE LIBRARY LOAD FAILED ---"); System.err.println("Failed to load native library. This often means a dependency is missing on the host system."); System.err.println("For Windows: Ensure 'Visual C++ Redistributable for Visual Studio (x64)' is installed."); System.err.println("Libraries were extracted to: " + tempDir.toAbsolutePath()); System.err.println("-----------------------------------"); throw e; // 重新抛出原始错误 } } /** * * @param pathInJar * @param tempDir */ private static void extractLibraryFromJar(String pathInJar, Path tempDir) throws IOException { String libName = new File(pathInJar).getName(); try (InputStream in = YoloSdk.class.getResourceAsStream(pathInJar)) { if (in == null) { throw new FileNotFoundException("Library " + pathInJar + " not found in JAR."); } // 目标文件路径,保持原始文件名 Path targetFile = tempDir.resolve(libName); // 将文件从JAR复制到临时目录 Files.copy(in, targetFile, StandardCopyOption.REPLACE_EXISTING); // 为解压的目录/文件设置退出时删除(双重保险) targetFile.toFile().deleteOnExit(); } } // --- Native JNI 方法 --- private native long nativeInit(String modelPath, int inputWidth, int inputHeight); private native void nativeRelease(long handle); private native Detection[] nativePredict( long handle, byte[] bgrBytes, int imageWidth, int imageHeight, float confThreshold, float iouThreshold ); // --- 公共 Java API --- /** * 初始化YOLO SDK。 * @param modelPath * @param inputWidth * @param inputHeight */ public YoloSdk(String modelPath, int inputWidth, int inputHeight) { this.nativeHandle = nativeInit(modelPath, inputWidth, inputHeight); if (this.nativeHandle == 0) { throw new RuntimeException("Failed to initialize native YOLO SDK. Check logs for details."); } } /** * @param image * @param confThreshold * @param iouThreshold * @return */ public Detection[] predict(BufferedImage image, float confThreshold, float iouThreshold) { if (this.nativeHandle == 0) { throw new IllegalStateException("SDK already closed or failed to initialize."); } byte[] bgrBytes = getBgrBytes(image); return nativePredict( this.nativeHandle, bgrBytes, image.getWidth(), image.getHeight(), confThreshold, iouThreshold ); } /** * 释放本地资源。 */ @Override public void close() { if (this.nativeHandle != 0) { nativeRelease(this.nativeHandle); this.nativeHandle = 0; } } /** * * @param image * @return */ private byte[] getBgrBytes(BufferedImage image) { // 如果图像已经是 3-byte BGR,直接返回 if (image.getType() == BufferedImage.TYPE_3BYTE_BGR) { return ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); } // 否则,创建一个 BGR 副本 BufferedImage bgrImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR); bgrImage.getGraphics().drawImage(image, 0, 0, null); return ((DataBufferByte) bgrImage.getRaster().getDataBuffer()).getData(); } }