Merge pull request #359 from KryptKode/feat/always-on-torch

add always on torch mode for image capture
This commit is contained in:
Tibor Kaputa 2022-10-28 15:35:43 +02:00 committed by GitHub
commit e9f68515da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 19 deletions

View File

@ -40,17 +40,11 @@ import com.simplemobiletools.camera.views.FocusCircleView
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_flash.*
import kotlinx.android.synthetic.main.layout_top.*
import java.util.concurrent.TimeUnit
import kotlin.math.abs
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_flash.flash_auto
import kotlinx.android.synthetic.main.layout_flash.flash_off
import kotlinx.android.synthetic.main.layout_flash.flash_on
import kotlinx.android.synthetic.main.layout_flash.flash_toggle_group
import kotlinx.android.synthetic.main.layout_top.change_resolution
import kotlinx.android.synthetic.main.layout_top.default_icons
import kotlinx.android.synthetic.main.layout_top.settings
import kotlinx.android.synthetic.main.layout_top.toggle_flash
class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, CameraXPreviewListener {
private companion object {
@ -352,7 +346,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
mTimerHandler = Handler(Looper.getMainLooper())
setupPreviewImage(true)
val initialFlashlightState = if (mIsInPhotoMode) config.flashlightState else FLASH_OFF
val initialFlashlightState = if (mIsInPhotoMode && config.flashlightState != FLASH_ALWAYS_ON) config.flashlightState else FLASH_OFF
mPreview!!.setFlashlightState(initialFlashlightState)
updateFlashlightState(initialFlashlightState)
initFlashModeTransitionNames()
@ -363,6 +357,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
flash_auto.transitionName = "$baseName$FLASH_AUTO"
flash_off.transitionName = "$baseName$FLASH_OFF"
flash_on.transitionName = "$baseName$FLASH_ON"
flash_always_on.transitionName = "$baseName$FLASH_ALWAYS_ON"
}
private fun initButtons() {
@ -384,10 +379,13 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
flash_auto.setShadowIcon(R.drawable.ic_flash_auto_vector)
flash_auto.setOnClickListener { selectFlashMode(FLASH_AUTO) }
flash_always_on.setShadowIcon(R.drawable.ic_flashlight_vector)
flash_always_on.setOnClickListener { selectFlashMode(FLASH_ALWAYS_ON) }
}
@SuppressLint("ClickableViewAccessibility")
private fun initModeSwitcher(){
private fun initModeSwitcher() {
val gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
override fun onFling(event1: MotionEvent, event2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
val deltaX = event1.x - event2.x
@ -456,7 +454,8 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
val flashDrawable = when (state) {
FLASH_OFF -> R.drawable.ic_flash_off_vector
FLASH_ON -> R.drawable.ic_flash_on_vector
else -> R.drawable.ic_flash_auto_vector
FLASH_AUTO -> R.drawable.ic_flash_auto_vector
else -> R.drawable.ic_flashlight_vector
}
toggle_flash.setShadowIcon(flashDrawable)
toggle_flash.transitionName = "${getString(R.string.toggle_flash)}$state"
@ -849,6 +848,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
val transitionSet = createTransition()
TransitionManager.go(flashModeScene, transitionSet)
flash_auto.beVisibleIf(photoCapture)
flash_always_on.beVisibleIf(photoCapture)
flash_toggle_group.check(config.flashlightState.toFlashModeId())
flash_toggle_group.beVisible()

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.camera.extensions
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.helpers.FLASH_ALWAYS_ON
import com.simplemobiletools.camera.helpers.FLASH_AUTO
import com.simplemobiletools.camera.helpers.FLASH_OFF
import com.simplemobiletools.camera.helpers.FLASH_ON
@ -12,6 +13,7 @@ fun Int.toCameraXFlashMode(): Int {
FLASH_ON -> ImageCapture.FLASH_MODE_ON
FLASH_OFF -> ImageCapture.FLASH_MODE_OFF
FLASH_AUTO -> ImageCapture.FLASH_MODE_AUTO
FLASH_ALWAYS_ON -> ImageCapture.FLASH_MODE_OFF
else -> throw IllegalArgumentException("Unknown mode: $this")
}
}
@ -30,6 +32,7 @@ fun Int.toFlashModeId(): Int {
FLASH_ON -> R.id.flash_on
FLASH_OFF -> R.id.flash_off
FLASH_AUTO -> R.id.flash_auto
FLASH_ALWAYS_ON -> R.id.flash_always_on
else -> throw IllegalArgumentException("Unknown mode: $this")
}
}

View File

@ -24,6 +24,7 @@ const val CAPTURE_MODE = "capture_mode"
const val FLASH_OFF = 0
const val FLASH_ON = 1
const val FLASH_AUTO = 2
const val FLASH_ALWAYS_ON = 3
fun compensateDeviceRotation(orientation: Int) = when (orientation) {
ORIENT_LANDSCAPE_LEFT -> 270

View File

@ -395,15 +395,17 @@ class CameraXPreview(
}
override fun setFlashlightState(state: Int) {
val newFlashMode = state.toCameraXFlashMode()
if (!isPhotoCapture) {
camera?.cameraControl?.enableTorch(newFlashMode == FLASH_MODE_ON)
if (isPhotoCapture) {
camera?.cameraControl?.enableTorch(state == FLASH_ALWAYS_ON)
} else {
camera?.cameraControl?.enableTorch(state == FLASH_ON || state == FLASH_ALWAYS_ON)
}
val newFlashMode = state.toCameraXFlashMode()
flashMode = newFlashMode
imageCapture?.flashMode = newFlashMode
val appFlashMode = flashMode.toAppFlashMode()
config.flashlightState = appFlashMode
listener.onChangeFlashMode(appFlashMode)
config.flashlightState = state
listener.onChangeFlashMode(state)
}
override fun tryTakePicture() {

View File

@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="384dp" android:height="384dp" android:viewportWidth="384" android:viewportHeight="384">
<path android:fillColor="#FFFFFFFF" android:pathData="M189.53 7.64c2.76-0.51 6.41-1.08 8.48 1.31 1.9 1.79 2.76 4.44 2.53 7.02-0.18 12.02 0.07 24.05 0.02 36.07 0.32 5.22-5.71 9.02-10.39 7.06-3.29-1.12-5.26-4.63-5.01-8.01 0.03-12.04 0.36-24.08 0.05-36.12-0.27-3.08 1.7-5.88 4.32-7.33zm51.71 22.53c5.97-1.28 10.56 6.39 7.18 11.26-4.62 8.01-9.07 16.13-13.97 23.97-3.44 4.97-12.13 3.22-13.34-2.69-1.28-3.31 1.07-6.32 2.56-9.08 3.95-6.74 7.72-13.59 11.78-20.26 1.18-2.06 3.38-3.41 5.79-3.2zM139.51 31.4c1.73-1.65 4.37-1.14 6.54-1.05 2.47 0.2 3.91 2.59 5.15 4.46 4.07 7.21 8.31 14.34 12.43 21.51 2.05 3.26 1.71 8.29-1.64 10.58-3.34 2.71-8.73 1.73-10.95-1.93-4.38-7.07-8.19-14.47-12.54-21.55-2.82-3.55-3.06-9.3 1.01-12.02zm-7.77 60.19c15.57-6.43 32.54-8.54 49.25-9.3 14.27-1.27 28.62-0.06 42.82 1.45 11.87 1.9 24.01 4.03 34.72 9.76 4.37 2.28 9.08 5.56 10.17 10.68 0.11 28.37-15.19 56.9-40.37 70.59 0.91 4.09 2.5 8.06 2.37 12.32 0.11 13.63-0.07 27.27 0.1 40.91 0.25 15.33-0.43 30.67 0.37 45.99 0.37 13.01-0.97 26.01 0 39-0.17 11.64 0.46 23.31-0.76 34.92-1.84 11.36-9.39 21.51-19.54 26.85-10.02 5.23-22.41 5.43-32.78 1.07-13.54-5.77-22.82-20.07-22.57-34.79-0.05-50.69-0.03-101.37-0.06-152.05-0.26-4.72 1.17-9.25 2.38-13.74-11-6.67-20.69-15.57-27.64-26.43-8.37-13.28-12.76-29.01-12.97-44.66 1.81-6.69 8.74-9.88 14.51-12.57m12.69 8.16c-4.63 1.31-9.43 2.74-13.18 5.89 13.5 7.02 28.92 8.8 43.81 10.59 17.2 0.86 34.59 0.96 51.6-2.08 9.27-1.92 18.81-3.62 27.18-8.29l-0.24-1.48c-6.25-3.1-12.94-5.31-19.79-6.63-29.43-5.87-60.28-5.6-89.38 2m47.82 104.84c-6.92 0.6-13.12 6.33-13.61 13.37-0.15 6.35 0.04 12.71 0 19.06-0.18 5 1.79 10.45 6.2 13.18 4.78 3.31 11.55 3.29 16.41 0.15 4.16-2.61 6.4-7.64 6.33-12.46 0.04-6.56 0.19-13.14 0.04-19.7-0.38-7.7-7.85-13.81-15.37-13.6z"/>
</vector>

View File

@ -10,7 +10,6 @@
app:singleSelection="true"
tools:visibility="visible">
<com.google.android.material.button.MaterialButton
android:id="@+id/flash_off"
style="@style/Widget.App.Button.OutlineButton.IconOnly"
@ -35,5 +34,12 @@
android:layout_weight="1"
app:icon="@drawable/ic_flash_on_vector" />
<com.google.android.material.button.MaterialButton
android:id="@+id/flash_always_on"
style="@style/Widget.App.Button.OutlineButton.IconOnly"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:icon="@drawable/ic_flashlight_vector" />
</com.google.android.material.button.MaterialButtonToggleGroup>