package com.bjzc.bhzhgd.utils;

import android.content.Context;
import android.hardware.camera2.CameraManager;

public class FlashUtil {
    private boolean isFlashOn = false;
    private CameraManager camManager;
    public void toggle(Context context) {
        try {
            this.camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            String cameraId = this.camManager.getCameraIdList()[0];
            this.camManager.setTorchMode(cameraId, !this.isFlashOn);
            this.isFlashOn = !isFlashOn;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //页面销毁的时候调用此方法
    public void finishFlashUtils() {
        try {
            if(camManager!=null){
                String cameraId = camManager.getCameraIdList()[0];
                camManager.setTorchMode(cameraId, false);
                this.isFlashOn = false;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
