This project demonstrates real-time edge detection on Android using the camera and OpenCV 4.12 SDK. The app has a clean, minimal design, making it an ideal starting point for learning how to integrate OpenCV into Android applications.
- Real-time Edge Detection: Processes live camera frames using the Canny algorithm.
- Full-Screen Display: Displays only the camera view in a minimal layout.
- Auto-Rotation: Automatically rotates the screen based on device orientation (fullSensor).
- Modular Image Processing: Organizes image algorithms for easy extension.
- Ultra-Simple Design: Maintains a minimal and clear project structure and code.
Camera preview |
Canny edge detection |
- OpenCV for Android: Open-source library for image processing.
- MainActivity.java Structure:
- cannyedge-camerax — Uses the CameraX lifecycle to manage the camera and processes images with ImageAnalysis.
- cannyedge — Extends
AppCompatActivityand follows the Android Activity lifecycle. For extendingCameraActivity, refer to MainExtendsCam.java. - Image algorithms are organized in a modular way.
- Minimum Android Version: Android 7.0 (API level 24).
Follow the steps below to clone and run this project on your own system.
- Android Studio Narwhal 3
- OpenJDK 17 — OpenCV Prerequisite
- opencv-4.12.0-android-sdk — Download File
YourProject/
├── assets/
├── cannyedge/
├── opencv-sdk/ <-- add module dependency
├── README.md
└── settings.gradleClone the project
-
Import this project as a module in Android Studio.
- Click File -> New -> Import module... and select cannyedge path
-
Follow the OpenCV official tutorial to import the OpenCV SDK as a module.
- Click File -> New -> Import module... and select OpenCV SDK path
-
Ensure that
settings.gradlecorrectly includes the modules:include ':opencv-sdk' include ':cannyedge'
-
Follow the OpenCV official tutorial to add the OpenCV module to this project as a Module dependency.
- Click File -> Project structure... -> Dependencies -> All modules -> + (Add Dependency button) -> Module dependency
-
Ensure that
build.gradleproperly sets the OpenCV SDK as a module dependency:dependencies { implementation project(':opencv-sdk') }CameraX project only: add dependencies (Click to expand)
dependencies { def camerax_version = "1.3.1" implementation "androidx.camera:camera-core:${camerax_version}" implementation "androidx.camera:camera-camera2:${camerax_version}" implementation "androidx.camera:camera-lifecycle:${camerax_version}" implementation "androidx.camera:camera-view:${camerax_version}" } -
Follow the OpenCV sample script, and make sure the
build.gradleincludes the following configuration:buildTypes { debug { packagingOptions { doNotStrip '**/*.so' // controlled by OpenCV CMake scripts } } release { packagingOptions { doNotStrip '**/*.so' // controlled by OpenCV CMake scripts } } } -
Build and run — the app will display the real-time Canny edge detection output on your device.
| Cause | Solution |
|---|---|
| OpenCV not initialized | Call OpenCVLoader.initLocal() in onResume() |
| Camera permission not granted | Request permission using requestPermissions() |
| setCameraPermissionGranted() not called | Add it before enableView() |
This project was developed by AdevLog. Documentation and code suggestions were provided by ChatGPT, Google Gemini, and Claude.
This project is licensed under the MIT License. See the LICENSE file for details.

