add always on torch mode for image capture

This commit is contained in:
darthpaul 2022-10-23 00:00:22 +01:00
parent 122841b641
commit 3d4c38de04
6 changed files with 52 additions and 10 deletions

View File

@ -39,10 +39,13 @@ import com.simplemobiletools.camera.views.FocusCircleView
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
import java.util.concurrent.TimeUnit
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_flash.* import kotlinx.android.synthetic.main.layout_flash.*
import kotlinx.android.synthetic.main.layout_top.* import kotlinx.android.synthetic.main.layout_top.change_resolution
import java.util.concurrent.TimeUnit 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 { class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, CameraXPreviewListener {
private companion object { private companion object {
@ -342,7 +345,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
mTimerHandler = Handler(Looper.getMainLooper()) mTimerHandler = Handler(Looper.getMainLooper())
setupPreviewImage(true) 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) mPreview!!.setFlashlightState(initialFlashlightState)
updateFlashlightState(initialFlashlightState) updateFlashlightState(initialFlashlightState)
initFlashModeTransitionNames() initFlashModeTransitionNames()
@ -353,6 +356,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
flash_auto.transitionName = "$baseName$FLASH_AUTO" flash_auto.transitionName = "$baseName$FLASH_AUTO"
flash_off.transitionName = "$baseName$FLASH_OFF" flash_off.transitionName = "$baseName$FLASH_OFF"
flash_on.transitionName = "$baseName$FLASH_ON" flash_on.transitionName = "$baseName$FLASH_ON"
flash_always_on.transitionName = "$baseName$FLASH_ALWAYS_ON"
} }
private fun initButtons() { private fun initButtons() {
@ -374,6 +378,9 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
flash_auto.setShadowIcon(R.drawable.ic_flash_auto_vector) flash_auto.setShadowIcon(R.drawable.ic_flash_auto_vector)
flash_auto.setOnClickListener { selectFlashMode(FLASH_AUTO) } flash_auto.setOnClickListener { selectFlashMode(FLASH_AUTO) }
flash_always_on.setShadowIcon(R.drawable.ic_flashlight_vector)
flash_always_on.setOnClickListener { selectFlashMode(FLASH_ALWAYS_ON) }
} }
private fun selectFlashMode(flashMode: Int) { private fun selectFlashMode(flashMode: Int) {
@ -409,7 +416,8 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
val flashDrawable = when (state) { val flashDrawable = when (state) {
FLASH_OFF -> R.drawable.ic_flash_off_vector FLASH_OFF -> R.drawable.ic_flash_off_vector
FLASH_ON -> R.drawable.ic_flash_on_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.setShadowIcon(flashDrawable)
toggle_flash.transitionName = "${getString(R.string.toggle_flash)}$state" toggle_flash.transitionName = "${getString(R.string.toggle_flash)}$state"
@ -814,6 +822,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
val transitionSet = createTransition() val transitionSet = createTransition()
TransitionManager.go(flashModeScene, transitionSet) TransitionManager.go(flashModeScene, transitionSet)
flash_auto.beVisibleIf(photoCapture) flash_auto.beVisibleIf(photoCapture)
flash_always_on.beVisibleIf(photoCapture)
flash_toggle_group.check(config.flashlightState.toFlashModeId()) flash_toggle_group.check(config.flashlightState.toFlashModeId())
flash_toggle_group.beVisible() flash_toggle_group.beVisible()

View File

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

View File

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

View File

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

View File

@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="384dp"
android:height="384dp"
android:viewportWidth="384"
android:viewportHeight="384">
<path
android:pathData="M189.53,7.64C192.29,7.13 195.94,6.56 198.01,8.95C199.91,10.74 200.77,13.39 200.54,15.97C200.36,27.99 200.61,40.02 200.56,52.04C200.88,57.26 194.85,61.06 190.17,59.1C186.88,57.98 184.91,54.47 185.16,51.09C185.19,39.05 185.52,27.01 185.21,14.97C184.94,11.89 186.91,9.09 189.53,7.64Z"
android:fillColor="#ffffff"/>
<path
android:pathData="M241.24,30.17C247.21,28.89 251.8,36.56 248.42,41.43C243.8,49.44 239.35,57.56 234.45,65.4C231.01,70.37 222.32,68.62 221.11,62.71C219.83,59.4 222.18,56.39 223.67,53.63C227.62,46.89 231.39,40.04 235.45,33.37C236.63,31.31 238.83,29.96 241.24,30.17Z"
android:fillColor="#ffffff"/>
<path
android:pathData="M139.51,31.4C141.24,29.75 143.88,30.26 146.05,30.35C148.52,30.55 149.96,32.94 151.2,34.81C155.27,42.02 159.51,49.15 163.63,56.32C165.68,59.58 165.34,64.61 161.99,66.9C158.65,69.61 153.26,68.63 151.04,64.97C146.66,57.9 142.85,50.5 138.5,43.42C135.68,39.87 135.44,34.12 139.51,31.4Z"
android:fillColor="#ffffff"/>
<path
android:pathData="M131.74,91.59C147.31,85.16 164.28,83.05 180.99,82.29C195.26,81.02 209.61,82.23 223.81,83.74C235.68,85.64 247.82,87.77 258.53,93.5C262.9,95.78 267.61,99.06 268.7,104.18C268.81,132.55 253.51,161.08 228.33,174.77C229.24,178.86 230.83,182.83 230.7,187.09C230.81,200.72 230.63,214.36 230.8,228C231.05,243.33 230.37,258.67 231.17,273.99C231.54,287 230.2,300 231.17,312.99C231,324.63 231.63,336.3 230.41,347.91C228.57,359.27 221.02,369.42 210.87,374.76C200.85,379.99 188.46,380.19 178.09,375.83C164.55,370.06 155.27,355.76 155.52,341.04C155.47,290.35 155.49,239.67 155.46,188.99C155.2,184.27 156.63,179.74 157.84,175.25C146.84,168.58 137.15,159.68 130.2,148.82C121.83,135.54 117.44,119.81 117.23,104.16C119.04,97.47 125.97,94.28 131.74,91.59M144.43,99.75C139.8,101.06 135,102.49 131.25,105.64C144.75,112.66 160.17,114.44 175.06,116.23C192.26,117.09 209.65,117.19 226.66,114.15C235.93,112.23 245.47,110.53 253.84,105.86C253.78,105.49 253.66,104.75 253.6,104.38C247.35,101.28 240.66,99.07 233.81,97.75C204.38,91.88 173.53,92.15 144.43,99.75M192.25,204.59C185.33,205.19 179.13,210.92 178.64,217.96C178.49,224.31 178.68,230.67 178.64,237.02C178.46,242.02 180.43,247.47 184.84,250.2C189.62,253.51 196.39,253.49 201.25,250.35C205.41,247.74 207.65,242.71 207.58,237.89C207.62,231.33 207.77,224.75 207.62,218.19C207.24,210.49 199.77,204.38 192.25,204.59Z"
android:fillColor="#ffffff"/>
</vector>

View File

@ -36,4 +36,13 @@
app:icon="@drawable/ic_flash_on_vector" /> 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> </com.google.android.material.button.MaterialButtonToggleGroup>