本文介绍了检查哪个相机是Open Front或Back Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在打开前置摄像头时设置布尔标志。如果flag为true,则表示前置摄像头已开启。

I know i can set a boolean flag while opening front Camera. And if flag is true it means front camera is on.

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

But is there a way using Android API to know which Camera is Open right now? Front or Back.

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
}

摄像头预览正在反转(上行donw)当我打开前置摄像头时。所以我必须添加一个检查,相机打开如果打开FrontCamera然后矩阵= 270.否则矩阵= 90。

Camera Preview is inverting(upside donw) when i open Front Camera. So i have to add a check which Camera is open if FrontCamera is opened then matrix = 270. otherwise matrix =90.

onPreviewFrame(字节abyte0 [],相机相机)

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


推荐答案

在新的包,您可以通过属性,每个 CameraDevice 使用很容易找到特征。

In new android.hardware.camera2 package, you can enquire from CameraCharacteristics.LENS_FACING property and each CameraDevice publishes its id with CameraDevice.getId() it's easy to get to the characteristics.

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

In the older camera API, I think the only way is to keep track of the index you opened it with.

private int cameraId;

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

然后使用 cameraId 稍后,这个可能是实现目标的更好方式:

Then use cameraId later, this little snippet might be a better way of achieving what you are trying to:

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

这篇关于检查哪个相机是Open Front或Back Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:10