Issue 1:
System information (version)

OpenCV => 3.1

Operating System / Platform => Windows 64 Bit

IDE => Android Studio

Description

StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); int bestWidth = 0, bestHeight = 0; float aspect = (float) width / height; android.util.Size[] sizes = map.getOutputSizes(ImageReader.class);

The problem is cause by the getOutputSizes return null. But I can't debug the StreamConfigurationMap Class. Is there anyone know how to solve it? Thanks in advance!

Error Info

05-11 10:13:54.132 29590-29590/com.example.administrator.myapplication E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.administrator.myapplication, PID: 29590

java.lang.RuntimeException: Interrupted while setCameraPreviewSize.

at org.opencv.android.JavaCamera2View.connectCamera(JavaCamera2View.java:320)

at org.opencv.android.CameraBridgeViewBase.onEnterStartedState(CameraBridgeViewBase.java:360)

at org.opencv.android.CameraBridgeViewBase.processEnterState(CameraBridgeViewBase.java:321)

at org.opencv.android.CameraBridgeViewBase.checkCurrentState(CameraBridgeViewBase.java:313)

at org.opencv.android.CameraBridgeViewBase.surfaceChanged(CameraBridgeViewBase.java:198)

at android.view.SurfaceView.updateWindow(SurfaceView.java:688)

at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:202)

at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)

at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2391)

at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1318)

at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6728)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:802)

at android.view.Choreographer.doCallbacks(Choreographer.java:605)

at android.view.Choreographer.doFrame(Choreographer.java:574)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:788)

at android.os.Handler.handleCallback(Handler.java:815)

at android.os.Handler.dispatchMessage(Handler.java:104)

at android.os.Looper.loop(Looper.java:194)

at android.app.ActivityThread.main(ActivityThread.java:5898)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1019)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:814)

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

at org.opencv.android.JavaCamera2View.calcPreviewSize(JavaCamera2View.java:265)

at org.opencv.android.JavaCamera2View.connectCamera(JavaCamera2View.java:300)

Steps to reproduce

Activity

package com.example.administrator.myapplication;

import android.content.Context;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

import android.widget.Toast;

import org.opencv.android.BaseLoaderCallback;

import org.opencv.android.CameraBridgeViewBase;

import org.opencv.android.InstallCallbackInterface;

import org.opencv.android.JavaCamera2View;

import org.opencv.android.LoaderCallbackInterface;

import org.opencv.android.OpenCVLoader;

import org.opencv.core.Core;

import org.opencv.core.CvType;

import org.opencv.core.Mat;

import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {

Mat mat1, mat2, mat3;

CameraBridgeViewBase base;

BaseLoaderCallback callback;

// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}

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

base = findViewById(R.id.camera);
base.setCvCameraViewListener(this);

callback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
switch (status) {
case BaseLoaderCallback.SUCCESS:
base.enableView();
toast("BaseLoaderCallback.SUCCESS");
break;
}
}

@Override
public void onPackageInstall(int operation, InstallCallbackInterface callback) {
super.onPackageInstall(operation, callback);
}
};

}

private void toast(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});
}

/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();

@Override
public void onCameraViewStarted(int width, int height) {
Toast.makeText(this, "onCameraViewStarted", Toast.LENGTH_LONG).show();
mat1 = new Mat(width, height, CvType.CV_8UC4);
mat2 = new Mat(width, height, CvType.CV_8UC4);
mat3 = new Mat(width, height, CvType.CV_8UC4);
}

@Override
public void onCameraViewStopped() {
Toast.makeText(this, "onCameraViewStopped", Toast.LENGTH_LONG).show();
mat1.release();
mat2.release();
mat3.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
toast("onCameraFrame");
mat1 = inputFrame.rgba();

//Core.transpose(mat1, mat2);

//Imgproc.resize(mat2, mat2, mat3.size(),0, 0,0 );

//Core.flip(mat3, mat1, 1);

return mat1;
}

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

@Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Toast.makeText(this, "Load Failed", Toast.LENGTH_LONG).show();
} else {
callback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
Toast.makeText(this, "Load Success", Toast.LENGTH_LONG).show();
}
}

}

XML

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.administrator.myapplication.MainActivity">

<org.opencv.android.JavaCamera2View
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

Issue 2:
System information (version)

OpenCV => 3.4.1

Operating System / Platform => Android 5.0.1

Compiler => -/-

Detailed description

The Camera2 API Classes Camera2Renderer and JavaCamera2View are not functioning correctly. At least on my phone (Huawei P8, Android 5.0.1). JavaCamera2View crashes in Line 265 while accessing sizes[0] of the possible camera sizes with an IndexOutOfBoundsException 0 >= 0, and Camera2Renderer, used through CameraGLSurfaceView results in a preview with a distorted aspect ratio. The Android Samples are using obsolete and outdated code that doesn't even compile with this version of openCV.

Steps to reproduce

CameraGLSurfaceView

Minimal example

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import org.opencv.android.CameraGLSurfaceView;

public class MainActivity extends Activity {
static {
System.loadLibrary("opencv_java3");
}

private CameraGLSurfaceView mView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mView = new CameraGLSurfaceView(this, null);
setContentView(mView);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

View decorView = getWindow().getDecorView();

int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

}

@Override
protected void onPause() {
mView.onPause();
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
mView.onResume();
}
}

Results

holding phone in landscape

in portrait

JavaCamera2View

Minimal example

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import org.opencv.android.JavaCamera2View;

public class MainActivity extends Activity {
static {
System.loadLibrary("opencv_java3");
}

private JavaCamera2View mView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mView = findViewById(R.id.camera);
mView.enableView();

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

View decorView = getWindow().getDecorView();

int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

}
}

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
tools:context=".MainActivity">

<org.opencv.android.JavaCamera2View
android:layout_width="match_parent"
android:id="@+id/camera"
android:layout_height="match_parent"/>

</LinearLayout>

Stacktrace

java.lang.RuntimeException: Interrupted while setCameraPreviewSize.
at org.opencv.android.JavaCamera2View.connectCamera(JavaCamera2View.java:320)
at org.opencv.android.CameraBridgeViewBase.onEnterStartedState(CameraBridgeViewBase.java:360)
at org.opencv.android.CameraBridgeViewBase.processEnterState(CameraBridgeViewBase.java:321)
at org.opencv.android.CameraBridgeViewBase.checkCurrentState(CameraBridgeViewBase.java:313)
at org.opencv.android.CameraBridgeViewBase.surfaceChanged(CameraBridgeViewBase.java:198)
at android.view.SurfaceView.updateWindow(SurfaceView.java:614)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:187)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2018)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1094)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6054)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
at android.view.Choreographer.doCallbacks(Choreographer.java:606)
at android.view.Choreographer.doFrame(Choreographer.java:575)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at org.opencv.android.JavaCamera2View.calcPreviewSize(JavaCamera2View.java:265)
at org.opencv.android.JavaCamera2View.connectCamera(JavaCamera2View.java:300)
at org.opencv.android.CameraBridgeViewBase.onEnterStartedState(CameraBridgeViewBase.java:360)
at org.opencv.android.CameraBridgeViewBase.processEnterState(CameraBridgeViewBase.java:321)
at org.opencv.android.CameraBridgeViewBase.checkCurrentState(CameraBridgeViewBase.java:313)
at org.opencv.android.CameraBridgeViewBase.surfaceChanged(CameraBridgeViewBase.java:198)
at android.view.SurfaceView.updateWindow(SurfaceView.java:614)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:187)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2018)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1094)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6054)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
at android.view.Choreographer.doCallbacks(Choreographer.java:606)
at android.view.Choreographer.doFrame(Choreographer.java:575)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)

The only thing i got to work on my phone, is JavaCameraView, but only at low 15 FPS.