I'm trying to create a very simple app which shows the picture received from the camera on the display. I'm using the camera2 API, because I am new to this, so I try to use the most recent technology.
I reach the point where I call createCaptureSession()
, but then onConfigureFailed()
gets called and I don't know what the problem is or how to find it.
Here is my source:
SurfaceView mSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate() start");
setContentView(R.layout.activity_main);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
}
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() start");
initCamera();
}
private void initCamera() {
Log.d(TAG, "initCamera() start");
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
Log.d(TAG, "acquired cameraManager: " + cameraManager);
String[] cameraIdList;
try {
cameraIdList = cameraManager.getCameraIdList();
} catch (CameraAccessException e) {
Log.e(TAG, "couldn't get camera list", e);
return;
}
Log.d(TAG, "acquired cameraIdList: length: " + cameraIdList.length);
if (cameraIdList.length == 0) {
Log.w(TAG, "couldn't detect a camera");
return;
}
String camera0Id = cameraIdList[0];
Log.d(TAG, "chosen camera: " + camera0Id);
try {
cameraManager.openCamera(camera0Id, deviceCallback, null);
} catch (CameraAccessException e) {
Log.e(TAG, "couldn't open camera", e);
}
Log.d(TAG, "called cameraManager.openCamera()");
}
CameraDevice.StateCallback deviceCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
Log.d(TAG, "deviceCallback.onOpened() start");
Surface surface = mSurfaceView.getHolder().getSurface();
Log.d(TAG, "surface: " + surface);
List<Surface> surfaceList = Collections.singletonList(surface);
try {
camera.createCaptureSession(surfaceList, sessionCallback, null);
} catch (CameraAccessException e) {
Log.e(TAG, "couldn't create capture session for camera: " + camera.getId(), e);
return;
}
}
@Override
public void onDisconnected(CameraDevice camera) {
Log.d(TAG, "deviceCallback.onDisconnected() start");
}
@Override
public void onError(CameraDevice camera, int error) {
Log.d(TAG, "deviceCallback.onError() start");
}
};
CameraCaptureSession.StateCallback sessionCallback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
Log.i(TAG, "capture session configured: " + session);
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
Log.e(TAG, "capture session configure failed: " + session);
}
};
And the output is:
...﹕ onCreate() start
...﹕ onResume() start
...﹕ initCamera() start
...﹕ acquired cameraManager: android.hardware.camera2.CameraManager@5d68786
...﹕ acquired cameraIdList: length: 2
...﹕ chosen camera: 0
...﹕ called cameraManager.openCamera()
...﹕ deviceCallback.onOpened() start
...﹕ surface: Surface(name=null)/@0x52c91e3
...﹕ capture session configure failed: android.hardware.camera2.impl.CameraCaptureSessionImpl@1a8c7a99
The following is in my AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />
Set Custom camera code:
You can set camera preview size in "surfaceChanged()" method.
This code is work for me in Android 5.0.1 and 5.1.1 also.