android使用android前置摄像头拍照

时间:2014-12-23 09:24:29

标签: android android-camera

我试过这个

 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;
    }

但它只显示空白屏PLz帮助

1 个答案:

答案 0 :(得分:2)

也许这个答案和问题可以帮到你:capture photo from both front and back camera simultaneously

  

使用其他意图再次转到相机。这一次,添加一个额外的   你的意图,所以相机默认为前置摄像头   intent.putExtra(&#34; android.intent.extras.CAMERA_FACING&#34;,1);

所以你的代码就像这样:

  

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