检测缺乏后置摄像头摄像头

2023-09-06 09:07:06 作者:冰是睡着的水﹏

我的应用程序具有的功能,需要一个后置摄像头。是否有前面的相机或不无关我的需要。组建了一个强大的程序,以检测一后置摄像头是否存在,在任何情况下,被证明是非常棘手。例如,与HTC EVO 3D用户抱怨该应用程序说,有没有后置摄像头(有明确的),我也有一些来自其他用户类似的投诉。

这是一个棘手的事情来进行​​测试,因为尽管有若干装置我没有一个设备具有唯一的前置摄像头,如Nexus的7,或任何由用户所提及的车型。

下面就是我的,这是从code对本网站其他答案:

 布尔rearCameraFound = FALSE;
    如果(BUILD_VERSION→8){
        INT cameraCount = 0;
        相机凸轮=无效;
        Camera.CameraInfo cameraInfo =新Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        对于(INT camIdx = 0; camIdx< cameraCount; camIdx ++){
            Camera.getCameraInfo(camIdx,cameraInfo);
            如果(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
                尝试 {
                    凸轮= Camera.open(camIdx);
                    Log.d(TAG,后置摄像头检测);
                    rearCameraFound = TRUE;
                }赶上(RuntimeException的E){
                    Log.e(TAG,数码相机未能打开:+ e.getLocalizedMessage());
                }
            }
            如果(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
                Log.d(TAG,前置摄像头检测);
            }
        }
        返回rearCameraFound;
    }其他{
        如果(context.getPackageManager()。hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            //这个设备有一个摄像头
            返回true;
        } 其他 {
            //此设备上没有摄像头
            返回false;
        }
    }
 
最大的小米手机 小米3代全球首发评测

我现在换成这个code这个非常简单的版本:

  PackageManager PM = context.getPackageManager();
返回pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
 

不过,我不知道会发生什么,对了Nexus 7,例如,只有一个前置摄像头。这会返​​回true?

我在寻找code,它会告诉我可以肯定,如果有一个后置摄像头还是不行!

解决方案

的Nexus 7(其中只有一个正面摄像头),以 FEATURE_CAMERA 返回相反,假的,你可以使用 FEATURE_CAMERA_FRONT 。看看这个讨论。

现在,通过使用上面的,你可以确保有用于至少一个摄像头。所以,现在,你可以检查在手机摄像头的present数,如果是大于一,那么就会出现无疑是一个后置摄像头。

 进口android.hardware.Camera;

INT numCameras = Camera.getNumberOfCameras();
如果(numCameras→1){
  rearCamera = TRUE;
}
 

这是相当棘手。但是,这一切,我现在能想到的。只要给它一个尝试。

My app has functionality that requires a rear camera. Whether there is a front camera or not is irrelevant to my needs. Putting together a robust routine to detect whether or not a rear camera exists, in all circumstances, is proving tricky. For example, a user with an HTC Evo 3D has complained that the app says there's no rear camera (there clearly is), and I've had a number of similar complaints from other users.

This is a tricky thing to test, as despite having a number of devices I don't have a device with only a front camera, such as the Nexus 7, or any of the models mentioned by the users.

Here's what I have, and this was taken from code on other answers on this site:

boolean rearCameraFound = false;
    if(BUILD_VERSION > 8){
        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_BACK  ) {
                try {
                    cam = Camera.open( camIdx );
                    Log.d("TAG", "Rear camera detected");
                    rearCameraFound = true;
                } catch (RuntimeException e) {
                    Log.e("TAG", "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
            if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) {
                Log.d("TAG", "Front camera detected");
            }
        }
        return rearCameraFound;
    }else{
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

I've now replaced this code with this much simpler version:

PackageManager pm = context.getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);

However, I don't know what would happen on the Nexus 7 for example, with only a front camera. Would this return true?

I'm looking for code that will tell me for sure if there's a rear camera or not!

解决方案

Nexus 7 (which has only one frontal camera) returns false to FEATURE_CAMERA, Instead of it, you can use FEATURE_CAMERA_FRONT. Check out this discussion.

Now, by using the above, you could make sure that there is atleast one camera. So now, you can check the number of camera's present in the phone, if it is greater than one, then there will be surely a rear camera.

import android.hardware.Camera;

int numCameras = Camera.getNumberOfCameras();
if (numCameras > 1) {
  rearCamera = true;
}

This is quite tricky. But that's all, I can now think of. Just give it a try.