Fix flash and timer for video recording

This commit is contained in:
Matéo Duparc 2021-12-20 15:30:51 +01:00
parent 8b4adfbe21
commit b65ee230be
Signed by untrusted user: hardcoresushi
GPG Key ID: 007F84120107191E

View File

@ -84,6 +84,7 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
private var isBackCamera = true private var isBackCamera = true
private var isInVideoMode = false private var isInVideoMode = false
private var isRecording = false private var isRecording = false
private var isWaitingForTimer = false
private lateinit var binding: ActivityCameraBinding private lateinit var binding: ActivityCameraBinding
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -185,7 +186,21 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
dialog.show() dialog.show()
} }
binding.imageFlash.setOnClickListener { binding.imageFlash.setOnClickListener {
binding.imageFlash.setImageResource(when (imageCapture?.flashMode) { binding.imageFlash.setImageResource(if (isInVideoMode) {
when (imageCapture?.flashMode) {
ImageCapture.FLASH_MODE_ON -> {
camera?.cameraControl?.enableTorch(false)
imageCapture?.flashMode = ImageCapture.FLASH_MODE_OFF
R.drawable.icon_flash_off
}
else -> {
camera?.cameraControl?.enableTorch(true)
imageCapture?.flashMode = ImageCapture.FLASH_MODE_ON
R.drawable.icon_flash_on
}
}
} else {
when (imageCapture?.flashMode) {
ImageCapture.FLASH_MODE_AUTO -> { ImageCapture.FLASH_MODE_AUTO -> {
imageCapture?.flashMode = ImageCapture.FLASH_MODE_ON imageCapture?.flashMode = ImageCapture.FLASH_MODE_ON
R.drawable.icon_flash_on R.drawable.icon_flash_on
@ -198,11 +213,12 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
imageCapture?.flashMode = ImageCapture.FLASH_MODE_AUTO imageCapture?.flashMode = ImageCapture.FLASH_MODE_AUTO
R.drawable.icon_flash_auto R.drawable.icon_flash_auto
} }
}
}) })
} }
binding.imageModeSwitch.setOnClickListener { binding.imageModeSwitch.setOnClickListener {
isInVideoMode = !isInVideoMode isInVideoMode = !isInVideoMode
if (isInVideoMode) { binding.imageFlash.setImageResource(if (isInVideoMode) {
binding.recordVideoButton.visibility = View.VISIBLE binding.recordVideoButton.visibility = View.VISIBLE
binding.takePhotoButton.visibility = View.GONE binding.takePhotoButton.visibility = View.GONE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ -211,10 +227,14 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), AUDIO_PERMISSION_REQUEST_CODE) requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), AUDIO_PERMISSION_REQUEST_CODE)
} }
} }
imageCapture?.flashMode = ImageCapture.FLASH_MODE_OFF
R.drawable.icon_flash_off
} else { } else {
binding.recordVideoButton.visibility = View.GONE binding.recordVideoButton.visibility = View.GONE
binding.takePhotoButton.visibility = View.VISIBLE binding.takePhotoButton.visibility = View.VISIBLE
} imageCapture?.flashMode = ImageCapture.FLASH_MODE_AUTO
R.drawable.icon_flash_auto
})
} }
binding.imageCameraSwitch.setOnClickListener { binding.imageCameraSwitch.setOnClickListener {
isBackCamera = if (isBackCamera) { isBackCamera = if (isBackCamera) {
@ -337,8 +357,40 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
} }
} }
private fun takePhoto(outputPath: String) { private fun getOutputPath(isVideo: Boolean): String {
val imageCapture = imageCapture ?: return val baseName = if (isVideo) {"VID"} else {"IMG"}+'_'+dateFormat.format(Date())+'_'
var fileName: String
do {
fileName = baseName+(random.nextInt(fileNameRandomMax-fileNameRandomMin)+fileNameRandomMin)+'.'+ if (isVideo) {"mp4"} else {"jpg"}
} while (gocryptfsVolume.pathExists(fileName))
return PathUtils.pathJoin(outputDirectory, fileName)
}
private fun startTimerThen(action: () -> Unit) {
if (timerDuration > 0){
binding.textTimer.visibility = View.VISIBLE
isWaitingForTimer = true
Thread{
for (i in timerDuration downTo 1){
runOnUiThread { binding.textTimer.text = i.toString() }
Thread.sleep(1000)
}
runOnUiThread {
action()
binding.textTimer.visibility = View.GONE
}
isWaitingForTimer = false
}.start()
} else {
action()
}
}
private fun onClickTakePhoto() {
if (!isWaitingForTimer) {
val outputPath = getOutputPath(false)
startTimerThen {
imageCapture?.let { imageCapture ->
val outputBuff = ByteArrayOutputStream() val outputBuff = ByteArrayOutputStream()
val outputOptions = ImageCapture.OutputFileOptions.Builder(outputBuff).build() val outputOptions = ImageCapture.OutputFileOptions.Builder(outputBuff).build()
imageCapture.takePicture(outputOptions, executor, object : ImageCapture.OnImageSavedCallback { imageCapture.takePicture(outputOptions, executor, object : ImageCapture.OnImageSavedCallback {
@ -364,42 +416,18 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
} }
}) })
} }
private fun getOutputPath(isVideo: Boolean): String {
val baseName = if (isVideo) {"VID"} else {"IMG"}+'_'+dateFormat.format(Date())+'_'
var fileName: String
do {
fileName = baseName+(random.nextInt(fileNameRandomMax-fileNameRandomMin)+fileNameRandomMin)+'.'+ if (isVideo) {"mp4"} else {"jpg"}
} while (gocryptfsVolume.pathExists(fileName))
return PathUtils.pathJoin(outputDirectory, fileName)
} }
private fun onClickTakePhoto() {
val path = getOutputPath(false)
if (timerDuration > 0){
binding.textTimer.visibility = View.VISIBLE
Thread{
for (i in timerDuration downTo 1){
runOnUiThread { binding.textTimer.text = i.toString() }
Thread.sleep(1000)
}
runOnUiThread {
takePhoto(path)
binding.textTimer.visibility = View.GONE
}
}.start()
} else {
takePhoto(path)
} }
} }
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private fun onClickRecordVideo() { private fun onClickRecordVideo() {
isRecording = if (isRecording) { if (isRecording) {
videoCapture?.stopRecording() videoCapture?.stopRecording()
false isRecording = false
} else { } else if (!isWaitingForTimer) {
val path = getOutputPath(true) val path = getOutputPath(true)
startTimerThen {
val handleId = gocryptfsVolume.openWriteMode(path) val handleId = gocryptfsVolume.openWriteMode(path)
videoCapture?.startRecording(VideoCapture.OutputFileOptions(object : SeekableWriter { videoCapture?.startRecording(VideoCapture.OutputFileOptions(object : SeekableWriter {
var offset = 0L var offset = 0L
@ -424,7 +452,8 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
} }
}) })
binding.recordVideoButton.setImageResource(R.drawable.stop_recording_video_button) binding.recordVideoButton.setImageResource(R.drawable.stop_recording_video_button)
true isRecording = true
}
} }
} }