-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #817 from minvws/allow-user-focus-camera
Allow user focus camera
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
qrscanner/src/main/java/nl/rijksoverheid/ctr/qrscanner/PreviewExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package nl.rijksoverheid.ctr.qrscanner | ||
|
||
import android.annotation.SuppressLint | ||
import android.view.MotionEvent | ||
import android.view.ViewTreeObserver | ||
import androidx.camera.core.* | ||
import androidx.camera.view.PreviewView | ||
import timber.log.Timber | ||
|
||
fun PreviewView.focusOnTouch(cameraControl: CameraControl) { | ||
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { | ||
@SuppressLint("ClickableViewAccessibility") | ||
override fun onGlobalLayout() { | ||
if (measuredWidth > 0 && measuredHeight > 0) { | ||
viewTreeObserver.removeOnGlobalLayoutListener(this) | ||
|
||
setOnTouchListener { _, event -> | ||
return@setOnTouchListener when (event.action) { | ||
MotionEvent.ACTION_DOWN -> { | ||
true | ||
} | ||
MotionEvent.ACTION_UP -> { | ||
val factory: MeteringPointFactory = SurfaceOrientedMeteringPointFactory( | ||
width.toFloat(), height.toFloat() | ||
) | ||
val autoFocusPoint = factory.createPoint(event.x, event.y) | ||
try { | ||
cameraControl.startFocusAndMetering( | ||
FocusMeteringAction.Builder( | ||
autoFocusPoint, | ||
FocusMeteringAction.FLAG_AF | ||
).apply { | ||
// focus only when the user taps the preview | ||
disableAutoCancel() | ||
}.build() | ||
) | ||
} catch (e: CameraInfoUnavailableException) { | ||
Timber.d("ERROR", "cannot access camera", e) | ||
} | ||
true | ||
} | ||
else -> false // Unhandled event. | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters