# OpenCV 4.5.3 Android OnCameraFrame not being called

**URL:** <https://forum.opencv.org/t/opencv-4-5-3-android-oncameraframe-not-being-called/4936>\
**Category:** Android/Java\
**Created:** [August 26, 2021, 2:17pm UTC](https://forum.opencv.org/t/opencv-4-5-3-android-oncameraframe-not-being-called/4936 "2021-08-26T14:17:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nipun\_Ramani](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nipun_ramani/32/2969_2.png) [@Nipun\_Ramani](https://forum.opencv.org/u/Nipun_Ramani)\
**Post date:** [August 26, 2021, 2:17pm UTC](https://forum.opencv.org/t/opencv-4-5-3-android-oncameraframe-not-being-called/4936/1 "2021-08-26T14:17:19Z")

</div>

Hi everyone, I tried using OpenCV 4.5.3 in my android app but for some reason OnCameraFrame is not being called even though OnResume and OnCreate is called.  
Here is my code

```auto
package com.example.opencvproject;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraActivity;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.video.DenseOpticalFlow;
import org.opencv.video.FarnebackOpticalFlow;
import org.opencv.video.Video;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static org.opencv.core.CvType.CV_32F;
import static org.opencv.core.CvType.CV_64F;
import static org.opencv.core.CvType.CV_8U;
import static org.opencv.core.CvType.CV_8UC3;
import static org.opencv.imgproc.Imgproc.COLORMAP_VIRIDIS;
import static org.opencv.imgproc.Imgproc.COLOR_HSV2BGR;
import static org.opencv.imgproc.Imgproc.Sobel;
import static org.opencv.imgproc.Imgproc.applyColorMap;
import static org.opencv.imgproc.Imgproc.cvtColor;

public class MainActivity extends CameraActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
    private static final String TAG = "OCVSample::Activity";
    private CameraBridgeViewBase mOpenCvCameraView;
    private ImageView imageView;
    private Mat mRGB;
    private Mat mGray, mGrayT, prevMat;
    boolean isLoaded = false;
    private DenseOpticalFlow denseOpticalFlow;
    boolean first = true;
    private List<Mat> frames;
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    isLoaded = true;
                    Log.i("OpenCV", "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                    frameDeque = new LinkedList<Mat>();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Log.i("HELLO:", "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_hello_open_cv_layout);
        mOpenCvCameraView = (JavaCameraView) findViewById(R.id.HelloOpenCvView);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    public void onCameraViewStarted(int width, int height) {
        mGray = new Mat(height, width, CvType.CV_8UC4);
    }

    @Override
    public void onCameraViewStopped() {
        mGray.release();
    }

    @Override
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        return inputFrame.rgba();
    }

   
    @Override
    public void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture){

    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        if(mOpenCvCameraView != null){
            mOpenCvCameraView.disableView();
        }
    }

}

```

activity\_hello\_open\_cv\_layout.xml

```auto
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/fullscreenBackgroundColor"
    android:theme="@style/ThemeOverlay.OpencvProject.FullscreenContainer"
    tools:context=".HelloOpenCvLayout">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <TextView
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:keepScreenOn="true"
        android:text="@string/dummy_content"
        android:textColor="?attr/fullscreenTextColor"
        android:textSize="50sp"
        android:textStyle="bold" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <org.opencv.android.JavaCameraView
            android:id="@+id/HelloOpenCvView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:visibility="gone"
            opencv:camera_id="any"
            opencv:show_fps="true" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5" />

    </LinearLayout>

</FrameLayout>

```

AndroidManifest

```auto
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.opencvproject">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.OpencvProject">
        <activity
            android:name=".HelloOpenCvLayout"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_hello_open_cv_layout"
            android:theme="@style/Theme.OpencvProject.Fullscreen"
            android:screenOrientation="landscape"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <supports-screens android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
</manifest>

```

build.gradle

```auto
plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.opencvproject"
        minSdkVersion 28
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        applicationId "opencv.org"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main {
            jni {
                srcDirs 'src\\main\\jni', 'src\\main\\jniLibs'
            }
        }
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation project(path: ':sdk')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

```

Can this be due to improper bindings when the apk is running, i.e, an issue with jni libs not being loaded properly?  
my app directory  
I copied 4 libs in my jniLibs directory from the opencv4.5.3 directory sdk/java/libs  
 ![studio64_f0LAXHHjdH](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/8/8ef340f1c078460ef671404befd3a004b545a595.png)

---

<div class="post-metadata">

**Author:** ![Nipun\_Ramani](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nipun_ramani/32/2969_2.png) [@Nipun\_Ramani](https://forum.opencv.org/u/Nipun_Ramani)\
**Post date:** [August 26, 2021, 2:59pm UTC](https://forum.opencv.org/t/opencv-4-5-3-android-oncameraframe-not-being-called/4936/2 "2021-08-26T14:59:24Z")

</div>

For reference this is what I copied from my opencv4 sdk directory  
 ![studio64_NK5xgLNNWr](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/0bc44ff696b4fc11c9b0501ea0f2e32fa29efa16.png)

Here are the logs when I run the app on my phone

```auto

08/26 20:36:30: Launching 'app' on Google Pixel 4a.
Install successfully finished in 660 ms.
$ adb shell am start -n "opencv.org/com.example.opencvproject.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 13073 on device 'google-pixel_4a-0B231JEC206204'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/opencv.org: Late-enabling -Xcheck:jni
I/opencv.org: Unquickening 12 vdex files!
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/HELLO:: called onCreate
D/CameraBridge: Attr count: 7
D/OpenCV/StaticHelper: Trying to get library list
E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
D/OpenCV/StaticHelper: Library list: ""
    First attempt to load libs
    Trying to init OpenCV libs
    Trying to load library opencv_java4
D/OpenCV/StaticHelper: Library opencv_java4 loaded
    First attempt to load libs is OK
I/OpenCV/StaticHelper: General configuration for OpenCV 4.5.3 =====================================
      Version control: 4.5.3
      Platform:
        Timestamp: 2021-07-05T14:13:06Z
        Host: Linux 4.4.0-210-generic x86_64
        Target: Android 1 aarch64
        CMake: 3.6.0-rc2
        CMake generator: Ninja
        CMake build tool: /opt/android/android-sdk.gradle/cmake/3.6.4111459/bin/ninja
        Configuration: Release
      CPU/HW features:
        Baseline: NEON FP16
      C/C++:
        Built as dynamic libs?: NO
        C++ standard: 11
        C++ Compiler: /opt/android/android-ndk-r18b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ (ver 7.0)
        C++ flags (Release): -isystem /opt/android/android-ndk-r18b/sysroot/usr/include/aarch64-linux-android -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
        C++ flags (Debug): -isystem /opt/android/android-ndk-r18b/sysroot/usr/include/aarch64-linux-android -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG -g
        C Compiler: /opt/android/android-ndk-r18b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
        C flags (Release): -isystem /opt/android/android-ndk-r18b/sysroot/usr/include/aarch64-linux-android -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
        C flags (Debug): -isystem /opt/android/android-ndk-r18b/sysroot/usr/include/aarch64-linux-android -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG -g
        Linker flags (Release): -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /opt/android/android-ndk-r18b/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/opt/android/android-ndk-r18b/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--as-needed  
        Linker flags (Debug): -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /opt/android/android-ndk-r18b/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/opt/android/android-ndk-r18b/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--as-needed  
        ccache: YES
I/OpenCV/StaticHelper: Precompiled headers: NO
        Extra dependencies: z dl m log
        3rdparty dependencies: libcpufeatures libprotobuf ade tbb ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf quirc tegra_hal
      OpenCV modules:
        To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo stitching video videoio
        Disabled: world
        Disabled by dependency: -
        Unavailable: python2 python3 ts
        Applications: -
        Documentation: NO
        Non-free algorithms: NO
      Android NDK: /opt/android/android-ndk-r18b (ver 18.1.5063045)
        Android ABI: arm64-v8a
        NDK toolchain: aarch64-linux-android-clang
        STL type: c++_shared
        Native API level: 21
      Android SDK: /opt/android/android-sdk.gradle (tools: 26.1.1 build tools: 28.0.3)
      GUI: 
      Media I/O: 
        ZLib: z (ver 1.2.7)
        JPEG: build-libjpeg-turbo (ver 2.1.0-62)
        WEBP: build (ver encoder: 0x020f)
        PNG: build (ver 1.6.37)
        TIFF: build (ver 42 - 4.2.0)
        JPEG 2000: build (ver 2.4.0)
        OpenEXR: build (ver 2.3.0)
        HDR: YES
        SUNRASTER: YES
        PXM: YES
        PFM: YES
      Video I/O:
      Parallel framework: TBB (ver 2020.2 interface 11102)
      Trace: YES (with Intel ITT)
      Other third-party libraries:
        Custom HAL: YES (carotene (ver 0.0.1))
        Protobuf: build (3.5.1)
      Python (for build): /usr/bin/python2.7
      Java: export all functions
        ant: NO
        Java wrappers: YES
        Java tests: NO
      Install to: /build/master_pack-android/build/o4a/install
    -----------------------------------------------------------------
D/OCVSample::Activity: OpenCV library found inside package. Using it!
I/OpenCV: OpenCV loaded successfully
D/CameraBridge: call checkCurrentState
I/AdrenoGLES-0: QUALCOMM build : 85da404, I46ff5fc46f
    Build Date : 11/30/20
    OpenGL ES Shader Compiler Version: EV031.31.04.01
    Local Branch : promo490_3_Google
    Remote Branch : 
    Remote Branch : 
    Reconstruct Branch : 
    Build Config : S P 10.0.4 AArch64
    Driver Path : /vendor/lib64/egl/libGLESv2_adreno.so
I/AdrenoGLES-0: PFP: 0x016ee189, ME: 0x00000000
W/AdrenoUtils: <ReadGpuID_from_sysfs:197>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
    <ReadGpuID:221>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
D/CameraBridge: call surfaceChanged event
    call checkCurrentState
I/Gralloc4: mapper 4.x is not supported

```
