Android JavaCameraView - Huge frame rate drop in newer APIs

asked 2018-08-27 16:11:57 -0500

Yel gravatar image

I'm working on a simple application that get frames from my smartphone camera. When my phone has older APIs (24 and below) I always used to get 30 fps, since none image processing was being done. For my surprise, when I tried to use that application on the same phone with Android Oreo 8.0 (API 26), I can only get 7.5 fps, even if I change the camera resolution to minimum (320x240). I tried to add:

android:largeHeap="true"

in App's Manifest but it didn't work. I tried to update Opencv (from 3.1.0 to 3.4.2), Android Studio software (2.x.x to 3.1.4), build gradle, but nothing happened. The same code, the same phone, different OS's (so as API) and the FPS was reduced by 75%.

Other specifications: Motorola Z-Play (3gb Ram), Android 8.0, API 26.

Main Activity

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{

JavaCameraView javaCameraView;
Mat mRgba;

BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status){
            case BaseLoaderCallback.SUCCESS:
                javaCameraView.enableView();
                break;
            default:
                super.onManagerConnected(status);
                break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    javaCameraView = (JavaCameraView)findViewById(R.id.java_camera_view);
    javaCameraView.setVisibility(View.VISIBLE);
    javaCameraView.setCvCameraViewListener(this);
    javaCameraView.setCameraIndex(1);
    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    javaCameraView.setMaxFrameSize(640,480);
    javaCameraView.enableFpsMeter();

}

@Override
protected void onPause(){
    super.onPause();
    if(javaCameraView != null)
        javaCameraView.disableView();
}

protected void onDestroy(){
    super.onDestroy();
    if(javaCameraView != null)
        javaCameraView.disableView();
}

protected void onResume(){
    super.onResume();
    if(OpenCVLoader.initDebug()){
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
    else {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mLoaderCallback);
    }
}

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

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

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

}

edit retag flag offensive close merge delete