An Android application that captures camera frames, processes them using OpenCV Canny edge detection in C++, and displays the results in real-time using OpenGL ES 2.0.
-
Camera Feed Integration
- Real-time camera capture using CameraX API
- YUV420_888 format handling with proper stride management
- Efficient frame processing with background threading
-
Frame Processing via OpenCV (C++)
- Native C++ implementation using JNI bridge
- Canny edge detection with Gaussian blur preprocessing
- Morphological operations for edge enhancement
- Optimized grayscale (Y-plane) processing
-
Real-time OpenGL ES Rendering
- Custom OpenGL ES 2.0 renderer with vertex/fragment shaders
- Efficient texture streaming and updates
- Smooth real-time performance (10-15 FPS minimum)
- Modular Architecture: Clean separation between Java UI, JNI bridge, and native processing
- Memory Management: Proper resource cleanup and efficient buffer handling
- Error Handling: Comprehensive error checking for OpenCV, OpenGL, and camera operations
- Thread Safety: Background processing with UI thread synchronization
The app displays:
- White edges on black background showing detected object boundaries
- Real-time processing of camera feed
- Responsive edge detection when moving camera across objects
- Android Studio 4.2 or later
- Android NDK 25.1.8937393 or later
- Minimum SDK: API 21 (Android 5.0)
- Target SDK: API 34
-
Download OpenCV Android SDK
# Download from https://opencv.org/releases/ # Extract opencv-android-sdk.zip
-
Copy OpenCV Files
# Copy native libraries cp -r OpenCV-android-sdk/sdk/native/ app/src/main/cpp/opencv/ # Copy Java wrapper (if using OpenCV Java API) cp -r OpenCV-android-sdk/sdk/java/ app/src/main/java/opencv/
-
Project Structure
app/src/main/ βββ cpp/ β βββ opencv/ # OpenCV native libraries β β βββ jni/ β β βββ libs/ β βββ CMakeLists.txt β βββ native-lib.cpp β βββ opencv_processor.cpp βββ java/com/example/edgedetector/ β βββ MainActivity.java β βββ gl/ β β βββ GLRenderer.java β β βββ GLTextureView.java β βββ jni/ β βββ OpenCVProcessor.java βββ res/layout/ βββ activity_main.xml
-
Configure build.gradle (Module: app)
android { ndkVersion "25.1.8937393" externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" } } }
-
Build Project
# In Android Studio Build β Clean Project Build β Rebuild Project -
Run on Device
- Deploy to physical Android device (recommended for camera testing)
- Grant camera permissions when prompted
Camera β ImageAnalysis β JNI Bridge β OpenCV C++ β JNI Bridge β OpenGL β Display
- MainActivity: Orchestrates camera setup and lifecycle management
- CameraX Integration: Handles camera permissions, configuration, and frame capture
- GLTextureView: Custom OpenGL surface view for rendering processed frames
- OpenCVProcessor.java: Java interface for native method calls
- native-lib.cpp: JNI implementation bridging Java and C++
- Memory Management: Efficient byte array handling between Java and native code
- OpenCV Integration: Computer vision processing using OpenCV C++ API
- Canny Edge Detection: Multi-stage edge detection algorithm
- Gaussian blur for noise reduction
- Canny algorithm with dual thresholding
- Morphological operations for edge enhancement
- Format Conversion: YUV to grayscale to RGB processing
- GLRenderer: Custom OpenGL ES 2.0 renderer
- Shader Programs: Vertex and fragment shaders for texture mapping
- Texture Streaming: Efficient real-time texture updates
-
Camera Capture
CameraX β YUV420_888 ImageProxy β Y-plane extraction -
Native Processing
byte[] yData β JNI β cv::Mat β GaussianBlur β Canny β cvtColor β byte[]
-
OpenGL Rendering
byte[] β ByteBuffer β glTexImage2D β Fragment Shader β Screen
// Camera
implementation 'androidx.camera:camera-core:1.3.0'
implementation 'androidx.camera:camera-camera2:1.3.0'
implementation 'androidx.camera:camera-lifecycle:1.3.0'
implementation 'androidx.camera:camera-view:1.3.0'
// Native
// OpenCV C++ (included as native libraries)
// OpenGL ES 2.0 (system)
// Android NDK





