Cross-platform Java bindings for 60+ native C/C++ libraries including OpenCV, FFmpeg, PyTorch, TensorFlow, and scientific computing libraries
—
OpenCV (Open Source Computer Vision Library) bindings providing comprehensive image processing, computer vision, and machine learning capabilities for Java applications.
Load, save, and convert images between different formats.
/**
* Load an image from file
* @param filename Path to image file
* @return Mat object containing the image data
*/
public static native Mat imread(String filename);
/**
* Load an image from file with specific flags
* @param filename Path to image file
* @param flags Loading flags (IMREAD_COLOR, IMREAD_GRAYSCALE, etc.)
* @return Mat object containing the image data
*/
public static native Mat imread(String filename, int flags);
/**
* Save an image to file
* @param filename Output file path
* @param img Image data to save
* @return true if successful, false otherwise
*/
public static native boolean imwrite(String filename, Mat img);
/**
* Encode image to memory buffer
* @param ext File extension (e.g., ".jpg", ".png")
* @param img Image to encode
* @param buf Output buffer containing encoded image
* @return true if successful
*/
public static native boolean imencode(String ext, Mat img, ByteVector buf);Usage Examples:
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
// Load image
Mat image = imread("input.jpg", IMREAD_COLOR);
Mat grayscale = imread("input.jpg", IMREAD_GRAYSCALE);
// Save image
imwrite("output.png", image);
// Encode to memory
ByteVector buffer = new ByteVector();
imencode(".jpg", image, buffer);Core matrix and image data structure with comprehensive operations.
/**
* OpenCV Mat class representing n-dimensional dense arrays
*/
public class Mat extends Pointer {
/**
* Default constructor - empty matrix
*/
public Mat();
/**
* Create matrix with specified dimensions and type
* @param rows Number of rows
* @param cols Number of columns
* @param type Matrix type (CV_8UC1, CV_32FC3, etc.)
*/
public Mat(int rows, int cols, int type);
/**
* Create matrix with specified size and type
* @param size Matrix dimensions
* @param type Matrix type
*/
public Mat(Size size, int type);
/**
* Create matrix filled with scalar value
* @param rows Number of rows
* @param cols Number of columns
* @param type Matrix type
* @param s Fill value
*/
public Mat(int rows, int cols, int type, Scalar s);
/** Get number of rows */
public native int rows();
/** Get number of columns */
public native int cols();
/** Get matrix type */
public native int type();
/** Get number of channels */
public native int channels();
/** Get matrix size */
public native Size size();
/** Check if matrix is empty */
public native boolean empty();
/** Clone the matrix */
public native Mat clone();
/** Copy matrix to another matrix */
public native void copyTo(Mat m);
/** Convert matrix type */
public native void convertTo(Mat m, int rtype);
}
/**
* 2D Point structure
*/
public class Point extends Pointer {
public Point();
public Point(double x, double y);
public native double x();
public native double y();
public native Point x(double x);
public native Point y(double y);
}
/**
* Size structure
*/
public class Size extends Pointer {
public Size();
public Size(double width, double height);
public native double width();
public native double height();
}
/**
* Rectangle structure
*/
public class Rect extends Pointer {
public Rect();
public Rect(int x, int y, int width, int height);
public native int x();
public native int y();
public native int width();
public native int height();
}
/**
* Scalar values for multi-channel operations
*/
public class Scalar extends Pointer {
public Scalar();
public Scalar(double val0);
public Scalar(double val0, double val1, double val2);
public Scalar(double val0, double val1, double val2, double val3);
}Core image processing operations including filtering, transformations, and color space conversions.
/**
* Convert color space
* @param src Source image
* @param dst Destination image
* @param code Color conversion code (COLOR_BGR2GRAY, COLOR_BGR2HSV, etc.)
*/
public static native void cvtColor(Mat src, Mat dst, int code);
/**
* Resize image
* @param src Source image
* @param dst Destination image
* @param dsize Output image size
*/
public static native void resize(Mat src, Mat dst, Size dsize);
/**
* Resize with interpolation method
* @param src Source image
* @param dst Destination image
* @param dsize Output image size
* @param fx Scale factor along horizontal axis
* @param fy Scale factor along vertical axis
* @param interpolation Interpolation method
*/
public static native void resize(Mat src, Mat dst, Size dsize,
double fx, double fy, int interpolation);
/**
* Gaussian blur filter
* @param src Source image
* @param dst Destination image
* @param ksize Gaussian kernel size
* @param sigmaX Gaussian kernel standard deviation in X direction
*/
public static native void GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX);
/**
* Edge detection using Canny algorithm
* @param image Input image
* @param edges Output edge map
* @param threshold1 Lower threshold for edge linking
* @param threshold2 Upper threshold for edge linking
*/
public static native void Canny(Mat image, Mat edges, double threshold1, double threshold2);
/**
* Morphological operations
* @param src Source image
* @param dst Destination image
* @param op Morphological operation type
* @param kernel Structuring element
*/
public static native void morphologyEx(Mat src, Mat dst, int op, Mat kernel);Detect and extract features from images for computer vision tasks.
/**
* Harris corner detector
* @param src Input image
* @param dst Output corner response map
* @param blockSize Size of averaging window
* @param ksize Aperture parameter for Sobel operator
* @param k Harris detector free parameter
*/
public static native void cornerHarris(Mat src, Mat dst, int blockSize, int ksize, double k);
/**
* Good features to track detector
* @param image Input image
* @param corners Output detected corners
* @param maxCorners Maximum number of corners to return
* @param qualityLevel Quality level for corners
* @param minDistance Minimum distance between corners
*/
public static native void goodFeaturesToTrack(Mat image, Mat corners,
int maxCorners, double qualityLevel, double minDistance);
/**
* SIFT feature detector and descriptor
*/
public class SIFT extends Feature2D {
/**
* Create SIFT detector
* @param nfeatures Number of best features to retain
* @return SIFT detector instance
*/
public static native SIFT create(int nfeatures);
/**
* Detect keypoints and compute descriptors
* @param image Input image
* @param mask Input mask
* @param keypoints Output keypoints
* @param descriptors Output descriptors
*/
public native void detectAndCompute(Mat image, Mat mask,
KeyPointVector keypoints, Mat descriptors);
}
/**
* KeyPoint structure for feature detection
*/
public class KeyPoint extends Pointer {
public KeyPoint();
public KeyPoint(float x, float y, float size);
public native Point2f pt();
public native float size();
public native float angle();
public native float response();
}High-level object detection and recognition capabilities.
/**
* Haar cascade classifier for object detection
*/
public class CascadeClassifier extends Pointer {
public CascadeClassifier();
public CascadeClassifier(String filename);
/**
* Load classifier from file
* @param filename Path to cascade file
* @return true if loaded successfully
*/
public native boolean load(String filename);
/**
* Detect objects in image
* @param image Input image
* @param objects Output detected objects
*/
public native void detectMultiScale(Mat image, RectVector objects);
/**
* Detect objects with scale parameters
* @param image Input image
* @param objects Output detected objects
* @param scaleFactor Scale factor between successive scans
* @param minNeighbors Minimum number of neighbor rectangles
*/
public native void detectMultiScale(Mat image, RectVector objects,
double scaleFactor, int minNeighbors);
}
/**
* HOG (Histogram of Oriented Gradients) descriptor
*/
public class HOGDescriptor extends Pointer {
public HOGDescriptor();
/**
* Set SVM detector for pedestrian detection
*/
public native void setSVMDetector(Mat detector);
/**
* Get default people detector
* @return Default people detector coefficients
*/
public static native Mat getDefaultPeopleDetector();
/**
* Detect objects using HOG + SVM
* @param img Input image
* @param foundLocations Output detection locations
*/
public native void detectMultiScale(Mat img, RectVector foundLocations);
}Neural network inference through OpenCV's DNN module.
/**
* Deep neural network for inference
*/
public class Net extends Pointer {
/**
* Create input blob from image
* @param image Input image
* @return 4D blob suitable for network input
*/
public static native Mat blobFromImage(Mat image);
/**
* Create input blob with parameters
* @param image Input image
* @param scalefactor Multiplier for image values
* @param size Spatial size for output image
* @param mean Scalar with mean values subtracted from channels
* @param swapRB Flag to swap R and B channels
* @return 4D blob
*/
public static native Mat blobFromImage(Mat image, double scalefactor,
Size size, Scalar mean, boolean swapRB);
/**
* Set network input
* @param blob Input blob
*/
public native void setInput(Mat blob);
/**
* Run forward pass
* @return Network output
*/
public native Mat forward();
/**
* Run forward pass with output name
* @param outputName Name of output layer
* @return Network output
*/
public native Mat forward(String outputName);
}
/**
* Read network from files
* @param model Path to binary file with trained weights
* @param config Path to text file with network configuration
* @return Net object
*/
public static native Net readNet(String model, String config);
/**
* Read network from framework-specific files
* @param model Path to model file
* @param config Path to config file
* @param framework Framework name
* @return Net object
*/
public static native Net readNet(String model, String config, String framework);import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgcodecs.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
public class ImageProcessing {
static {
Loader.load(opencv_core.class);
Loader.load(opencv_imgcodecs.class);
Loader.load(opencv_imgproc.class);
}
public static void processImage() {
try (PointerScope scope = new PointerScope()) {
// Load image
Mat image = imread("input.jpg", IMREAD_COLOR);
if (image.empty()) {
System.err.println("Could not load image");
return;
}
// Convert to grayscale
Mat gray = new Mat();
cvtColor(image, gray, COLOR_BGR2GRAY);
// Apply Gaussian blur
Mat blurred = new Mat();
GaussianBlur(gray, blurred, new Size(15, 15), 0);
// Edge detection
Mat edges = new Mat();
Canny(blurred, edges, 50, 150);
// Save result
imwrite("edges.jpg", edges);
}
}
}import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgcodecs.*;
import org.bytedeco.opencv.opencv_objdetect.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
public class FaceDetection {
public static void detectFaces() {
try (PointerScope scope = new PointerScope()) {
// Load cascade classifier
CascadeClassifier faceCascade = new CascadeClassifier();
faceCascade.load("haarcascade_frontalface_alt.xml");
// Load image
Mat image = imread("people.jpg", IMREAD_COLOR);
Mat gray = new Mat();
cvtColor(image, gray, COLOR_BGR2GRAY);
// Detect faces
RectVector faces = new RectVector();
faceCascade.detectMultiScale(gray, faces, 1.1, 3);
// Draw rectangles around faces
for (int i = 0; i < faces.size(); i++) {
Rect face = faces.get(i);
rectangle(image, face, new Scalar(0, 255, 0), 2);
}
imwrite("faces_detected.jpg", image);
}
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-bytedeco--javacpp-presets-platform