2
votes

I'm trying to access the front facing camera through the Camera2 API using an actual device

the problem occurs when trying to

cameraManager.openCamera(..,..,..)

try {
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraID);
            if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("Time out waiting to lock camera opening.");
            }
            manager.openCamera(mCameraID, mStateCallback, mBackgroundHandler);
        } catch (CameraAccessException e) {
            Log.e("mr", "OpenCamera - Camera Access Exception");
        } catch (IllegalArgumentException e) {
            Log.e("mr", "OpenCamera - Illegal Argument Exception");
        } catch (SecurityException e) {
            e.printStackTrace();
            Log.e("mr", "OpenCamera - Security Exception:");
        } catch (InterruptedException e) {
            Log.e("mr", "OpenCamera - Interrupted Exception");
        }

the Cameramanager says

I/CameraManagerGlobal: Connecting to camera service

which is coming from this CameraManager.java function

private void connectCameraServiceLocked() {
            // Only reconnect if necessary
            if (mCameraService != null) return;

            Log.i(TAG, "Connecting to camera service");

Then my GLSurfaceView remains blank

AndroidMainfest.xml

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

P.S:

  • It works with the back camera
  • It works with the nexus 6 API 23 emulator
  • Permissions are requested at runtime for Android M
  • I'm using GLSurfaceView as a viewfinder
  • The front camera was working perfectly on the same device using the normal CameraAPI with a TextureView
  • Tried to Uninstall/Reboot/Clear Cache
  • the device I'm using is a Sony Z5 premium

I'm using this example Render camera preview using OpenGL ES 2.0 on Android API 21 or higher

3
Just to be clear: you're feeding the Camera2 output to a SurfaceTexture, and then rendering the external texture with GLES? The list of things you've tried looks pretty good... if the exact setup works with the back camera, but not the front camera, then that's a little odd. If all that's changing is the camera selection constant, then that limits the set of things that could be wrong. Why aren't you just using a TextureView? Are you applying a shader? - fadden
@fadden i'm trying to feed the Camera2 output to the GLSurfaceView, i was using the normal TextureView and it was working perfectly but i need to draw / take screenshots that's why i'm moving to GLSurfaceView with a shader - Wael hamada

3 Answers

1
votes

If you're just copying that example, I noticed it has the following line:

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);

The front camera probably doesn't support that, since it likely has no flash. You need to check what AE modes are actually supported - the same goes for the various other settings the sample sets, where it isn't checking what the device actually supports.

That said, you should be getting an error if the value is bad, but it might be coming in on the CameraCaptureSession.CaptureListener.onCaptureFailed, and the sample is not listening to those. There's probably also some logcat from the camera service, if you look at all logging, not just your own app's logs.

But try removing that line, and see if it works.

0
votes

I've ended with changing the Sony Xperia Z5 premium to another device and it worked perfectly. it looks like that Sony is facing some problems with the new API as i saw in the following link:

Sony Camera2 API limitations

there might be a work around but anyway i'm making an application for specific purpose i don't want it to be working on all devices

0
votes

First get Camera service like this

CameraManager cameraManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);