mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-06-27 09:02:59 +02:00
handle toggling between photo and video modes
This commit is contained in:
@ -110,7 +110,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener {
|
||||
}
|
||||
|
||||
private fun initVariables() {
|
||||
mIsInPhotoMode = false
|
||||
mIsInPhotoMode = config.initPhotoMode
|
||||
mIsCameraAvailable = false
|
||||
mIsVideoCaptureIntent = false
|
||||
mIsHardwareShutterHandled = false
|
||||
@ -202,7 +202,6 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener {
|
||||
mFocusCircleView = FocusCircleView(applicationContext)
|
||||
view_holder.addView(mFocusCircleView)
|
||||
|
||||
mIsInPhotoMode = true
|
||||
mTimerHandler = Handler()
|
||||
mFadeHandler = Handler()
|
||||
setupPreviewImage(true)
|
||||
@ -325,6 +324,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener {
|
||||
mPreview?.setFlashlightState(FLASH_OFF)
|
||||
hideTimer()
|
||||
mIsInPhotoMode = !mIsInPhotoMode
|
||||
config.initPhotoMode = mIsInPhotoMode
|
||||
showToggleCameraIfNeeded()
|
||||
checkButtons()
|
||||
toggleBottomButtons(false)
|
||||
|
@ -50,6 +50,10 @@ class Config(context: Context) : BaseConfig(context) {
|
||||
get() = prefs.getString(LAST_USED_CAMERA, context.getMyCamera().getBackCameraId().toString())
|
||||
set(cameraId) = prefs.edit().putString(LAST_USED_CAMERA, cameraId).apply()
|
||||
|
||||
var initPhotoMode: Boolean
|
||||
get() = prefs.getBoolean(INIT_PHOTO_MODE, false)
|
||||
set(initPhotoMode) = prefs.edit().putBoolean(INIT_PHOTO_MODE, initPhotoMode).apply()
|
||||
|
||||
var flashlightState: Int
|
||||
get() = prefs.getInt(FLASHLIGHT_STATE, FLASH_OFF)
|
||||
set(state) = prefs.edit().putInt(FLASHLIGHT_STATE, state).apply()
|
||||
|
@ -15,6 +15,7 @@ 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_2"
|
||||
const val FLASHLIGHT_STATE = "flashlight_state"
|
||||
const val INIT_PHOTO_MODE = "init_photo_mode"
|
||||
const val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index_2"
|
||||
const val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index_2"
|
||||
const val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index_2"
|
||||
|
@ -84,6 +84,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
mActivity = activity
|
||||
mTextureView = textureView
|
||||
mUseFrontCamera = activity.config.lastUsedCamera == activity.getMyCamera().getBackCameraId().toString()
|
||||
mIsInVideoMode = !activity.config.initPhotoMode
|
||||
|
||||
mTextureView.setOnTouchListener { view, event ->
|
||||
if (event.action == MotionEvent.ACTION_DOWN) {
|
||||
@ -227,8 +228,27 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
|
||||
private fun getCurrentResolution(): MySize {
|
||||
val configMap = getCameraCharacteristics().get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
|
||||
val resIndex = if (mUseFrontCamera) mActivity.config.frontPhotoResIndex else mActivity.config.backPhotoResIndex
|
||||
val size = configMap.getOutputSizes(ImageFormat.JPEG).sortedByDescending { it.width * it.height }[resIndex]
|
||||
val resIndex = if (mUseFrontCamera) {
|
||||
if (mIsInVideoMode) {
|
||||
mActivity.config.frontVideoResIndex
|
||||
} else {
|
||||
mActivity.config.frontPhotoResIndex
|
||||
}
|
||||
} else {
|
||||
if (mIsInVideoMode) {
|
||||
mActivity.config.backVideoResIndex
|
||||
} else {
|
||||
mActivity.config.backPhotoResIndex
|
||||
}
|
||||
}
|
||||
|
||||
val outputSizes = if (mIsInVideoMode) {
|
||||
configMap.getOutputSizes(MediaRecorder::class.java)
|
||||
} else {
|
||||
configMap.getOutputSizes(ImageFormat.JPEG)
|
||||
}
|
||||
|
||||
val size = outputSizes.sortedByDescending { it.width * it.height }[resIndex]
|
||||
return MySize(size.width, size.height)
|
||||
}
|
||||
|
||||
@ -248,8 +268,12 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
val configMap = getCameraCharacteristics().get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
|
||||
val currentResolution = getCurrentResolution()
|
||||
|
||||
if (mIsInVideoMode) {
|
||||
mImageReader = null
|
||||
} else {
|
||||
mImageReader = ImageReader.newInstance(currentResolution.width, currentResolution.height, ImageFormat.JPEG, 2)
|
||||
mImageReader!!.setOnImageAvailableListener(imageAvailableListener, mBackgroundHandler)
|
||||
}
|
||||
|
||||
val displaySize = getRealDisplaySize()
|
||||
var maxPreviewWidth = displaySize.width
|
||||
@ -275,7 +299,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
maxPreviewHeight = MAX_PREVIEW_HEIGHT
|
||||
}
|
||||
|
||||
val outputSizes = configMap.getOutputSizes(SurfaceTexture::class.java)
|
||||
val outputSizes = if (mIsInVideoMode) configMap.getOutputSizes(MediaRecorder::class.java) else configMap.getOutputSizes(SurfaceTexture::class.java)
|
||||
mPreviewSize = chooseOptimalSize(outputSizes, rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, currentResolution)
|
||||
|
||||
mTextureView.setAspectRatio(mPreviewSize!!.height, mPreviewSize!!.width)
|
||||
@ -354,8 +378,12 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
mCaptureSession = cameraCaptureSession
|
||||
try {
|
||||
if (mIsInVideoMode) {
|
||||
mPreviewRequestBuilder!!.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO)
|
||||
mCaptureSession!!.setRepeatingRequest(mPreviewRequestBuilder!!.build(), null, mBackgroundHandler)
|
||||
} else {
|
||||
mPreviewRequestBuilder!!.apply {
|
||||
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)
|
||||
setFlashAndExposure(this)
|
||||
@ -363,6 +391,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
}
|
||||
mCaptureSession!!.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler)
|
||||
mCameraState = STATE_PREVIEW
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
@ -378,7 +407,11 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
mPreviewRequestBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
|
||||
mPreviewRequestBuilder!!.addTarget(surface)
|
||||
|
||||
mCameraDevice?.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null)
|
||||
if (mIsInVideoMode) {
|
||||
mCameraDevice!!.createCaptureSession(Arrays.asList(surface), stateCallback, mBackgroundHandler)
|
||||
} else {
|
||||
mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
@ -669,22 +702,27 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||
}
|
||||
|
||||
override fun tryInitVideoMode() {
|
||||
initVideoMode()
|
||||
}
|
||||
|
||||
override fun initPhotoMode() {
|
||||
mIsInVideoMode = false
|
||||
closeCamera()
|
||||
openCamera(mTextureView.width, mTextureView.height)
|
||||
}
|
||||
|
||||
override fun initVideoMode(): Boolean {
|
||||
mIsInVideoMode = true
|
||||
return false
|
||||
closeCamera()
|
||||
openCamera(mTextureView.width, mTextureView.height)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun checkFlashlight() {
|
||||
if (mCameraState == STATE_PREVIEW && mIsFlashSupported) {
|
||||
setFlashAndExposure(mPreviewRequestBuilder!!)
|
||||
mPreviewRequest = mPreviewRequestBuilder!!.build()
|
||||
mCaptureSession!!.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler)
|
||||
mCaptureSession?.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler)
|
||||
mActivity.updateFlashlightState(mFlashlightState)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user