mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-02-17 03:40:38 +01:00
removing the Utils class
This commit is contained in:
parent
0cfebacd01
commit
9a6a4ffe9e
@ -32,7 +32,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.simplemobiletools:commons:2.13.7'
|
compile 'com.simplemobiletools:commons:2.13.9'
|
||||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import android.os.AsyncTask
|
|||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.simplemobiletools.camera.activities.MainActivity
|
import com.simplemobiletools.camera.activities.MainActivity
|
||||||
|
import com.simplemobiletools.camera.extensions.getOutputMediaFile
|
||||||
import com.simplemobiletools.commons.extensions.getFileDocument
|
import com.simplemobiletools.commons.extensions.getFileDocument
|
||||||
import com.simplemobiletools.commons.extensions.needsStupidWritePermissions
|
import com.simplemobiletools.commons.extensions.needsStupidWritePermissions
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
@ -28,7 +29,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?) : AsyncTask<Byte
|
|||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
path = uri.path
|
path = uri.path
|
||||||
} else {
|
} else {
|
||||||
path = Utils.getOutputMediaFile(activity, true)
|
path = activity.getOutputMediaFile(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isEmpty()) {
|
if (path.isEmpty()) {
|
||||||
|
@ -17,7 +17,8 @@ import android.view.SurfaceHolder
|
|||||||
import android.view.SurfaceView
|
import android.view.SurfaceView
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import com.simplemobiletools.camera.activities.MainActivity
|
import com.simplemobiletools.camera.activities.MainActivity
|
||||||
import com.simplemobiletools.camera.extensions.config
|
import com.simplemobiletools.camera.extensions.*
|
||||||
|
import com.simplemobiletools.commons.extensions.getNavBarHeight
|
||||||
import com.simplemobiletools.commons.extensions.needsStupidWritePermissions
|
import com.simplemobiletools.commons.extensions.needsStupidWritePermissions
|
||||||
import com.simplemobiletools.commons.extensions.scanPath
|
import com.simplemobiletools.commons.extensions.scanPath
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
@ -80,7 +81,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
mSetupPreviewAfterMeasure = false
|
mSetupPreviewAfterMeasure = false
|
||||||
mCurrVideoPath = ""
|
mCurrVideoPath = ""
|
||||||
config = activity.config
|
config = activity.config
|
||||||
mScreenSize = Utils.getScreenSize(activity)
|
mScreenSize = getScreenSize()
|
||||||
initGestureDetector()
|
initGestureDetector()
|
||||||
|
|
||||||
mSurfaceView.setOnTouchListener { view, event ->
|
mSurfaceView.setOnTouchListener { view, event ->
|
||||||
@ -142,8 +143,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
|
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
|
||||||
mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
|
mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
|
||||||
|
|
||||||
val rotation = Utils.getPreviewRotation(mActivity, cameraId)
|
mCamera!!.setDisplayOrientation(mActivity.getPreviewRotation(cameraId))
|
||||||
mCamera!!.setDisplayOrientation(rotation)
|
|
||||||
mCamera!!.parameters = mParameters
|
mCamera!!.parameters = mParameters
|
||||||
|
|
||||||
if (mCanTakePicture) {
|
if (mCanTakePicture) {
|
||||||
@ -155,7 +155,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mCallback.setFlashAvailable(Utils.hasFlash(mCamera))
|
mCallback.setFlashAvailable(hasFlash(mCamera))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mIsVideoMode) {
|
if (mIsVideoMode) {
|
||||||
@ -220,8 +220,8 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
|
|
||||||
fun takePicture() {
|
fun takePicture() {
|
||||||
if (mCanTakePicture) {
|
if (mCanTakePicture) {
|
||||||
var rotation = Utils.getMediaRotation(mActivity, mCurrCameraId)
|
var rotation = mActivity.getMediaRotation(mCurrCameraId)
|
||||||
rotation += Utils.compensateDeviceRotation(mCurrCameraId, mCallback.getCurrentOrientation())
|
rotation += mCallback.getCurrentOrientation().compensateDeviceRotation(mCurrCameraId)
|
||||||
|
|
||||||
/*final Camera.Size maxSize = getOptimalPictureSize();
|
/*final Camera.Size maxSize = getOptimalPictureSize();
|
||||||
mParameters.setPictureSize(maxSize.width, maxSize.height);*/
|
mParameters.setPictureSize(maxSize.width, maxSize.height);*/
|
||||||
@ -476,7 +476,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
mRecorder!!.setVideoSource(MediaRecorder.VideoSource.DEFAULT)
|
mRecorder!!.setVideoSource(MediaRecorder.VideoSource.DEFAULT)
|
||||||
mRecorder!!.setAudioSource(MediaRecorder.AudioSource.DEFAULT)
|
mRecorder!!.setAudioSource(MediaRecorder.AudioSource.DEFAULT)
|
||||||
|
|
||||||
mCurrVideoPath = Utils.getOutputMediaFile(mActivity, false)
|
mCurrVideoPath = mActivity.getOutputMediaFile(false)
|
||||||
if (mCurrVideoPath.isEmpty()) {
|
if (mCurrVideoPath.isEmpty()) {
|
||||||
mActivity.toast(R.string.video_creating_error)
|
mActivity.toast(R.string.video_creating_error)
|
||||||
return false
|
return false
|
||||||
@ -510,7 +510,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
}
|
}
|
||||||
mRecorder!!.setPreviewDisplay(mSurfaceHolder.surface)
|
mRecorder!!.setPreviewDisplay(mSurfaceHolder.surface)
|
||||||
|
|
||||||
val rotation = Utils.getFinalRotation(mActivity, mCurrCameraId, mCallback.getCurrentOrientation())
|
val rotation = mActivity.getFinalRotation(mCurrCameraId, mCallback.getCurrentOrientation())
|
||||||
mInitVideoRotation = rotation
|
mInitVideoRotation = rotation
|
||||||
mRecorder!!.setOrientationHint(rotation)
|
mRecorder!!.setOrientationHint(rotation)
|
||||||
|
|
||||||
@ -541,7 +541,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startRecording() {
|
private fun startRecording() {
|
||||||
if (mInitVideoRotation != Utils.getFinalRotation(mActivity, mCurrCameraId, mCallback.getCurrentOrientation())) {
|
if (mInitVideoRotation != mActivity.getFinalRotation(mCurrCameraId, mCallback.getCurrentOrientation())) {
|
||||||
cleanupRecorder()
|
cleanupRecorder()
|
||||||
initRecorder()
|
initRecorder()
|
||||||
}
|
}
|
||||||
@ -598,6 +598,32 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
|
|||||||
toggleShutterSound(false)
|
toggleShutterSound(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun hasFlash(camera: Camera?): Boolean {
|
||||||
|
if (camera == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (camera.parameters.flashMode == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val supportedFlashModes = camera.parameters.supportedFlashModes
|
||||||
|
if (supportedFlashModes == null || supportedFlashModes.isEmpty() ||
|
||||||
|
supportedFlashModes.size == 1 && supportedFlashModes[0] == Camera.Parameters.FLASH_MODE_OFF) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getScreenSize(): Point {
|
||||||
|
val display = mActivity.windowManager.defaultDisplay
|
||||||
|
val size = Point()
|
||||||
|
display.getSize(size)
|
||||||
|
size.y += mActivity.resources.getNavBarHeight()
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
private class SizesComparator : Comparator<Camera.Size>, Serializable {
|
private class SizesComparator : Comparator<Camera.Size>, Serializable {
|
||||||
|
|
||||||
override fun compare(a: Camera.Size, b: Camera.Size): Int {
|
override fun compare(a: Camera.Size, b: Camera.Size): Int {
|
||||||
|
@ -1,154 +0,0 @@
|
|||||||
package com.simplemobiletools.camera
|
|
||||||
|
|
||||||
import android.Manifest
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.content.res.Resources
|
|
||||||
import android.graphics.Point
|
|
||||||
import android.hardware.Camera
|
|
||||||
import android.support.v4.content.ContextCompat
|
|
||||||
import android.view.Surface
|
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
|
||||||
import java.io.File
|
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class Utils {
|
|
||||||
companion object {
|
|
||||||
fun getCameraInfo(cameraId: Int): Camera.CameraInfo {
|
|
||||||
val info = android.hardware.Camera.CameraInfo()
|
|
||||||
Camera.getCameraInfo(cameraId, info)
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showToast(context: Context, resId: Int) = context.toast(resId)
|
|
||||||
|
|
||||||
fun hasFlash(camera: Camera?): Boolean {
|
|
||||||
if (camera == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (camera.parameters.flashMode == null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val supportedFlashModes = camera.parameters.supportedFlashModes
|
|
||||||
if (supportedFlashModes == null || supportedFlashModes.isEmpty() ||
|
|
||||||
supportedFlashModes.size == 1 && supportedFlashModes[0] == Camera.Parameters.FLASH_MODE_OFF) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getOutputMediaFile(context: Context, isPhoto: Boolean): String {
|
|
||||||
val mediaStorageDir = File(Config.newInstance(context).savePhotosFolder)
|
|
||||||
|
|
||||||
if (!mediaStorageDir.exists()) {
|
|
||||||
if (!mediaStorageDir.mkdirs()) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
|
||||||
return if (isPhoto) {
|
|
||||||
"${mediaStorageDir.path}${File.separator}IMG_$timestamp.jpg"
|
|
||||||
} else {
|
|
||||||
"${mediaStorageDir.path}${File.separator}VID_$timestamp.mp4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun formatSeconds(duration: Int): String {
|
|
||||||
val sb = StringBuilder(8)
|
|
||||||
val hours = duration / (60 * 60)
|
|
||||||
val minutes = duration % (60 * 60) / 60
|
|
||||||
val seconds = duration % (60 * 60) % 60
|
|
||||||
|
|
||||||
if (duration > 3600000) {
|
|
||||||
sb.append(String.format(Locale.getDefault(), "%02d", hours)).append(":")
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.append(String.format(Locale.getDefault(), "%02d", minutes))
|
|
||||||
sb.append(":").append(String.format(Locale.getDefault(), "%02d", seconds))
|
|
||||||
|
|
||||||
return sb.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getScreenSize(activity: Activity): Point {
|
|
||||||
val display = activity.windowManager.defaultDisplay
|
|
||||||
val size = Point()
|
|
||||||
display.getSize(size)
|
|
||||||
size.y += getNavBarHeight(activity.resources)
|
|
||||||
return size
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getNavBarHeight(res: Resources): Int {
|
|
||||||
val id = res.getIdentifier("navigation_bar_height", "dimen", "android")
|
|
||||||
return if (id > 0 && hasNavBar(res)) {
|
|
||||||
res.getDimensionPixelSize(id)
|
|
||||||
} else
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
fun hasNavBar(res: Resources): Boolean {
|
|
||||||
val id = res.getIdentifier("config_showNavigationBar", "bool", "android")
|
|
||||||
return id > 0 && res.getBoolean(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun hasCameraPermission(context: Context) = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
|
|
||||||
|
|
||||||
fun hasAudioPermission(cxt: Context) = ContextCompat.checkSelfPermission(cxt, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
|
|
||||||
|
|
||||||
fun getRotationDegrees(activity: Activity): Int {
|
|
||||||
return when (activity.windowManager.defaultDisplay.rotation) {
|
|
||||||
Surface.ROTATION_0 -> 0
|
|
||||||
Surface.ROTATION_90 -> 90
|
|
||||||
Surface.ROTATION_180 -> 180
|
|
||||||
Surface.ROTATION_270 -> 270
|
|
||||||
else -> 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getPreviewRotation(activity: Activity, cameraId: Int): Int {
|
|
||||||
val info = Utils.getCameraInfo(cameraId)
|
|
||||||
val degrees = Utils.getRotationDegrees(activity)
|
|
||||||
|
|
||||||
var result: Int
|
|
||||||
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
||||||
result = (info.orientation + degrees) % 360
|
|
||||||
result = 360 - result
|
|
||||||
} else {
|
|
||||||
result = info.orientation - degrees + 360
|
|
||||||
}
|
|
||||||
|
|
||||||
return result % 360
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getMediaRotation(activity: Activity, cameraId: Int): Int {
|
|
||||||
val degrees = Utils.getRotationDegrees(activity)
|
|
||||||
val info = Utils.getCameraInfo(cameraId)
|
|
||||||
return if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
||||||
(360 + info.orientation + degrees) % 360
|
|
||||||
} else
|
|
||||||
(360 + info.orientation - degrees) % 360
|
|
||||||
}
|
|
||||||
|
|
||||||
fun compensateDeviceRotation(currCameraId: Int, deviceOrientation: Int): Int {
|
|
||||||
var degrees = 0
|
|
||||||
val isFrontCamera = currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT
|
|
||||||
if (deviceOrientation == Constants.ORIENT_LANDSCAPE_LEFT) {
|
|
||||||
degrees += if (isFrontCamera) 90 else 270
|
|
||||||
} else if (deviceOrientation == Constants.ORIENT_LANDSCAPE_RIGHT) {
|
|
||||||
degrees += if (isFrontCamera) 270 else 90
|
|
||||||
}
|
|
||||||
return degrees
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getFinalRotation(activity: Activity, currCameraId: Int, deviceOrientation: Int): Int {
|
|
||||||
var rotation = Utils.getMediaRotation(activity, currCameraId)
|
|
||||||
rotation += Utils.compensateDeviceRotation(currCameraId, deviceOrientation)
|
|
||||||
return rotation % 360
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -24,10 +24,7 @@ import com.simplemobiletools.camera.*
|
|||||||
import com.simplemobiletools.camera.Preview.PreviewListener
|
import com.simplemobiletools.camera.Preview.PreviewListener
|
||||||
import com.simplemobiletools.camera.extensions.config
|
import com.simplemobiletools.camera.extensions.config
|
||||||
import com.simplemobiletools.camera.views.FocusRectView
|
import com.simplemobiletools.camera.views.FocusRectView
|
||||||
import com.simplemobiletools.commons.extensions.checkWhatsNew
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
|
|
||||||
import com.simplemobiletools.commons.extensions.storeStoragePaths
|
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
|
||||||
import com.simplemobiletools.commons.models.Release
|
import com.simplemobiletools.commons.models.Release
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -99,7 +96,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
handleIntent()
|
handleIntent()
|
||||||
} else {
|
} else {
|
||||||
val permissions = ArrayList<String>(2)
|
val permissions = ArrayList<String>(2)
|
||||||
if (!Utils.hasCameraPermission(applicationContext)) {
|
if (!hasCameraPermission()) {
|
||||||
permissions.add(Manifest.permission.CAMERA)
|
permissions.add(Manifest.permission.CAMERA)
|
||||||
}
|
}
|
||||||
if (!hasWriteStoragePermission()) {
|
if (!hasWriteStoragePermission()) {
|
||||||
@ -110,8 +107,8 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun handleIntent() {
|
private fun handleIntent() {
|
||||||
if (intent != null && intent.action != null) {
|
if (intent?.action != null) {
|
||||||
if (intent.extras != null && intent.action == MediaStore.ACTION_IMAGE_CAPTURE || intent.action == MediaStore.ACTION_IMAGE_CAPTURE_SECURE) {
|
if (intent.action == MediaStore.ACTION_IMAGE_CAPTURE || intent.action == MediaStore.ACTION_IMAGE_CAPTURE_SECURE) {
|
||||||
mIsImageCaptureIntent = true
|
mIsImageCaptureIntent = true
|
||||||
hideToggleModeAbout()
|
hideToggleModeAbout()
|
||||||
val output = intent.extras.get(MediaStore.EXTRA_OUTPUT)
|
val output = intent.extras.get(MediaStore.EXTRA_OUTPUT)
|
||||||
@ -130,9 +127,9 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
initButtons()
|
initButtons()
|
||||||
|
|
||||||
if (Utils.hasNavBar(mRes) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
if (mRes.hasNavBar() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||||
val lp = btn_holder.layoutParams as RelativeLayout.LayoutParams
|
val lp = btn_holder.layoutParams as RelativeLayout.LayoutParams
|
||||||
lp.setMargins(0, 0, 0, lp.bottomMargin + Utils.getNavBarHeight(mRes))
|
lp.setMargins(0, 0, 0, lp.bottomMargin + mRes.getNavBarHeight())
|
||||||
}
|
}
|
||||||
|
|
||||||
mCurrCamera = config.lastUsedCamera
|
mCurrCamera = config.lastUsedCamera
|
||||||
@ -161,7 +158,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
toggle_photo_video.setOnClickListener { handleTogglePhotoVideo() }
|
toggle_photo_video.setOnClickListener { handleTogglePhotoVideo() }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasCameraAndStoragePermission() = Utils.hasCameraPermission(applicationContext) && hasWriteStoragePermission()
|
private fun hasCameraAndStoragePermission() = hasCameraPermission() && hasWriteStoragePermission()
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||||
@ -176,7 +173,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
} else if (requestCode == AUDIO_PERMISSION) {
|
} else if (requestCode == AUDIO_PERMISSION) {
|
||||||
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
togglePhotoVideo()
|
togglePhotoVideo()
|
||||||
} else {
|
} else {
|
||||||
toast(R.string.no_audio_permissions)
|
toast(R.string.no_audio_permissions)
|
||||||
@ -311,7 +308,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Utils.hasAudioPermission(applicationContext)) {
|
if (!hasRecordAudioPermission()) {
|
||||||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), AUDIO_PERMISSION)
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), AUDIO_PERMISSION)
|
||||||
mIsAskingPermissions = true
|
mIsAskingPermissions = true
|
||||||
return
|
return
|
||||||
@ -414,7 +411,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun hideTimer() {
|
private fun hideTimer() {
|
||||||
video_rec_curr_timer.text = Utils.formatSeconds(0)
|
video_rec_curr_timer.text = 0.getFormattedDuration()
|
||||||
video_rec_curr_timer.visibility = View.GONE
|
video_rec_curr_timer.visibility = View.GONE
|
||||||
mCurrVideoRecTimer = 0
|
mCurrVideoRecTimer = 0
|
||||||
mTimerHandler.removeCallbacksAndMessages(null)
|
mTimerHandler.removeCallbacksAndMessages(null)
|
||||||
@ -428,7 +425,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
|
|||||||
private fun setupTimer() {
|
private fun setupTimer() {
|
||||||
runOnUiThread(object : Runnable {
|
runOnUiThread(object : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
video_rec_curr_timer.text = Utils.formatSeconds(mCurrVideoRecTimer++)
|
video_rec_curr_timer.text = mCurrVideoRecTimer++.getFormattedDuration()
|
||||||
mTimerHandler.postDelayed(this, 1000)
|
mTimerHandler.postDelayed(this, 1000)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.simplemobiletools.camera.extensions
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.hardware.Camera
|
||||||
|
import android.view.Surface
|
||||||
|
|
||||||
|
fun Activity.getPreviewRotation(cameraId: Int): Int {
|
||||||
|
val info = getCameraInfo(cameraId)
|
||||||
|
val degrees = getRotationDegrees(this)
|
||||||
|
|
||||||
|
var result: Int
|
||||||
|
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
||||||
|
result = (info.orientation + degrees) % 360
|
||||||
|
result = 360 - result
|
||||||
|
} else {
|
||||||
|
result = info.orientation - degrees + 360
|
||||||
|
}
|
||||||
|
|
||||||
|
return result % 360
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Activity.getMediaRotation(cameraId: Int): Int {
|
||||||
|
val degrees = getRotationDegrees(this)
|
||||||
|
val info = getCameraInfo(cameraId)
|
||||||
|
return if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
||||||
|
(360 + info.orientation + degrees) % 360
|
||||||
|
} else
|
||||||
|
(360 + info.orientation - degrees) % 360
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Activity.getFinalRotation(currCameraId: Int, deviceOrientation: Int): Int {
|
||||||
|
var rotation = getMediaRotation(currCameraId)
|
||||||
|
rotation += deviceOrientation.compensateDeviceRotation(currCameraId)
|
||||||
|
return rotation % 360
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getRotationDegrees(activity: Activity) = when (activity.windowManager.defaultDisplay.rotation) {
|
||||||
|
Surface.ROTATION_90 -> 90
|
||||||
|
Surface.ROTATION_180 -> 180
|
||||||
|
Surface.ROTATION_270 -> 270
|
||||||
|
else -> 0
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getCameraInfo(cameraId: Int): Camera.CameraInfo {
|
||||||
|
val info = android.hardware.Camera.CameraInfo()
|
||||||
|
Camera.getCameraInfo(cameraId, info)
|
||||||
|
return info
|
||||||
|
}
|
@ -2,5 +2,25 @@ package com.simplemobiletools.camera.extensions
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.simplemobiletools.camera.Config
|
import com.simplemobiletools.camera.Config
|
||||||
|
import java.io.File
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
val Context.config: Config get() = Config.newInstance(this)
|
val Context.config: Config get() = Config.newInstance(this)
|
||||||
|
|
||||||
|
fun Context.getOutputMediaFile(isPhoto: Boolean): String {
|
||||||
|
val mediaStorageDir = File(config.savePhotosFolder)
|
||||||
|
|
||||||
|
if (!mediaStorageDir.exists()) {
|
||||||
|
if (!mediaStorageDir.mkdirs()) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
||||||
|
return if (isPhoto) {
|
||||||
|
"${mediaStorageDir.path}${File.separator}IMG_$timestamp.jpg"
|
||||||
|
} else {
|
||||||
|
"${mediaStorageDir.path}${File.separator}VID_$timestamp.mp4"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.simplemobiletools.camera.extensions
|
||||||
|
|
||||||
|
import android.hardware.Camera
|
||||||
|
import com.simplemobiletools.camera.Constants
|
||||||
|
|
||||||
|
fun Int.compensateDeviceRotation(currCameraId: Int): Int {
|
||||||
|
var degrees = 0
|
||||||
|
val isFrontCamera = currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT
|
||||||
|
if (this == Constants.ORIENT_LANDSCAPE_LEFT) {
|
||||||
|
degrees += if (isFrontCamera) 90 else 270
|
||||||
|
} else if (this == Constants.ORIENT_LANDSCAPE_RIGHT) {
|
||||||
|
degrees += if (isFrontCamera) 270 else 90
|
||||||
|
}
|
||||||
|
return degrees
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user