Ask Your Question

Not able to see camera/ onCameraFrame is not called in signed APK/bundle Android

asked 2020-01-06 00:26:24 -0500

Vismay gravatar image

updated 2020-01-06 05:50:27 -0500

I have developed a face detection application which works fine if installed from Android Studio or via debug APK but camera screen goes black and oncameraFrame() is not called for singed APK/Bundle. OnCameraStarted() gets called for both the apk's unlike onCameraFrame().

edit retag flag offensive close merge delete


can you please post your code?

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:14:20 -0500 )edit

i think you doing something wrong in your code because i think oncameraFrame() return same result in debug or signed apk. it's not return different output for different build variants.

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:17:30 -0500 )edit


override fun onCreate(savedInstanceState: Bundle?){
                  mOpenCvCameraViewBase = findViewById(
                  mOpenCvCameraViewBase?.visibility = View.VISIBLE
    override fun onCameraViewStarted(width: Int, height: Int) {
     mRgba = Mat()
        mGray = Mat()
           override fun onCameraFrame(inputFrame: CameraBridgeViewBase.CvCameraViewFrame): Mat {
            mRgba = inputFrame.rgba()
             mGray = inputFrame.gray()
             return mRgba!! }
      override fun onCameraViewStopped() {  
        mRgba?.release()  }

This code works well expect for release apk/bundle.

Vismay gravatar imageVismay ( 2020-01-06 08:19:55 -0500 )edit

If my code is wrong then why it's working in debug apk and not in signed?

Vismay gravatar imageVismay ( 2020-01-06 08:23:52 -0500 )edit

yes you are right but i have also use onCameraFrame() and it's working fine in both debug and signed

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:27:31 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2020-01-31 04:03:40 -0500

Vismay gravatar image

updated 2020-01-31 05:17:40 -0500

After trying for almost a month now got the solution. Putting it here if anyone faces similar issue in future.

So, before my opencv gradle file looked this sourceSets { main { jniLibs.srcDirs = ['native/libs'] java.srcDirs = ['java/src'] aidl.srcDirs = ['java/src'] res.srcDirs = ['java/res'] manifest.srcFile 'java/AndroidManifest.xml' } }

I just added a single line in my sourceSets which is this jni.srcDirs = ['native/jni', 'native/libs/'] So basically my jni.srcDirs path was missing. Also, I have added the entire OpenCV sdk and not only Java folder unlike others.


After adding above code I generated signed Bundle and tried uploading to Play Store but the Play Store refused the app. Then I removed

debuggable true

from app gradle file and kept it only inside the opencv gradle file and it worked despite removing the above code. So, play store doesn't accepts the bundle if you have set

debuggable true

for your app level gradle file but accepts if any module which has

debuggable true

in it's gradle.

The problem was not in the jni.

edit flag offensive delete link more

answered 2020-01-06 08:25:47 -0500

Akhil Patel gravatar image

updated 2020-01-06 08:28:16 -0500

try this :-

 public void onCameraViewStarted(int width, int height)
        mRgba = new Mat();
        mGray = new Mat();
edit flag offensive delete link more


Updated my comment, I already have this in my code.

Vismay gravatar imageVismay ( 2020-01-06 08:28:13 -0500 )edit

can you put the BaseLoaderCallback method code here?

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:30:51 -0500 )edit


private val mLoaderCallBack = object : BaseLoaderCallback(this) {
        override fun onManagerConnected(status: Int) {
            when (status) {
                LoaderCallbackInterface.SUCCESS -> {
                    Log.d(TAG, "OpenCV loaded successfully")

                else -> super.onManagerConnected(status)

    override fun onResume() {
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization")
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallBack)
        } else {
            Log.d(TAG, "OpenCV library found inside package) }}
Vismay gravatar imageVismay ( 2020-01-06 08:32:19 -0500 )edit

add mOpenCvCameraViewBase.enableView(); in your BaseLoaderCallback

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:33:56 -0500 )edit


private fun initializeOpenCVDependencies() {
        try { // Copy the resource into a temp file so OpenCV can load it
            val inputStream: InputStream = resources.openRawResource(R.raw.lbpcascade_frontalface)
            val cascadeDir = getDir("cascade", Context.MODE_PRIVATE)
            mCascadeFile = File(cascadeDir, "lbpcascade_frontalface.xml")
            val os = FileOutputStream(mCascadeFile!!)
            val buffer = ByteArray(4096)
            var bytesRead: Int
            while ( { bytesRead = it } != -1) {
                os.write(buffer, 0, bytesRead)
Vismay gravatar imageVismay ( 2020-01-06 08:35:34 -0500 )edit


cascadeClassifier = CascadeClassifier(mCascadeFile?.absolutePath)
                    if (cascadeClassifier?.empty()!!) {
                        Log.d(TAG, "Failed to load mCascadeFile")
                    } else {
                        Log.d(TAG, "cascadeClassifier path >> ${mCascadeFile?.absolutePath}")

                } catch (e: Exception) {
                    Log.e(TAG, "Error loading cascade", e)
Vismay gravatar imageVismay ( 2020-01-06 08:36:07 -0500 )edit

Its there check this method

Vismay gravatar imageVismay ( 2020-01-06 08:36:21 -0500 )edit

also mOpenCvCameraViewBase.disableView(); in onPause() and onDestory()

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:36:27 -0500 )edit

disableView() is in onDestroy()

Vismay gravatar imageVismay ( 2020-01-06 08:37:10 -0500 )edit

can you please put the mOpenCvCameraViewBase?.enableView() before initializeOpenCVDependencies() and than run the code.

Akhil Patel gravatar imageAkhil Patel ( 2020-01-06 08:39:38 -0500 )edit

Question Tools

1 follower


Asked: 2020-01-06 00:26:24 -0500

Seen: 643 times

Last updated: Jan 31 '20