add basic support for focus and metering

This commit is contained in:
darthpaul 2022-06-25 17:51:13 +01:00
parent 1f3dd341d0
commit f0030670cf
3 changed files with 48 additions and 7 deletions

View File

@ -568,6 +568,10 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
video_rec_curr_timer.text = seconds.getFormattedDuration() video_rec_curr_timer.text = seconds.getFormattedDuration()
} }
override fun onFocusCamera(xPos: Float, yPos: Float) {
mFocusCircleView.drawFocusCircle(xPos, yPos)
}
fun setRecordingState(isRecording: Boolean) { fun setRecordingState(isRecording: Boolean) {
runOnUiThread { runOnUiThread {
if (isRecording) { if (isRecording) {

View File

@ -7,6 +7,9 @@ import android.net.Uri
import android.os.Environment import android.os.Environment
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Log import android.util.Log
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.OrientationEventListener import android.view.OrientationEventListener
import android.view.Surface import android.view.Surface
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -152,6 +155,7 @@ class CameraXPreview(
captureUseCase, captureUseCase,
) )
preview?.setSurfaceProvider(viewFinder.surfaceProvider) preview?.setSurfaceProvider(viewFinder.surfaceProvider)
setupFocus()
} }
private fun setupCameraObservers() { private fun setupCameraObservers() {
@ -261,6 +265,38 @@ class CameraXPreview(
return AspectRatio.RATIO_16_9 return AspectRatio.RATIO_16_9
} }
@SuppressLint("ClickableViewAccessibility")
// source: https://stackoverflow.com/a/60095886/10552591
private fun setupFocus() {
val gestureDetector = GestureDetector(activity, object : SimpleOnGestureListener() {
override fun onSingleTapConfirmed(event: MotionEvent?): Boolean {
Log.i(TAG, "onSingleTapConfirmed: x=${event?.x}, y=${event?.y}")
return event?.let {
val factory: MeteringPointFactory = SurfaceOrientedMeteringPointFactory(viewFinder.width.toFloat(), viewFinder.height.toFloat())
val xPos = event.x
val yPos = event.y
val autoFocusPoint = factory.createPoint(event.x, event.y)
try {
val focusMeteringAction = FocusMeteringAction.Builder(autoFocusPoint, FocusMeteringAction.FLAG_AF)
.disableAutoCancel()
.build()
camera?.cameraControl?.startFocusAndMetering(focusMeteringAction)
listener.onFocusCamera(xPos, yPos)
Log.i(TAG, "start focus")
} catch (e: CameraInfoUnavailableException) {
Log.e(TAG, "cannot access camera", e)
}
true
} ?: false
}
})
viewFinder.setOnTouchListener { _, event ->
Log.i(TAG, "setOnTouchListener: x=${event.x}, y=${event.y}")
gestureDetector.onTouchEvent(event)
true
}
}
override fun onStart(owner: LifecycleOwner) { override fun onStart(owner: LifecycleOwner) {
orientationEventListener.enable() orientationEventListener.enable()
} }
@ -375,7 +411,7 @@ class CameraXPreview(
.start(mainExecutor) { recordEvent -> .start(mainExecutor) { recordEvent ->
Log.d(TAG, "recordEvent=$recordEvent ") Log.d(TAG, "recordEvent=$recordEvent ")
recordingState = recordEvent recordingState = recordEvent
when(recordEvent){ when (recordEvent) {
is VideoRecordEvent.Start -> { is VideoRecordEvent.Start -> {
playStartVideoRecordingSoundIfEnabled() playStartVideoRecordingSoundIfEnabled()
listener.onVideoRecordingStarted() listener.onVideoRecordingStarted()
@ -408,20 +444,20 @@ class CameraXPreview(
} }
} }
private fun playShutterSoundIfEnabled(){ private fun playShutterSoundIfEnabled() {
if(config.isSoundEnabled){ if (config.isSoundEnabled) {
mediaSoundHelper.playShutterSound() mediaSoundHelper.playShutterSound()
} }
} }
private fun playStartVideoRecordingSoundIfEnabled(){ private fun playStartVideoRecordingSoundIfEnabled() {
if(config.isSoundEnabled){ if (config.isSoundEnabled) {
mediaSoundHelper.playStartVideoRecordingSound() mediaSoundHelper.playStartVideoRecordingSound()
} }
} }
private fun playStopVideoRecordingSoundIfEnabled(){ private fun playStopVideoRecordingSoundIfEnabled() {
if(config.isSoundEnabled){ if (config.isSoundEnabled) {
mediaSoundHelper.playStopVideoRecordingSound() mediaSoundHelper.playStopVideoRecordingSound()
} }
} }

View File

@ -13,4 +13,5 @@ interface CameraXPreviewListener {
fun onVideoRecordingStarted() fun onVideoRecordingStarted()
fun onVideoRecordingStopped() fun onVideoRecordingStopped()
fun onVideoDurationChanged(durationNanos: Long) fun onVideoDurationChanged(durationNanos: Long)
fun onFocusCamera(xPos: Float, yPos: Float)
} }