fix #26, add automatic flash

This commit is contained in:
tibbi 2017-03-26 18:24:51 +02:00
parent 10549b4542
commit 604c5b9e76
4 changed files with 33 additions and 20 deletions

View File

@ -34,9 +34,9 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK)
set(cameraId) = prefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply()
var lastFlashlightState: Boolean
get() = prefs.getBoolean(LAST_FLASHLIGHT_STATE, false)
set(enabled) = prefs.edit().putBoolean(LAST_FLASHLIGHT_STATE, enabled).apply()
var flashlightState: Int
get() = prefs.getInt(FLASHLIGHT_STATE, FLASH_OFF)
set(state) = prefs.edit().putInt(FLASHLIGHT_STATE, state).apply()
var backPhotoResIndex: Int
get() = prefs.getInt(BACK_PHOTO_RESOLUTION_INDEX, -1)

View File

@ -9,8 +9,12 @@ val SAVE_PHOTOS = "save_photos"
val SHOW_PREVIEW = "show_preview"
val SOUND = "sound"
val LAST_USED_CAMERA = "last_used_camera"
val LAST_FLASHLIGHT_STATE = "last_flashlight_state"
val FLASHLIGHT_STATE = "flashlight_state"
val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index"
val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index"
val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index"
val FRONT_VIDEO_RESOLUTION_INDEX = "front_video_resolution_index"
val FLASH_OFF = 0
val FLASH_ON = 1
val FLASH_AUTO = 2

View File

@ -6,6 +6,7 @@ import android.graphics.Rect
import android.hardware.Camera
import android.media.*
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.Handler
import android.util.Log
@ -44,7 +45,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private var mCurrVideoPath = ""
private var mCanTakePicture = false
private var mIsFlashEnabled = false
private var mIsRecording = false
private var mIsVideoMode = false
private var mIsSurfaceCreated = false
@ -68,7 +68,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mSurfaceHolder.addCallback(this)
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
mCanTakePicture = false
mIsFlashEnabled = false
mIsVideoMode = false
mIsSurfaceCreated = false
mSetupPreviewAfterMeasure = false
@ -267,7 +266,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
}
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mCamera!!.enableShutterSound(false)
}
@ -465,15 +464,18 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
fun enableFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH
mCamera!!.parameters = mParameters
mIsFlashEnabled = true
}
fun disableFlash() {
mIsFlashEnabled = false
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
mCamera!!.parameters = mParameters
}
fun autoFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_AUTO
mCamera!!.parameters = mParameters
}
fun initPhotoMode() {
stopRecording()
cleanupRecorder()

View File

@ -44,7 +44,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
private var mPreview: Preview? = null
private var mPreviewUri: Uri? = null
private var mIsFlashEnabled = false
private var mFlashlightState = FLASH_OFF
private var mIsInPhotoMode = false
private var mIsAskingPermissions = false
private var mIsCameraAvailable = false
@ -140,7 +140,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
mIsInPhotoMode = true
mTimerHandler = Handler()
mFadeHandler = Handler()
mIsFlashEnabled = config.lastFlashlightState
mFlashlightState = config.flashlightState
setupPreviewImage(true)
}
@ -228,30 +228,37 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
return
}
mIsFlashEnabled = !mIsFlashEnabled
mFlashlightState = ++mFlashlightState % 3
checkFlash()
}
private fun checkFlash() {
if (mIsFlashEnabled) {
enableFlash()
} else {
disableFlash()
when (mFlashlightState) {
FLASH_ON -> enableFlash()
FLASH_AUTO -> autoFlash()
else -> disableFlash()
}
}
private fun disableFlash() {
mPreview?.disableFlash()
toggle_flash.setImageResource(R.drawable.ic_flash_off)
mIsFlashEnabled = false
config.lastFlashlightState = mIsFlashEnabled
mFlashlightState = FLASH_OFF
config.flashlightState = FLASH_OFF
}
private fun enableFlash() {
mPreview?.enableFlash()
toggle_flash.setImageResource(R.drawable.ic_flash_on)
mIsFlashEnabled = true
config.lastFlashlightState = mIsFlashEnabled
mFlashlightState = FLASH_ON
config.flashlightState = FLASH_ON
}
private fun autoFlash() {
mPreview?.autoFlash()
toggle_flash.setImageResource(R.drawable.ic_flash_auto)
mFlashlightState = FLASH_AUTO
config.flashlightState = FLASH_AUTO
}
private fun shutterPressed() {