我需要在android中打开前置摄像头。我正在创建一个镜像应用程序。我该怎么打开?

时间:2015-03-10 09:15:14

标签: android camera

这是我在android studio中用来打开前置摄像头的代码......但是我得到了#34; Camera"功能弃用帮助我!!!

private static final String TAG = null ;

private Camera openFrontFacingCamera()
{
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

1 个答案:

答案 0 :(得分:0)

您是否还在AndroidManifest.xml中添加了以下内容?

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

如果您使用的是API 21,那么您应该查看http://developer.android.com/reference/android/hardware/camera2/package-summary.html