繁体   English   中英

检查哪个摄像头是 Open Front 或 Back Android

[英]Check which camera is Open Front or Back Android

我知道我可以boolean flag while opening front Camera设置一个boolean flag while opening front Camera 如果 flag 为 true,则表示前置摄像头已打开。

但是有没有办法使用 Android API 来知道哪个相机现在是打开的? 正面或背面。

public int getFrontCameraId() {
    CameraInfo ci = new CameraInfo();
    for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, ci);
        if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
    }
    return -1; // No front-facing camera found
}

当我打开前置摄像头时,摄像头预览正在反转(颠倒)。 所以我必须添加一个检查, if FrontCamera is opened then matrix = 270. otherwise matrix =90. ,哪个相机是打开的if FrontCamera is opened then matrix = 270. otherwise matrix =90.

onPreviewFrame(byte abyte0[] , Camera camera)

 int[] rgbData = YuvUtils.decodeGreyscale(abyte0, mWidth,mHeight);

    editedBitmap.setPixels(rgbData, 0, widthPreview, 0, 0, widthPreview, heightPreview);

    finalBitmap = Bitmap.createBitmap(editedBitmap, 0, 0, widthPreview, heightPreview, matrix, true);
    private boolean safeCameraOpen(int id) {
    boolean qOpened = false;

    try {
        releaseCameraAndPreview();
        mCamera = Camera.open(id);
        qOpened = (mCamera != null);
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    return qOpened;    
}

private void releaseCameraAndPreview() {
    mPreview.setCamera(null);
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

从 API 级别 9 开始,相机框架支持多个相机。
如果您使用旧 API 并在不带参数的情况下调用 open(),您将获得第一个后置摄像头。

Android 设备可以有多个摄像头,例如用于摄影的后置摄像头和用于视频通话的前置摄像头。

Android 2.3(API 级别 9)及更高版本允许您使用Camera.getNumberOfCameras()方法检查设备上可用的摄像头数量。

要访问主摄像头,请使用Camera.open()方法并确保捕获任何异常,如下面的代码所示:

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

在运行 Android 2.3(API 级别 9)或更高版本的设备上,您可以使用 Camera.open(int) 访问特定相机。
上面的示例代码将访问具有多个摄像头的设备上的第一个后置摄像头。

在新的android.hardware.camera2包中,您可以从CameraCharacteristics.LENS_FACING属性中查询,并且每个CameraDevice使用CameraDevice.getId()发布其id很容易获得特征。

在较旧的相机 API 中,我认为唯一的方法是跟踪您打开它的索引。

private int cameraId;

public void openFrontCamera(){
   cameraId = getFrontCameraId();
   if (cameraId != -1)
     camera = Camera.open(cameraId); //try catch omitted for brevity
}

然后稍后使用cameraId ,这个小片段可能是实现您想要的更好的方法:

public void onOrientationChanged(int orientation) {
     if (orientation == ORIENTATION_UNKNOWN) return;
     android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     orientation = (orientation + 45) / 90 * 90;
     int rotation = 0;
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
         rotation = (info.orientation - orientation + 360) % 360;
     } else {  // back-facing camera
         rotation = (info.orientation + orientation) % 360;
     }
     mParameters.setRotation(rotation);
 }

如果您有自定义相机活动,您可以尝试此方法。

boolean inPreview;

在您的 SurfaceChanged 方法中从 SurfaceView 集

inPreview = True;

在您的 Camera.CallbackListener 变量集中

inPreview = false;

暂无
暂无

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

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