【问题标题】:android take photo using android front cameraandroid 使用 android 前置摄像头拍照
【发布时间】:2015-02-21 10:09:22
【问题描述】:

我试过了

 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帮助

【问题讨论】:

  • 我认为您可以使用相机类中的以下 api 在 ID 的帮助下打开您想要的任何相机:public static Camera open (int cameraId) -- 在 API 级别 9 中引入,即 Gingerbread。通常 cameraIds 将是,0 用于后置摄像头,1 用于前置摄像头。但是您仍然可以像上面那样验证 cameraIds。
  • 哪个 API?你指定了
  • 假设cameraID 1代表前置摄像头,然后这样调用:Camera frontCam = Camera.open(1);

标签: android android-camera


【解决方案1】:

也许这个答案和问题会对你有所帮助:capture photo from both front and back camera simultaneously

使用另一个意图再次转到相机。这一次,添加一个额外的 您的意图,因此相机默认为前置相机 intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

所以你的代码应该是这样的:

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

【讨论】:

  • 你能详细解释一下吗?
  • 试试这个不确定它是否会工作: 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);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多