6. 카메라 플래쉬라이트 ON/OFF
페이지 정보
작성자 관리자 댓글 1건 조회 4,324회 작성일 20-04-30 09:24본문
6. 카메라 플래쉬라이트 ON/OFF
카메라 Flashlight를 켜고 끄는 예제를 만들어 보겠습니다.
1. 새프로젝트 생성
프로젝트명 : FlashLightTest
2. 화면 디자인 수정
activity_main.xml 을 수정합니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnOn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FLASH ON" />
<Button
android:id="@+id/btnOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FLASH OFF" />
</LinearLayout>
3. MainActivity.kt 수정
package kr.co.leelab.flashlighttest
import android.content.Context
import android.hardware.Camera
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private val TAG = "My_TAG"
private var mCamera: Camera? = null
private var parm: Camera.Parameters? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 카메라 플래시라이트 켜기
btnOn.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val camManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
var cameraId: String? = null
try {
if (camManager != null) {
cameraId = camManager.cameraIdList[0]
camManager.setTorchMode(cameraId, true) //Turn ON
}
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString());
}
} else {
mCamera = Camera.open()
parm = mCamera!!.parameters
parm!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH
mCamera!!.parameters = parm
mCamera!!.startPreview()
}
}
// 카메라 플래시라이트 끄기
btnOff.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val camManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
var cameraId: String? = null
try {
if (camManager != null) {
cameraId = camManager.cameraIdList[0]
camManager.setTorchMode(cameraId!!, false) //Turn OFF
}
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString());
}
} else {
mCamera = Camera.open();
parm = mCamera!!.parameters
parm!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
mCamera!!.parameters = parm
mCamera!!.stopPreview()
}
}
}
override fun onStop() {
super.onStop()
if (mCamera != null) {
mCamera!!.release()
}
}
}
4. 권한 설정
AndroidManifest.xml 파일을 수정합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.leelab.flashlighttest">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5. 실행
실제 휴대폰을 연결하고, 실행해보겠습니다.
휴대폰을 연결하면 아래와 같이 모델이 보이게 되면 선택합니다.
실행버튼을 눌러 실행합다.
댓글목록
관리자님의 댓글
관리자 작성일
private fun turnFlashlightOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val camManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
var cameraId: String? = null
try {
if (camManager != null) {
cameraId = camManager.cameraIdList[0]
camManager.setTorchMode(cameraId, true) //Turn ON
Log.i(TAG, "cameraId"+cameraId)
}
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString());
}
} else {
mCamera = Camera.open()
parm = mCamera!!.parameters
parm!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH
mCamera!!.parameters = parm
mCamera!!.startPreview()
}
isLighOn = true
}
private fun turnFlashlightOff() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val camManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
var cameraId: String? = null
try {
if (camManager != null) {
cameraId = camManager.cameraIdList[0]
camManager.setTorchMode(cameraId!!, false) //Turn OFF
}
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString());
}
} else {
mCamera = Camera.open();
parm = mCamera!!.parameters
parm!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
mCamera!!.parameters = parm
mCamera!!.stopPreview()
}
isLighOn = false
}