Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Wook No.1

안드로이드 카메라 플래시 제어 본문

카테고리 없음

안드로이드 카메라 플래시 제어

Wook No.1 2022. 4. 27. 15:07

안드로이드 6.0부터 카메라 플래시 사용 API가 생겨 편리하게 사용할수 있다.

 

필요한 권한 요청

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera.flash" />

 

object FlashManager {
    private var flashOn = false
    private lateinit var cameraManager: CameraManager
    private var cameraId: String? = null
    private var context: WeakReference<Context>? = null
    var flashCallback: FlashChangeListener? = null

    fun init(context: Context) {
        this.context = WeakReference(context)
        initFlash(context)
    }

    private fun initFlash(context: Context) {
        if (!context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            Toast.makeText(context, "There is no camera flash.\n The app will finish!", Toast.LENGTH_LONG).show()
            return
        }
        cameraManager = context.getSystemService(AppCompatActivity.CAMERA_SERVICE) as CameraManager
        cameraManager.registerTorchCallback(object : CameraManager.TorchCallback() {
            override fun onTorchModeChanged(cameraId: String, enabled: Boolean) {
                super.onTorchModeChanged(cameraId, enabled)
                setFlashOn(enabled)
            }
        }, null)
    }

    private fun setFlash() {
        if (cameraId == null) {
            try {
                for (id in cameraManager.cameraIdList) {
                    val c = cameraManager.getCameraCharacteristics(id)
                    val flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)
                    val lensFacing = c.get(CameraCharacteristics.LENS_FACING)
                    if (flashAvailable != null && flashAvailable
                        && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK
                    ) {
                        cameraId = id
                        break
                    }
                }
            } catch (e: CameraAccessException) {
                cameraId = null
                e.printStackTrace()
                return
            }
        }
    }
    
    fun flashlight() {
        setFlash()

        try {
            cameraId?.let {
                cameraManager.setTorchMode(it, !isFlashOn())
                setFlashOn(!isFlashOn())
            }
        } catch (e: CameraAccessException) {
            e.printStackTrace()
        }
    }

    fun flashRelease() {
        setFlash()
        try {
            if(flashOn) {
                cameraId?.let {
                    cameraManager.setTorchMode(it, false)
                    setFlashOn(false)
                }
            }
        } catch (e: CameraAccessException) {
            e.printStackTrace()
        }
    }

    fun setFlashOn(isFlashOn: Boolean) {
        flashOn = isFlashOn
        flashCallback?.onFlashChange(flashOn)
    }

    fun isFlashOn(): Boolean {
        return flashOn
    }

    interface FlashChangeListener {
        fun onFlashChange(flashOn: Boolean)
    }
}

 

 

안드로이드 6.0 미만에서 플래시를 제어하려면 camera API를 사용해야 한다.

camera = cameraProvider.bindToLifecycle(
                this, cameraSelector, preview, imageCapture, imageAnalyzer
           )

cameraController = camera?.cameraControl

when(cameraInfo?.torchState?.value){
    TorchState.ON -> {
        cameraController?.enableTorch(false)
        it.isSelected = false
    }
    TorchState.OFF -> {
        cameraController?.enableTorch(true)
        it.isSelected = true
    }
}

 

Comments