I need to set a preview for camera, to use it to scan a QR code, using Camera2 API. My problem is to configure camera. This is my code:
//setting the preview surface layout file
<?xml version="1.0" encoding="utf-8"?>
<SurfaceView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="@+id/surfaceView"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/scanqr"
android:id="@+id/textView"
android:gravity="center_horizontal"
android:layout_above="@+id/surfaceView"
android:layout_centerHorizontal="true" />
In code:
//After I get my CameraDevice...
CameraCharacteristics caracteristicas=cManager.getCameraCharacteristics(chosenCam);
StreamConfigurationMap map = caracteristicas.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] mPreviewSize = map.getOutputSizes(WHAT TO PUT HERE);
surfaceViewCamera = (SurfaceView)dCamera.findViewById(R.id.surfaceView);
surfaceViewCamera.getHolder().addCallback(this);
mPreviewRequestBuilder= cDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
Surface surface=surfaceViewCamera.getHolder().getSurface();
mPreviewRequestBuilder.addTarget(surface);
cDevice.createCaptureSession(Arrays.asList(surface),
new CameraCaptureSession.StateCallback(){...onConfigured, onCaptureFailed and onConfigureFailed methods...}
My problem is that I don't know how to configure properly the camera, as always enters the onConfigurationFailed method. Any help to do that? Someone knows any good specific tutorial about that topic?
Thank you.
EDIT:
No luck. My code now looks like this:
CameraCharacteristics caracteristicas=cManager.getCameraCharacteristics(chosenCam);
StreamConfigurationMap map = caracteristicas.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] mPreviewSize = map.getOutputSizes(ImageFormat.JPEG);
iReader=ImageReader.newInstance(mPreviewSize[0].getWidth(), mPreviewSize[0].getHeight(),ImageFormat.JPEG, 1);
iReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Log.i("TAG", "We have image. Yay!");
}
}, backgroundHandler);
I don't receive any exception, it only goes to onConfigurationFailed, and says:
06-29 08:20:50.377 1410-1410/my.package E/Legacy-CameraDevice-JNI﹕ LegacyCameraDevice_nativeDetectSurfaceDimens: Could not retrieve native window from surface.
06-29 08:20:50.388 1410-1410/my.package W/CameraDevice-JV-0﹕ Stream configuration failed
06-29 08:20:51.283 1410-1410/my.package E/CameraCaptureSession﹕ Session 0: Failed to create capture session; configuration failed
06-29 08:21:12.179 1410-1410/my.package I/Choreographer﹕ Skipped 26759 frames! The application may be doing too much work on its main thread.
06-29 08:21:13.424 1410-1416/my.package W/art﹕ Suspending all threads took: 13.264ms
06-29 08:21:20.458 1410-1416/my.package W/art﹕ Suspending all threads took: 12.178ms
06-29 08:21:24.481 1410-1416/my.package W/art﹕ Suspending all threads took: 7.194ms
06-29 08:21:28.496 1410-1416/my.package W/art﹕ Suspending all threads took: 5.281ms
06-29 08:21:34.031 1410-1416/my.package W/art﹕ Suspending all threads took: 7.869ms
06-29 08:21:39.923 1410-1477/my.package I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-29 08:21:39.989 1410-1477/my.package W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-29 08:21:39.989 1410-1477/my.package W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb4ba9b60, error=EGL_SUCCESS
PD: Im debugging with emulator, just in case it helps