0

i have tried this

 private Camera openFrontFacingCamera() {
        Camera camera = null;

        // Look for front-facing camera, using the Gingerbread API.
        // Java reflection is used for backwards compatibility with pre-Gingerbread APIs.
        try {
            Class<?> cameraClass = Class.forName("android.hardware.Camera");
            Object cameraInfo = null;
            Field field = null;
            int cameraCount = 0;
            Method getNumberOfCamerasMethod = cameraClass.getMethod( "getNumberOfCameras" );
            if ( getNumberOfCamerasMethod != null ) {
                cameraCount = (Integer) getNumberOfCamerasMethod.invoke( null, (Object[]) null );
            }
            Class<?> cameraInfoClass = Class.forName("android.hardware.Camera$CameraInfo");
            if ( cameraInfoClass != null ) {
                cameraInfo = cameraInfoClass.newInstance();
            }
            if ( cameraInfo != null ) {
                field = cameraInfo.getClass().getField( "facing" );
            }
            Method getCameraInfoMethod = cameraClass.getMethod( "getCameraInfo", Integer.TYPE, cameraInfoClass );
            if ( getCameraInfoMethod != null && cameraInfoClass != null && field != null ) {
                for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
                    getCameraInfoMethod.invoke( null, camIdx, cameraInfo );
                    int facing = field.getInt( cameraInfo );
                    if ( facing == 1 ) { // Camera.CameraInfo.CAMERA_FACING_FRONT 
                        try {
                            Method cameraOpenMethod = cameraClass.getMethod( "open", Integer.TYPE );
                            if ( cameraOpenMethod != null ) {
                                camera = (Camera) cameraOpenMethod.invoke( null, camIdx );
                            }
                        } catch (RuntimeException e) {
                            Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                        }
                    }
                }
            }
        }
        // Ignore the bevy of checked exceptions the Java Reflection API throws - if it fails, who cares.
        catch ( ClassNotFoundException e        ) {Log.e(TAG, "ClassNotFoundException" + e.getLocalizedMessage());}
        catch ( NoSuchMethodException e         ) {Log.e(TAG, "NoSuchMethodException" + e.getLocalizedMessage());}
        catch ( NoSuchFieldException e          ) {Log.e(TAG, "NoSuchFieldException" + e.getLocalizedMessage());}
        catch ( IllegalAccessException e        ) {Log.e(TAG, "IllegalAccessException" + e.getLocalizedMessage());}
        catch ( InvocationTargetException e     ) {Log.e(TAG, "InvocationTargetException" + e.getLocalizedMessage());}
        catch ( InstantiationException e        ) {Log.e(TAG, "InstantiationException" + e.getLocalizedMessage());}
        catch ( SecurityException e             ) {Log.e(TAG, "SecurityException" + e.getLocalizedMessage());}

        if ( camera == null ) {
            // Try using the pre-Gingerbread APIs to open the camera.
            try {
                camera = Camera.open();
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }

        return camera;
    }

but it display only blank screen PLz help

Android
  • 8,995
  • 9
  • 67
  • 108
  • I think you can use the below api from camera class to open whichever camera you want with the help of ID: public static Camera open (int cameraId) -- introduced in API level 9 which is Gingerbread. Generally cameraIds will be, 0 for rear camera and 1 for front facing camera. But still you can validate the cameraIds as you are doing above. – Santhosh Dec 23 '14 at 11:16
  • which API? you specified – Android Dec 23 '14 at 11:27
  • Lets say cameraID 1 represents front facing camera, then call like this: Camera frontCam = Camera.open(1); – Santhosh Dec 23 '14 at 11:48

1 Answers1

2

Maybe this answer and question will help you: capture photo from both front and back camera simultaneously

Use another intent to go to Camera again. This time, add an extra to your intent to so the camera defaults to front camera intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

So yor code would be like this:

android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri()); cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

Community
  • 1
  • 1
Jure
  • 799
  • 6
  • 25
  • can you plz explain in detail? – Android Dec 23 '14 at 09:40
  • Try this not sure if it will work: Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri()); cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE); – Jure Dec 23 '14 at 09:43