create a helper function for getting the proper camera impl

This commit is contained in:
tibbi 2018-05-27 23:35:40 +02:00
parent 3e6aab0cb5
commit 2cdae95de8
9 changed files with 49 additions and 51 deletions

View File

@ -16,10 +16,9 @@ import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.camera.BuildConfig
import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.getMyCamera
import com.simplemobiletools.camera.extensions.navBarHeight
import com.simplemobiletools.camera.helpers.*
import com.simplemobiletools.camera.implementations.MyCameraOneImpl
import com.simplemobiletools.camera.implementations.MyCameraTwoImpl
import com.simplemobiletools.camera.interfaces.MyCamera
import com.simplemobiletools.camera.views.FocusCircleView
import com.simplemobiletools.camera.views.PreviewCameraOne
@ -121,7 +120,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
mCurrVideoRecTimer = 0
mCurrCameraId = 0
mLastHandledOrientation = 0
mCameraImpl = if (isLollipopPlus()) MyCameraTwoImpl(applicationContext) else MyCameraOneImpl(applicationContext)
mCameraImpl = getMyCamera()
if (config.alwaysOpenBackCamera) {
config.lastUsedCamera = mCameraImpl.getBackCameraId()

View File

@ -8,6 +8,7 @@ import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.activities.SimpleActivity
import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.getAspectRatio
import com.simplemobiletools.camera.extensions.getMyCamera
import com.simplemobiletools.camera.helpers.Config
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
@ -16,7 +17,7 @@ import kotlinx.android.synthetic.main.dialog_change_resolution.view.*
class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) {
var dialog: AlertDialog
private val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
private val isBackCamera = activity.config.lastUsedCamera == activity.getMyCamera().getBackCameraId()
init {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply {

View File

@ -9,7 +9,7 @@ fun Activity.getPreviewRotation(cameraId: Int): Int {
val degrees = getDeviceRotationDegrees()
var result: Int
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
if (info.facing == getMyCamera().getFrontCameraId()) {
result = (info.orientation + degrees) % 360
result = 360 - result
} else {

View File

@ -4,6 +4,11 @@ import android.content.Context
import android.graphics.Point
import android.view.WindowManager
import com.simplemobiletools.camera.helpers.Config
import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_LEFT
import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_RIGHT
import com.simplemobiletools.camera.implementations.MyCameraOneImpl
import com.simplemobiletools.camera.implementations.MyCameraTwoImpl
import com.simplemobiletools.commons.helpers.isLollipopPlus
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
@ -44,3 +49,12 @@ val Context.realScreenSize: Point
}
val Context.navBarHeight: Int get() = realScreenSize.y - usableScreenSize.y
fun Context.getMyCamera() = if (isLollipopPlus()) MyCameraTwoImpl(applicationContext) else MyCameraOneImpl(applicationContext)
fun Context.compensateDeviceRotation(orientation: Int, currCameraId: Int) = when {
orientation == ORIENT_LANDSCAPE_LEFT -> 270
orientation == ORIENT_LANDSCAPE_RIGHT -> 90
currCameraId == getMyCamera().getFrontCameraId() -> 180
else -> 0
}

View File

@ -1,12 +0,0 @@
package com.simplemobiletools.camera.extensions
import android.hardware.Camera
import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_LEFT
import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_RIGHT
fun Int.compensateDeviceRotation(currCameraId: Int) = when {
this == ORIENT_LANDSCAPE_LEFT -> 270
this == ORIENT_LANDSCAPE_RIGHT -> 90
currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT -> 180
else -> 0
}

View File

@ -1,8 +1,8 @@
package com.simplemobiletools.camera.helpers
import android.content.Context
import android.hardware.Camera
import android.os.Environment
import com.simplemobiletools.camera.extensions.getMyCamera
import com.simplemobiletools.commons.helpers.BaseConfig
import java.io.File
@ -47,7 +47,7 @@ class Config(context: Context) : BaseConfig(context) {
set(flipPhotos) = prefs.edit().putBoolean(FLIP_PHOTOS, flipPhotos).apply()
var lastUsedCamera: Int
get() = prefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK)
get() = prefs.getInt(LAST_USED_CAMERA, context.getMyCamera().getBackCameraId())
set(cameraId) = prefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply()
var flashlightState: Int

View File

@ -1,29 +1,29 @@
package com.simplemobiletools.camera.helpers
val ORIENT_PORTRAIT = 0
val ORIENT_LANDSCAPE_LEFT = 1
val ORIENT_LANDSCAPE_RIGHT = 2
const val ORIENT_PORTRAIT = 0
const val ORIENT_LANDSCAPE_LEFT = 1
const val ORIENT_LANDSCAPE_RIGHT = 2
// shared preferences
val SAVE_PHOTOS = "save_photos"
val SHOW_PREVIEW = "show_preview"
val SOUND = "sound"
val FOCUS_BEFORE_CAPTURE = "focus_before_capture"
val VOLUME_BUTTONS_AS_SHUTTER = "volume_buttons_as_shutter"
val TURN_FLASH_OFF_AT_STARTUP = "turn_flash_off_at_startup"
val FLIP_PHOTOS = "flip_photos"
val LAST_USED_CAMERA = "last_used_camera"
val FLASHLIGHT_STATE = "flashlight_state"
val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index"
val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index"
val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index"
val FRONT_VIDEO_RESOLUTION_INDEX = "front_video_resolution_index"
val PHOTO_PREVIEW_HINT_SHOWN = "photo_preview_hint_shown"
val KEEP_SETTINGS_VISIBLE = "keep_settings_visible"
val ALWAYS_OPEN_BACK_CAMERA = "always_open_back_camera"
val SAVE_PHOTO_METADATA = "save_photo_metadata"
val PHOTO_QUALITY = "photo_quality"
const val SAVE_PHOTOS = "save_photos"
const val SHOW_PREVIEW = "show_preview"
const val SOUND = "sound"
const val FOCUS_BEFORE_CAPTURE = "focus_before_capture"
const val VOLUME_BUTTONS_AS_SHUTTER = "volume_buttons_as_shutter"
const val TURN_FLASH_OFF_AT_STARTUP = "turn_flash_off_at_startup"
const val FLIP_PHOTOS = "flip_photos"
const val LAST_USED_CAMERA = "last_used_camera"
const val FLASHLIGHT_STATE = "flashlight_state"
const val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index"
const val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index"
const val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index"
const val FRONT_VIDEO_RESOLUTION_INDEX = "front_video_resolution_index"
const val PHOTO_PREVIEW_HINT_SHOWN = "photo_preview_hint_shown"
const val KEEP_SETTINGS_VISIBLE = "keep_settings_visible"
const val ALWAYS_OPEN_BACK_CAMERA = "always_open_back_camera"
const val SAVE_PHOTO_METADATA = "save_photo_metadata"
const val PHOTO_QUALITY = "photo_quality"
val FLASH_OFF = 0
val FLASH_ON = 1
val FLASH_AUTO = 2
const val FLASH_OFF = 0
const val FLASH_ON = 1
const val FLASH_AUTO = 2

View File

@ -3,17 +3,13 @@ package com.simplemobiletools.camera.helpers
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.hardware.Camera
import android.media.ExifInterface
import android.net.Uri
import android.os.AsyncTask
import android.os.Environment
import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.activities.MainActivity
import com.simplemobiletools.camera.extensions.compensateDeviceRotation
import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.getOutputMediaFile
import com.simplemobiletools.camera.extensions.getPreviewRotation
import com.simplemobiletools.camera.extensions.*
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.isNougatPlus
import java.io.File
@ -72,7 +68,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
var image = BitmapFactory.decodeByteArray(data, 0, data.size)
val exif = ExifInterface(photoFile.toString())
val deviceRot = deviceOrientation.compensateDeviceRotation(currCameraId)
val deviceRot = activity.compensateDeviceRotation(deviceOrientation, currCameraId)
val previewRot = activity.getPreviewRotation(currCameraId)
val orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)
val imageRot = orient.degreesFromOrientation()
@ -82,7 +78,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
image = rotate(image, totalRotation)
}
if (currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT && !activity.config.flipPhotos) {
if (currCameraId == activity.getMyCamera().getFrontCameraId() && !activity.config.flipPhotos) {
val matrix = Matrix()
if (path.startsWith(activity.internalStoragePath)) {
matrix.preScale(1f, -1f)

View File

@ -735,7 +735,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
}
private fun getVideoRotation(): Int {
val deviceRot = mActivity!!.mLastHandledOrientation.compensateDeviceRotation(mCurrCameraId)
val deviceRot = mActivity!!.compensateDeviceRotation(mActivity!!.mLastHandledOrientation, mCurrCameraId)
val previewRot = mActivity!!.getPreviewRotation(mCurrCameraId)
return (deviceRot + previewRot) % 360
}