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()
}
override fun onFocusCamera(xPos: Float, yPos: Float) {
mFocusCircleView.drawFocusCircle(xPos, yPos)
}
fun setRecordingState(isRecording: Boolean) {
runOnUiThread {
if (isRecording) {

View File

@ -7,6 +7,9 @@ import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.OrientationEventListener
import android.view.Surface
import androidx.appcompat.app.AppCompatActivity
@ -152,6 +155,7 @@ class CameraXPreview(
captureUseCase,
)
preview?.setSurfaceProvider(viewFinder.surfaceProvider)
setupFocus()
}
private fun setupCameraObservers() {
@ -261,6 +265,38 @@ class CameraXPreview(
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) {
orientationEventListener.enable()
}
@ -375,7 +411,7 @@ class CameraXPreview(
.start(mainExecutor) { recordEvent ->
Log.d(TAG, "recordEvent=$recordEvent ")
recordingState = recordEvent
when(recordEvent){
when (recordEvent) {
is VideoRecordEvent.Start -> {
playStartVideoRecordingSoundIfEnabled()
listener.onVideoRecordingStarted()
@ -408,20 +444,20 @@ class CameraXPreview(
}
}
private fun playShutterSoundIfEnabled(){
if(config.isSoundEnabled){
private fun playShutterSoundIfEnabled() {
if (config.isSoundEnabled) {
mediaSoundHelper.playShutterSound()
}
}
private fun playStartVideoRecordingSoundIfEnabled(){
if(config.isSoundEnabled){
private fun playStartVideoRecordingSoundIfEnabled() {
if (config.isSoundEnabled) {
mediaSoundHelper.playStartVideoRecordingSound()
}
}
private fun playStopVideoRecordingSoundIfEnabled(){
if(config.isSoundEnabled){
private fun playStopVideoRecordingSoundIfEnabled() {
if (config.isSoundEnabled) {
mediaSoundHelper.playStopVideoRecordingSound()
}
}

View File

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