简体   繁体   English

如何在Android中使用Camera Intent启动前置摄像头?

[英]How to start front facing camera in android Using Camera Intent?

I am developing an app in which I want to open front camera on button click (if front camera is present on that device). 我正在开发一个应用程序,我想在按钮点击时打开前置摄像头(如果该设备上有前置摄像头)。

I have use this code which works for me on Asus Tablet having OS 3.2.1. 我已经使用了这个代码,它适用于具有OS 3.2.1的华硕平板电脑。 But same code is not working on OS 4.2.1. 但是相同的代码不适用于OS 4.2.1。

I want the code which works on all android versions. 我想要适用于所有Android版本的代码。 Please help me in this problem. 请帮我解决这个问题。

Following is my code- 以下是我的代码 -

Intent takePictureIntent = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(f));
                    Log.v("", "Camera Id-" + camId);

                    if (!TextUtils.isEmpty(camera)) {
                        if (camera.equalsIgnoreCase("Front")) {
                            Log.v("", "Inside if");
                            takePictureIntent.putExtra(
                                    "android.intent.extras.CAMERA_FACING",
                                    Camera.CameraInfo.CAMERA_FACING_FRONT);

                        } else {
                            Log.v("", "Inside else");
                            takePictureIntent.putExtra(
                                    "android.intent.extras.CAMERA_FACING",
                                    Camera.CameraInfo.CAMERA_FACING_BACK);

                        }
                    }
startActivityForResult(takePictureIntent, actionCode);

Note: This feature is available in Gingerbread and Up Android Version. 注意:此功能在Gingerbread和Up Android版本中可用。 Intent handles camera action in its own way. Intent以自己的方式处理相机动作。 This technique is used when you are using SurfaceView to exploit camera functionality. 当您使用SurfaceView来利用相机功能时,将使用此技术。

 private Camera openFrontFacingCameraGingerbread() {
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;}

in manifest file: 在清单文件中:

<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" />

Today, I was trying the same thing and ran into same problems as yours. 今天,我正在尝试同样的事情并遇到与你相同的问题。 Then I searched the problem and found the perfect solution on this link which helped me. 然后我搜索了问题,并找到了这个链接的完美解决方案,这对我有帮助。 Android can't record video with Front Facing Camera, MediaRecorder start failed: -19 Android无法使用前置摄像头录制视频,MediaRecorder启动失败:-19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM