Skip to content

Commit

Permalink
Trying to fix native libraries cross platform loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Arxa committed Oct 10, 2017
1 parent 6712171 commit ce06e75
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 124 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/build
/out
/.gradle
.idea
outputs/
src/main/resources/Outputs/
src/main/resources/Natives/
src/main/resources/OCR/
52 changes: 25 additions & 27 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {
}

apply plugin: 'java'
// Uncomment the following plugin to test for unused libraries
// Uncomment the following plugin to test for unused libraries (gradle analyze)
//apply plugin: 'ca.cutterslade.analyze'
apply plugin: 'idea'
apply plugin: 'application'
Expand All @@ -37,30 +37,30 @@ repositories{
mavenCentral()
}

jar {
manifest {
attributes(
'Main-Class': 'Main',
)
}
}

task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}

artifacts {
archives fatJar
}
//jar {
// manifest {
// attributes(
// 'Main-Class': 'Main',
// )
// }
//}
//
//task fatJar(type: Jar) {
// manifest.from jar.manifest
// classifier = 'all'
// from {
// configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
// } {
// exclude "META-INF/*.SF"
// exclude "META-INF/*.DSA"
// exclude "META-INF/*.RSA"
// }
// with jar
//}
//
//artifacts {
// archives fatJar
//}


ext {
Expand All @@ -83,8 +83,6 @@ ext {

}



dependencies {

// Local Libraries
Expand Down
45 changes: 13 additions & 32 deletions src/main/java/Entities/ApplicationPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,51 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ApplicationPaths
{
public static Caller CALLER;
public static String USER_DIR = "";
public static String IDE_DIR = "";
public static String RESOURCES;
public static String USER_DIR;
public static String RESOURCES_NATIVES;
public static String RESOURCES_OCR;
public static String RESOURCES_OCR_TESSDATA;
public static String RESOURCES_OCR_LANGUAGES;
public static String FOLDER_PATH;
public static String RESOURCES_OUTPUTS;
public static String UNIQUE_FOLDER_NAME;
public static String FILE_SEPERATOR;

/***
* Sets the application's directory paths differently whether it is a JAR file calling or an IDE
*/
public static void setApplicationPaths()
{
final File jarFile = new File(ApplicationPaths.class.getProtectionDomain().getCodeSource().getLocation().getPath());

if(jarFile.isFile()) {
if (FilenameUtils.getExtension(jarFile.getPath()).equals("jar")){
CALLER = Caller.JAR;
USER_DIR = jarFile.getParent();
}

RESOURCES = USER_DIR;
RESOURCES_NATIVES = USER_DIR + FILE_SEPERATOR + "Natives";
RESOURCES_OCR = USER_DIR + FILE_SEPERATOR + "OCR";
RESOURCES_OCR_TESSDATA = USER_DIR + FILE_SEPERATOR + "OCR" + FILE_SEPERATOR + "tessdata";
RESOURCES_OCR_LANGUAGES = USER_DIR + FILE_SEPERATOR + "OCR" + FILE_SEPERATOR + "languages" + FILE_SEPERATOR;
FOLDER_PATH = USER_DIR + FILE_SEPERATOR + "Outputs";

CALLER = Caller.JAR;
USER_DIR = jarFile.getParentFile().getPath();
} else {
CALLER = Caller.IDE;
try {
IDE_DIR = new File(".").getCanonicalPath() + FILE_SEPERATOR + "src" +
FILE_SEPERATOR + "main" + FILE_SEPERATOR + "resources" + FILE_SEPERATOR;
RESOURCES = IDE_DIR;
RESOURCES_NATIVES = IDE_DIR + "Natives";
RESOURCES_OCR = IDE_DIR + "OCR";
RESOURCES_OCR_TESSDATA = IDE_DIR + "OCR" + FILE_SEPERATOR + "tessdata";
RESOURCES_OCR_LANGUAGES = "src/main/resources/OCR/languages/";
FOLDER_PATH = "src" + FILE_SEPERATOR + "main" + FILE_SEPERATOR +
"resources" + FILE_SEPERATOR + "Outputs";
USER_DIR = Paths.get(new File(".").getCanonicalPath(),"src","main","resources").toFile().getPath();
} catch (IOException e) {
System.out.println("Couldn't create IDE directories");
Platform.exit();
System.exit(1);
}
}
RESOURCES_NATIVES = Paths.get(USER_DIR, "Natives").toAbsolutePath().toString();
RESOURCES_OCR = Paths.get(USER_DIR, "OCR").toAbsolutePath().toString();
RESOURCES_OCR_TESSDATA = Paths.get(USER_DIR, "OCR","tessdata").toAbsolutePath().toString();
RESOURCES_OCR_LANGUAGES = Paths.get(USER_DIR, "OCR","languages").toAbsolutePath().toString();
RESOURCES_OUTPUTS = Paths.get(USER_DIR, "Outputs").toAbsolutePath().toString();
}

// TODO maybe delete this - not necessary
public static void checkCaller(){
switch (CALLER){
case EXE:
Controllers.getLogController().logTextArea.appendText("EXE called\n");
Controllers.getLogController().logTextArea.appendText("USER_DIR: " + USER_DIR + "\n");
break;
case JAR:
Controllers.getLogController().logTextArea.appendText("JAR called\n");
Controllers.getLogController().logTextArea.appendText("USER_DIR: " + USER_DIR + "\n");
break;
case IDE:
Controllers.getLogController().logTextArea.appendText("IDE called\n");
Expand Down
17 changes: 3 additions & 14 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
import org.opencv.core.Mat;
import org.opencv.core.Scalar;

import java.lang.reflect.Field;

public class Main extends Application
{
@Override
public void start(Stage stage) throws Exception
{
if (SystemUtils.IS_OS_WINDOWS){
ApplicationPaths.FILE_SEPERATOR = "\\";
} else if (SystemUtils.IS_OS_LINUX){
ApplicationPaths.FILE_SEPERATOR = "/";
}
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("Views/main.fxml"));
Parent root = loader.load();
stage.setTitle("VideoText Extractor");
Expand All @@ -37,9 +34,7 @@ public void start(Stage stage) throws Exception
MainController.setMainStage(stage);

FileProcessor.loadLibraries();
//testOpenCV();

// Terminating all Threads when the application is closed. ?
stage.setOnCloseRequest(e -> {
System.exit(0);
});
Expand All @@ -49,11 +44,5 @@ public static void main(String[] args) {
launch(args);
}

private void testOpenCV() {
System.out.println("Welcome to OpenCV " + Core.VERSION);
Controllers.getLogController().logTextArea.appendText("Welcome to OpenCV " + Core.VERSION + "\n");
Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
System.out.println("OpenCV Mat: " + m);
Controllers.getLogController().logTextArea.appendText("OpenCV Mat: " + m + "\n");
}

}
63 changes: 34 additions & 29 deletions src/main/java/Processors/FileProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Processors;

import Entities.ApplicationPaths;
import Entities.Caller;
import Entities.Controllers;
import ViewControllers.MainController;
import javafx.application.Platform;
Expand All @@ -10,8 +11,11 @@
import javafx.stage.Stage;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.Contract;
import org.opencv.core.Core;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -63,18 +67,12 @@ public static boolean createDirectories(File chosenFile)
// Generating unique name of current video file operation
ApplicationPaths.UNIQUE_FOLDER_NAME = chosenFile.getName().replace(".mp4","")+" "+
new Date().toString().replace(":","-");

// Creating paths for write operations
Files.createDirectories(Paths.get(ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR
+ ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR + "Text Blocks"));
Files.createDirectories(Paths.get(ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR
+ ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR + "Painted Frames"));
Files.createDirectories(Paths.get(ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR
+ ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR +"Steps"));
Files.createDirectories(Paths.get(ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR
+ ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR + "Video"));
Files.createDirectories(Paths.get(ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR
+ ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR + "OCR Images"));
// Creating paths for application outputs
Files.createDirectories(Paths.get(ApplicationPaths.RESOURCES_OUTPUTS,ApplicationPaths.UNIQUE_FOLDER_NAME, "Text Blocks"));
Files.createDirectories(Paths.get(ApplicationPaths.RESOURCES_OUTPUTS,"Painted Frames"));
Files.createDirectories(Paths.get(ApplicationPaths.RESOURCES_OUTPUTS,ApplicationPaths.UNIQUE_FOLDER_NAME, "Steps"));
Files.createDirectories(Paths.get(ApplicationPaths.RESOURCES_OUTPUTS,ApplicationPaths.UNIQUE_FOLDER_NAME,"Video"));
Files.createDirectories(Paths.get(ApplicationPaths.RESOURCES_OUTPUTS,ApplicationPaths.UNIQUE_FOLDER_NAME, "OCR Images"));
return true;
}
catch (RuntimeException | IOException ex) {
Expand Down Expand Up @@ -107,28 +105,28 @@ public static File showFileDialog()
//TODO load natives cross platform
public static void loadLibraries() throws IOException, URISyntaxException
{
String path = ApplicationPaths.RESOURCES_NATIVES;
setLibraryPath();
try {
if(SystemUtils.IS_OS_WINDOWS)
{
int bit = Integer.parseInt(System.getProperty("sun.arch.data.model"));
if(bit == 32){
System.load(path + "\\opencv_java320.dll");
System.loadLibrary("opencv_320_32");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Windows 32 bit\n");
System.load(path + "\\openh264-1.7.0-win32.dll");
System.loadLibrary("openh264-1.6.0-win32msvc");
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Windows 32 bit\n");
}
else if (bit == 64){
System.load(path + "\\opencv_java320.dll");
System.loadLibrary("opencv_320_64");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Windows 64 bit\n");
System.load(path + "\\openh264-1.7.0-win64.dll");
System.loadLibrary("openh264-1.6.0-win64msvc");
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Windows 64 bit\n");
}
else{
Controllers.getLogController().logTextArea.appendText("Unknown Windows bit - trying with 32");
System.load(path + "\\opencv_java320.dll");
System.loadLibrary("opencv_320_32");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Windows 32 bit\n");
System.load(path + "\\openh264-1.7.0-win32.dll");
System.loadLibrary("openh264-1.6.0-win32msvc");
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Windows 32 bit\n");
}
}
Expand All @@ -138,23 +136,17 @@ else if(SystemUtils.IS_OS_MAC){
else if(SystemUtils.IS_OS_LINUX){
int bit = Integer.parseInt(System.getProperty("sun.arch.data.model"));
if (bit == 32){
System.load(path + "/libopencv_java320.so");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Linux 32 bit\n");
System.load(path + "/libopenh264-1.7.0-linux32.4.so");
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Linux 32 bit\n");
Controllers.getLogController().logTextArea.appendText("OS not supported yet\n");
}
else if (bit == 64){
System.load(path + "/libopencv_java320.so");
System.loadLibrary("opencv_320_64");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Linux 64 bit\n");
System.load(path + "/libopenh264-1.7.0-linux64.4.so");
System.loadLibrary("openh264-1.6.0-linux64.3");
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Linux 64 bit\n");
}
else {
Controllers.getLogController().logTextArea.appendText("Unknown Linux bit - trying with 32\n");
System.load(path + "/libopencv_java320.so");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Linux 32 bit\n");
System.load(path + "/libopenh264-1.7.0-linux32.4.so");
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Linux 32 bit\n");
Controllers.getLogController().logTextArea.appendText("OS not supported yet\n");
}
}
} catch (Throwable e) {
Expand All @@ -165,4 +157,17 @@ else if (bit == 64){
Platform.exit();
}
}

private static void setLibraryPath() {
try {
System.setProperty("java.library.path", ApplicationPaths.RESOURCES_NATIVES);
Controllers.getLogController().logTextArea.appendText("JavaLibraryPath= " + ApplicationPaths.RESOURCES_NATIVES+"\n");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
}
18 changes: 10 additions & 8 deletions src/main/java/Processors/ImageWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Created by arxa on 26/2/2017.
Expand All @@ -23,8 +25,8 @@ public class ImageWriter
@NotNull
public static File writeTextBlock(Mat frame)
{
String filePath = ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR + ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR +
"Text Blocks" + ApplicationPaths.FILE_SEPERATOR + fileCounter++ + ".png";
String filePath = Paths.get(ApplicationPaths.RESOURCES_OUTPUTS, ApplicationPaths.UNIQUE_FOLDER_NAME,
"Text Blocks", fileCounter++ + ".png").toAbsolutePath().toString();
MatOfInt params = new MatOfInt(Imgcodecs.CV_IMWRITE_PNG_COMPRESSION);
Imgcodecs.imwrite(filePath,frame,params);
return new File(filePath);
Expand All @@ -33,26 +35,26 @@ public static File writeTextBlock(Mat frame)
public static void writePaintedFrame(Mat frame)
{
if (!writingEnabled) return;
String filePath = ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR + ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR +
"Painted Frames" + ApplicationPaths.FILE_SEPERATOR + fileCounter++ + ".png";
String filePath = Paths.get(ApplicationPaths.RESOURCES_OUTPUTS, ApplicationPaths.UNIQUE_FOLDER_NAME,
"Painted Frames", fileCounter++ + ".png").toAbsolutePath().toString();
MatOfInt params = new MatOfInt(Imgcodecs.CV_IMWRITE_PNG_COMPRESSION);
Imgcodecs.imwrite(filePath,frame,params);
}

public static void writeOCRImage(Mat frame)
{
if (!writingEnabled) return;
String filePath = ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR + ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR +
"OCR Images" + ApplicationPaths.FILE_SEPERATOR + fileCounter++ + ".png";
String filePath = Paths.get(ApplicationPaths.RESOURCES_OUTPUTS, ApplicationPaths.UNIQUE_FOLDER_NAME,
"OCR Images", fileCounter++ + ".png").toAbsolutePath().toString();
MatOfInt params = new MatOfInt(Imgcodecs.CV_IMWRITE_PNG_COMPRESSION);
Imgcodecs.imwrite(filePath,frame,params);
}

public static void writeStep(Mat frame)
{
if (!writingEnabled) return;
String filePath = ApplicationPaths.FOLDER_PATH + ApplicationPaths.FILE_SEPERATOR + ApplicationPaths.UNIQUE_FOLDER_NAME + ApplicationPaths.FILE_SEPERATOR +
"Steps" + ApplicationPaths.FILE_SEPERATOR + fileCounter++ + ".png";
String filePath = Paths.get(ApplicationPaths.RESOURCES_OUTPUTS, ApplicationPaths.UNIQUE_FOLDER_NAME,
"Steps", fileCounter++ + ".png").toAbsolutePath().toString();
MatOfInt params = new MatOfInt(Imgcodecs.CV_IMWRITE_PNG_COMPRESSION);
Imgcodecs.imwrite(filePath,frame,params);
}
Expand Down
Loading

0 comments on commit ce06e75

Please sign in to comment.