Enable OpenCV with java8. (For myself)

Primitive man running Java on the command line

I introduced it because I have to use OpenCV at the university graduate research That way Even if I searched now, I had a hard time because most of the stories were in Python and C ++ and there were few stories about java.

Reference site

I basically obeyed the following sites. Basically, this one is more polite. However, with the latest version (4.4.0), the test code cannot be executed well, so I will write it there. Use OpenCV in Java| JProgramer

Isostall

It is good to execute the downloaded .exe file and extract it to the C drive, so I created a folder called OpenCV and extracted it there. 7-Zip self-extracting archive 2020_08_20 15_43_43.png

Pass through

Although it is a user variable on the reference site, I added it to the system variable. Select "Path" and click [Edit]. At the bottom C:\OpenCV\opencv\build\java\x86 If you just want to use OpenCV in java, you can add this much, and if you use it in Python or Visual Studio, this is not enough. I have VS2017 in it, so I'm passing it through. クラスパス.PNG

The last part is [x86] if java is 32bit, [x64] if java is 64bit Since my machine is 64bit, when I thought it was 64bit and executed the test code (described later), I got an error like "I am 32bit! 64bit program is unreasonable!", So the machine and java bit are not necessarily the same. Does not seem to be limited.

Test code

The test code of the reference site gives an error in Ver.4.4.0. Specifically, the image output class has moved to `ʻimgcodecsinstead of highgui``.

Test.java


import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
 
public class Test{
    public static void main(String[] args){
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat redImg=new Mat(100,200,CvType.CV_8UC3,new Scalar(0,0,255));
        Imgcodecs.imwrite("test.jpg ",redImg);
    }
}

The compile command is below ↓

javac -classpath “C:\Saved folder\opencv\build\java\opencv-version.jar” Test.java

this

java -cp .;C:\Saved folder\opencv\build\java\opencv-version.jar Test

When executed with, a bright red JPG image was output (saved) in the same folder.

When you set the classpath

javac Test.java java Test

You can do it with. Please see the reference site for details.

Recommended Posts

Enable OpenCV with java8. (For myself)
Links & memos for getting started with Java (for myself)
Enable Java EE with NetBeans 9
[OpenCV3.2.0] Eclipse (Java) settings (for Mac)
Getting Started with Ruby for Java Engineers
Learn Java with "So What" [For beginners]
I tried using OpenCV with Java + Tomcat
Interoperability tips with Kotlin for Java developers
Build OpenCV with Java Wrapper on Ubuntu 18.04
For JAVA learning (2018-03-16-01)
2017 IDE for Java
Java for statement
[Java] How to test for null with JUnit
Summary of Java environment settings for myself [mac]
Ask for n business days later with JAVA
Simple obstacle racing made with processing for Java
Docker Container Operations with Docker-Client API for Java
Enable / disable SNI in Java for each communication
Install java with Homebrew
[Java] for statement, while statement
Desktop: OpenCV Use EqualizeHist With Add Weight For RGB
Change seats with java
Install Java with Ansible
[Java] Package for management
[Java] for statement / extended for statement
Desktop: OpenCV Java Repository
Java programming exercises for newcomers unpopular with active engineers
Comfortable download with JAVA
[Java basics] Let's make a triangle with a for statement
Get Azure App Service for Java Configuration with System.getEnv ()
Generate dummy data for various tests with Faker (java)
Switch java with direnv
Use OpenCV in Java
NLP for Java (NLP4J) (2)
[Java] Environment construction procedure for developing struts 1.3 with Eclipse
(Memo) Java for statement
NLP for Java (NLP4J) (1)
(For myself) Build gitlab with ubuntu18.04 + docker for home (Note)
Download Java with Ansible
Try connecting to AzureCosmosDB Emulator for Docker with Java
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Let's scrape with Java! !!
Nested class (for myself)
Build Java with Wercker
Endian conversion with JAVA
Store in Java 2D map and turn with for statement
Deploying a Java environment with Windows Subsystem for Linux (WSL)
Investigated how to call services with Watson SDK for Java
[Java] Sample project for developing web applications with Spring Boot
Java multi-project creation with Gradle
Getting Started with Java Collection
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Java update for Scala users
Java to play with Function
Try DB connection with Java