Android Camera2前置摄像头 [英] Android Camera2 front camera

查看:446
本文介绍了Android Camera2前置摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近注意到不推荐使用Camera API,并且找到了名为Camera2的新API.

I recently noticed that the Camera API is deprecated and I found the new API called Camera2.

我已经阅读了文档,但我不太了解.

I have read the documentation but I don't really understand it.

所以我的问题是:如何使用新的相机API预览前置摄像头?

So my question is: how do I preview the front camera with the new camera api?

只是预览,而不是录制.

Just a preview, not recording.

我想使用这个新的API,因为将来我猜想当前的Camera API将被替换并停止工作.

I want to use this new API because in the future I'm guessing the current Camera API will be replaced and stop working.

所以我想做好准备,坐在那里看着大家都惊慌失措. XD

So I want to be prepared and just sit and watch while everyone panics. XD

推荐答案

首先,找出前置摄像头的ID(如果有的话)

First of all, find out the id of your front camera (if it has one of course)

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
            try {
                return manager.getCameraIdList();
            } catch (CameraAccessException e) {
                return null;
            }

然后找到像这样的faceCamera:

Then find the faceCamera like this:

CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);

        if (cameraCharacteristics == null)
            throw new NullPointerException("No camera with id " + cameraId);

        return cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;

最后,您必须将摄像机设置为该ID:

Lastly, you have to set the camera with that id:

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
   try {
       characteristics = manager.getCameraCharacteristics(mCameraId);
   }  catch (CameraAccessException e) {
       e.printStackTrace();
   } 

请注意,这些只是有关如何做想做的提示. 有关如何开始预览的详细信息,请参阅: http://developer.android.com/samples/Camera2Basic/index.html

Note, these are just tips on how to do what you wanna do. For details on how to start a preview and more, refer to: http://developer.android.com/samples/Camera2Basic/index.html

这篇关于Android Camera2前置摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆