Вопрос

Hi I am developing a custom video app. I am able to get the currentCameraId by

currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;

I have two questions to ask:

1) How to detect android devices with only front camera.

Because on tablets with only front camera like in Micromax tab, the currentCameraId is 0.

2) How to check camera flash availability as the below code is not working on some of the phones

flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

Please Help.

Thanks!

Это было полезно?

Решение

To Check Flash Light is available or not

boolean hasFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

To Check Camera is available or not

PackageManager pm = context.getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

}

If you are using API level 9 (Android 2.3) or above, you can do the following to calculate the index of the (first) front-facing camera:

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
}

you can then use the index for the Camera.open

For eg

int index = getFrontCameraId();
if (index == -1) error();
Camera c = Camera.open(index);

Другие советы

You can try this

public boolean hasFlash() {
        if (camera == null) {
            return false;
        }

        Camera.Parameters parameters = camera.getParameters();

        if (parameters.getFlashMode() == null) {
            return false;
        }

        List<String> supportedFlashModes = parameters.getSupportedFlashModes();
        if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        }

        return true;
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top