add toggling flash mode / media size

This commit is contained in:
darthpaul 2022-08-25 08:30:56 +01:00
parent 64ff75511c
commit fd5f64ecf3
2 changed files with 114 additions and 47 deletions

View File

@ -21,7 +21,6 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.button.MaterialButton
import com.google.android.material.button.MaterialButtonToggleGroup
import com.google.android.material.tabs.TabLayout
import com.simplemobiletools.camera.BuildConfig
import com.simplemobiletools.camera.R
@ -59,7 +58,6 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
private lateinit var mCameraImpl: MyCameraImpl
private var mPreview: MyPreview? = null
private var mPreviewUri: Uri? = null
private var buttonCheckedListener: MaterialButtonToggleGroup.OnButtonCheckedListener? = null
private var mIsInPhotoMode = true
private var mIsCameraAvailable = false
private var mIsHardwareShutterHandled = false
@ -361,7 +359,11 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
private fun toggleFlash() {
if (checkCameraAvailable()) {
if (mIsInPhotoMode) {
showFlashOptions(mIsInPhotoMode)
} else {
mPreview?.toggleFlashlight()
}
}
}
@ -706,14 +708,8 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
media_size_toggle_group.removeAllViews()
media_size_toggle_group.clearChecked()
resolutions.map(::createButton).forEach { button ->
media_size_toggle_group.addView(button)
}
buttonCheckedListener?.let { media_size_toggle_group.removeOnButtonCheckedListener(it) }
buttonCheckedListener = MaterialButtonToggleGroup.OnButtonCheckedListener { _, checkedId, isChecked ->
if (isChecked) {
val index = resolutions.indexOfFirst { it.buttonViewId == checkedId }
val onItemClick = { clickedViewId: Int ->
val index = resolutions.indexOfFirst { it.buttonViewId == clickedViewId }
if (isPhotoCapture) {
if (isFrontCamera) {
config.frontPhotoResIndex = index
@ -728,17 +724,20 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
}
}
closeOptions()
onSelect.invoke(selectedResolution.buttonViewId != checkedId)
onSelect.invoke(selectedResolution.buttonViewId != clickedViewId)
}
resolutions.map {
createButton(it, onItemClick)
}.forEach { button ->
media_size_toggle_group.addView(button)
}
buttonCheckedListener?.let {
media_size_toggle_group.check(selectedResolution.buttonViewId)
media_size_toggle_group.addOnButtonCheckedListener(it)
}
showResolutionOptions()
}
private fun createButton(resolutionOption: ResolutionOption): MaterialButton {
private fun createButton(resolutionOption: ResolutionOption, onClick: (clickedViewId: Int) -> Unit): MaterialButton {
val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT).apply {
weight = 1f
}
@ -746,6 +745,9 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
layoutParams = params
icon = AppCompatResources.getDrawable(context, resolutionOption.imageDrawableResId)
id = resolutionOption.buttonViewId
setOnClickListener {
onClick.invoke(id)
}
}
}

View File

@ -341,14 +341,19 @@ class CameraXPreview(
}
override fun showChangeResolution() {
val imageSizes = imageQualityManager.getSupportedResolutions(cameraSelector)
val videoSizes = videoQualityManager.getSupportedQualities(cameraSelector)
val selectedVideoSize = videoQualityManager.getUserSelectedQuality(cameraSelector)
val selectedImageSize = imageQualityManager.getUserSelectedResolution(cameraSelector)
val selectedResolution = if (isPhotoCapture) selectedImageSize.toResolutionOption() else selectedVideoSize.toResolutionOption()
val resolutions = if (isPhotoCapture) imageSizes.map { it.toResolutionOption() } else videoSizes.map { it.toResolutionOption() }
val selectedResolution = if (isPhotoCapture) {
imageQualityManager.getUserSelectedResolution(cameraSelector).toResolutionOption()
} else {
videoQualityManager.getUserSelectedQuality(cameraSelector).toResolutionOption()
}
val resolutions = if (isPhotoCapture) {
imageQualityManager.getSupportedResolutions(cameraSelector).map { it.toResolutionOption() }
} else {
videoQualityManager.getSupportedQualities(cameraSelector).map { it.toResolutionOption() }
}
if (resolutions.size > 2) {
listener.showImageSizes(
selectedResolution = selectedResolution,
resolutions = resolutions,
@ -360,6 +365,48 @@ class CameraXPreview(
}
startCamera()
}
} else {
toggleResolutions(resolutions)
}
}
private fun toggleResolutions(resolutions: List<ResolutionOption>) {
val currentIndex = if (isPhotoCapture) {
if (isFrontCameraInUse()) {
config.frontPhotoResIndex
} else {
config.backPhotoResIndex
}
} else {
if (isFrontCameraInUse()) {
config.frontVideoResIndex
} else {
config.backVideoResIndex
}
}
val nextIndex = if (currentIndex >= resolutions.lastIndex) {
0
} else {
currentIndex + 1
}
if (isPhotoCapture) {
if (isFrontCameraInUse()) {
config.frontPhotoResIndex = nextIndex
} else {
config.backPhotoResIndex = nextIndex
}
} else {
if (isFrontCameraInUse()) {
config.frontVideoResIndex = nextIndex
} else {
config.backVideoResIndex = nextIndex
}
}
currentRecording?.stop()
startCamera()
}
override fun toggleFrontBackCamera() {
@ -373,6 +420,24 @@ class CameraXPreview(
startCamera(switching = true)
}
override fun toggleFlashlight() {
val newFlashMode = if (isPhotoCapture) {
when (flashMode) {
FLASH_MODE_OFF -> FLASH_MODE_ON
FLASH_MODE_ON -> FLASH_MODE_AUTO
FLASH_MODE_AUTO -> FLASH_MODE_OFF
else -> throw IllegalArgumentException("Unknown mode: $flashMode")
}
} else {
when (flashMode) {
FLASH_MODE_OFF -> FLASH_MODE_ON
FLASH_MODE_ON -> FLASH_MODE_OFF
else -> throw IllegalArgumentException("Unknown mode: $flashMode")
}
}
setFlashlightState(newFlashMode.toAppFlashMode())
}
override fun setFlashlightState(state: Int) {
val newFlashMode = state.toCameraXFlashMode()
if (!isPhotoCapture) {