首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Android如何知道手电筒是否亮着

Android可以通过使用Camera类来判断手电筒是否亮着。下面是一个完整的示例代码:

代码语言:java
复制
import android.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;

public class FlashlightUtils {
    private static Camera camera;
    private static Camera.Parameters parameters;
    private static boolean isFlashlightOn = false;

    // 判断设备是否支持闪光灯
    public static boolean isFlashlightSupported(Context context) {
        if (context.getPackageManager().hasSystemFeature(android.content.pm.PackageManager.FEATURE_CAMERA_FLASH)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                try {
                    CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
                    String[] cameraIds = cameraManager.getCameraIdList();
                    for (String cameraId : cameraIds) {
                        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
                        Boolean flashAvailable = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                        Integer lensFacing = characteristics.get(CameraCharacteristics.LENS_FACING);
                        if (flashAvailable != null && flashAvailable && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                            return true;
                        }
                    }
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            } else {
                if (camera == null) {
                    camera = Camera.open();
                }
                parameters = camera.getParameters();
                if (parameters.getFlashMode() == null) {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    // 打开手电筒
    public static void turnOnFlashlight(Context context) {
        if (isFlashlightSupported(context)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
                    String[] cameraIds = cameraManager.getCameraIdList();
                    for (String cameraId : cameraIds) {
                        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
                        Boolean flashAvailable = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                        Integer lensFacing = characteristics.get(CameraCharacteristics.LENS_FACING);
                        if (flashAvailable != null && flashAvailable && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                            cameraManager.setTorchMode(cameraId, true);
                            isFlashlightOn = true;
                            break;
                        }
                    }
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            } else {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(parameters);
                camera.startPreview();
                isFlashlightOn = true;
            }
        }
    }

    // 关闭手电筒
    public static void turnOffFlashlight(Context context) {
        if (isFlashlightSupported(context)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
                    String[] cameraIds = cameraManager.getCameraIdList();
                    for (String cameraId : cameraIds) {
                        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
                        Boolean flashAvailable = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                        Integer lensFacing = characteristics.get(CameraCharacteristics.LENS_FACING);
                        if (flashAvailable != null && flashAvailable && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                            cameraManager.setTorchMode(cameraId, false);
                            isFlashlightOn = false;
                            break;
                        }
                    }
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            } else {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(parameters);
                camera.stopPreview();
                isFlashlightOn = false;
            }
        }
    }

    // 判断手电筒是否亮着
    public static boolean isFlashlightOn() {
        return isFlashlightOn;
    }
}

使用示例:

代码语言:java
复制
// 判断设备是否支持闪光灯
boolean isSupported = FlashlightUtils.isFlashlightSupported(context);

// 打开手电筒
FlashlightUtils.turnOnFlashlight(context);

// 关闭手电筒
FlashlightUtils.turnOffFlashlight(context);

// 判断手电筒是否亮着
boolean isOn = FlashlightUtils.isFlashlightOn();

这段代码通过Camera类来控制手电筒的开关,同时也考虑了不同Android版本的兼容性。请注意,在使用完手电筒后,记得调用turnOffFlashlight()方法关闭手电筒,以释放相机资源。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券