diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt index a6f86dd3..a71eb4d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt @@ -1,20 +1,21 @@ package com.simplemobiletools.camera.dialogs -import android.hardware.Camera import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.View import com.simplemobiletools.camera.R import com.simplemobiletools.camera.activities.SimpleActivity -import com.simplemobiletools.camera.extensions.getAspectRatio -import com.simplemobiletools.camera.helpers.Config +import com.simplemobiletools.camera.extensions.config +import com.simplemobiletools.camera.models.MySize import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.models.RadioItem import kotlinx.android.synthetic.main.dialog_change_resolution.view.* -class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val isFrontCamera: Boolean, val callback: () -> Unit) { - var dialog: AlertDialog +class ChangeResolutionDialog(val activity: SimpleActivity, val isFrontCamera: Boolean, val photoResolutions: ArrayList, + val videoResolutions: ArrayList, val callback: () -> Unit) { + private var dialog: AlertDialog + private val config = activity.config init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply { @@ -31,7 +32,7 @@ class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, v } private fun setupPhotoResolutionPicker(view: View) { - val items = getFormattedResolutions(camera.parameters.supportedPictureSizes) + val items = getFormattedResolutions(photoResolutions) var selectionIndex = if (isFrontCamera) config.frontPhotoResIndex else config.backPhotoResIndex selectionIndex = Math.max(selectionIndex, 0) @@ -51,7 +52,7 @@ class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, v } private fun setupVideoResolutionPicker(view: View) { - val items = getFormattedResolutions(camera.parameters.supportedVideoSizes ?: camera.parameters.supportedPreviewSizes) + val items = getFormattedResolutions(videoResolutions) var selectionIndex = if (isFrontCamera) config.frontVideoResIndex else config.backVideoResIndex view.change_resolution_video_holder.setOnClickListener { @@ -69,7 +70,7 @@ class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, v view.change_resolution_video.text = items[selectionIndex].title } - private fun getFormattedResolutions(resolutions: List): ArrayList { + private fun getFormattedResolutions(resolutions: List): ArrayList { val items = ArrayList(resolutions.size) val sorted = resolutions.sortedByDescending { it.width * it.height } sorted.forEachIndexed { index, size -> diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Size.kt b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Size.kt index 46a26047..ffd2c630 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Size.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Size.kt @@ -4,62 +4,3 @@ import android.content.Context import android.hardware.Camera import com.simplemobiletools.camera.R -const val RATIO_TOLERANCE = 0.1f - -fun Camera.Size.isSixteenToNine(): Boolean { - val selectedRatio = Math.abs(width / height.toFloat()) - val checkedRatio = 16 / 9.toFloat() - val diff = Math.abs(selectedRatio - checkedRatio) - return diff < RATIO_TOLERANCE -} - -fun Camera.Size.isFiveToThree(): Boolean { - val selectedRatio = Math.abs(width / height.toFloat()) - val checkedRatio = 5 / 3.toFloat() - val diff = Math.abs(selectedRatio - checkedRatio) - return diff < RATIO_TOLERANCE -} - -fun Camera.Size.isFourToThree(): Boolean { - val selectedRatio = Math.abs(width / height.toFloat()) - val checkedRatio = 4 / 3.toFloat() - val diff = Math.abs(selectedRatio - checkedRatio) - return diff < RATIO_TOLERANCE -} - -fun Camera.Size.isThreeToFour(): Boolean { - val selectedRatio = Math.abs(width / height.toFloat()) - val checkedRatio = 3 / 4.toFloat() - val diff = Math.abs(selectedRatio - checkedRatio) - return diff < RATIO_TOLERANCE -} - -fun Camera.Size.isThreeToTwo(): Boolean { - val selectedRatio = Math.abs(width / height.toFloat()) - val checkedRatio = 3 / 2.toFloat() - val diff = Math.abs(selectedRatio - checkedRatio) - return diff < RATIO_TOLERANCE -} - -fun Camera.Size.isSixToFive(): Boolean { - val selectedRatio = Math.abs(width / height.toFloat()) - val checkedRatio = 6 / 5.toFloat() - val diff = Math.abs(selectedRatio - checkedRatio) - return diff < RATIO_TOLERANCE -} - -fun Camera.Size.isOneNineToOne() = Math.abs(1.9 - (width / height.toFloat())) < RATIO_TOLERANCE - -fun Camera.Size.isSquare() = width == height - -fun Camera.Size.getAspectRatio(context: Context) = when { - isSixteenToNine() -> "16:9" - isFiveToThree() -> "5:3" - isFourToThree() -> "4:3" - isThreeToFour() -> "3:4" - isThreeToTwo() -> "3:2" - isSixToFive() -> "6:5" - isOneNineToOne() -> "1.9:1" - isSquare() -> "1:1" - else -> context.resources.getString(R.string.other) -} diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt index 4d37980a..9de2ed92 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.camera.helpers const val ORIENT_PORTRAIT = 0 const val ORIENT_LANDSCAPE_LEFT = 1 const val ORIENT_LANDSCAPE_RIGHT = 2 +const val RATIO_TOLERANCE = 0.1f // shared preferences const val SAVE_PHOTOS = "save_photos" diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt b/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt new file mode 100644 index 00000000..fdbfe47d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt @@ -0,0 +1,65 @@ +package com.simplemobiletools.camera.models + +import android.content.Context +import com.simplemobiletools.camera.R +import com.simplemobiletools.camera.helpers.RATIO_TOLERANCE + +data class MySize(val width: Int, val height: Int) { + fun isSixteenToNine(): Boolean { + val selectedRatio = Math.abs(width / height.toFloat()) + val checkedRatio = 16 / 9.toFloat() + val diff = Math.abs(selectedRatio - checkedRatio) + return diff < RATIO_TOLERANCE + } + + private fun isFiveToThree(): Boolean { + val selectedRatio = Math.abs(width / height.toFloat()) + val checkedRatio = 5 / 3.toFloat() + val diff = Math.abs(selectedRatio - checkedRatio) + return diff < RATIO_TOLERANCE + } + + private fun isFourToThree(): Boolean { + val selectedRatio = Math.abs(width / height.toFloat()) + val checkedRatio = 4 / 3.toFloat() + val diff = Math.abs(selectedRatio - checkedRatio) + return diff < RATIO_TOLERANCE + } + + private fun isThreeToFour(): Boolean { + val selectedRatio = Math.abs(width / height.toFloat()) + val checkedRatio = 3 / 4.toFloat() + val diff = Math.abs(selectedRatio - checkedRatio) + return diff < RATIO_TOLERANCE + } + + private fun isThreeToTwo(): Boolean { + val selectedRatio = Math.abs(width / height.toFloat()) + val checkedRatio = 3 / 2.toFloat() + val diff = Math.abs(selectedRatio - checkedRatio) + return diff < RATIO_TOLERANCE + } + + private fun isSixToFive(): Boolean { + val selectedRatio = Math.abs(width / height.toFloat()) + val checkedRatio = 6 / 5.toFloat() + val diff = Math.abs(selectedRatio - checkedRatio) + return diff < RATIO_TOLERANCE + } + + private fun isOneNineToOne() = Math.abs(1.9 - (width / height.toFloat())) < RATIO_TOLERANCE + + private fun isSquare() = width == height + + fun getAspectRatio(context: Context) = when { + isSixteenToNine() -> "16:9" + isFiveToThree() -> "5:3" + isFourToThree() -> "4:3" + isThreeToFour() -> "3:4" + isThreeToTwo() -> "3:2" + isSixToFive() -> "6:5" + isOneNineToOne() -> "1.9:1" + isSquare() -> "1:1" + else -> context.resources.getString(R.string.other) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt index c3f225af..eb11f421 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt @@ -19,10 +19,14 @@ import android.view.* import com.simplemobiletools.camera.R import com.simplemobiletools.camera.activities.MainActivity import com.simplemobiletools.camera.dialogs.ChangeResolutionDialog -import com.simplemobiletools.camera.extensions.* +import com.simplemobiletools.camera.extensions.config +import com.simplemobiletools.camera.extensions.getMyCamera +import com.simplemobiletools.camera.extensions.getOutputMediaFile +import com.simplemobiletools.camera.extensions.realScreenSize import com.simplemobiletools.camera.helpers.* import com.simplemobiletools.camera.implementations.MyCameraOneImpl import com.simplemobiletools.camera.interfaces.MyPreview +import com.simplemobiletools.camera.models.MySize import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.isJellyBean1Plus import java.io.File @@ -219,7 +223,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MyPreview { rescheduleAutofocus() } - private fun getSelectedResolution(): Camera.Size { + private fun getSelectedResolution(): MySize { if (mParameters == null) { mParameters = mCamera!!.parameters } @@ -229,7 +233,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MyPreview { mParameters!!.supportedVideoSizes ?: mParameters!!.supportedPreviewSizes } else { mParameters!!.supportedPictureSizes - }.sortedByDescending { it.width * it.height } + }.map { MySize(it.width, it.height) }.sortedByDescending { it.width * it.height } if (index == -1) { index = getDefaultFullscreenResolution(resolutions) ?: 0 @@ -247,7 +251,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MyPreview { } } - private fun getDefaultFullscreenResolution(resolutions: List): Int? { + private fun getDefaultFullscreenResolution(resolutions: List): Int? { val screenAspectRatio = mActivity!!.realScreenSize.y / mActivity!!.realScreenSize.x.toFloat() resolutions.forEachIndexed { index, size -> val diff = screenAspectRatio - (size.width / size.height.toFloat()) @@ -491,7 +495,10 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MyPreview { override fun showChangeResolutionDialog() { if (mCamera != null) { val oldResolution = getSelectedResolution() - ChangeResolutionDialog(mActivity!!, mConfig, mCamera!!, getIsUsingFrontCamera()) { + val photoResolutions = mCamera!!.parameters.supportedPictureSizes.map { MySize(it.width, it.height) } as ArrayList + val videoSizes = mCamera!!.parameters.supportedVideoSizes ?: mCamera!!.parameters.supportedPreviewSizes + val videoResolutions = videoSizes.map { MySize(it.width, it.height) } as ArrayList + ChangeResolutionDialog(mActivity!!, getIsUsingFrontCamera(), photoResolutions, videoResolutions) { if (oldResolution != getSelectedResolution()) { refreshPreview() }