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

View File

@ -341,25 +341,72 @@ class CameraXPreview(
} }
override fun showChangeResolution() { 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 selectedResolution = if (isPhotoCapture) {
val resolutions = if (isPhotoCapture) imageSizes.map { it.toResolutionOption() } else videoSizes.map { it.toResolutionOption() } imageQualityManager.getUserSelectedResolution(cameraSelector).toResolutionOption()
} else {
listener.showImageSizes( videoQualityManager.getUserSelectedQuality(cameraSelector).toResolutionOption()
selectedResolution = selectedResolution,
resolutions = resolutions,
isPhotoCapture = isPhotoCapture,
isFrontCamera = isFrontCameraInUse()
) { changed ->
if (changed) {
currentRecording?.stop()
}
startCamera()
} }
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,
isPhotoCapture = isPhotoCapture,
isFrontCamera = isFrontCameraInUse()
) { changed ->
if (changed) {
currentRecording?.stop()
}
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() { override fun toggleFrontBackCamera() {
@ -373,6 +420,24 @@ class CameraXPreview(
startCamera(switching = true) 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) { override fun setFlashlightState(state: Int) {
val newFlashMode = state.toCameraXFlashMode() val newFlashMode = state.toCameraXFlashMode()
if (!isPhotoCapture) { if (!isPhotoCapture) {