adding some touch to focus handling
This commit is contained in:
parent
328c4300a5
commit
5c6d4132cb
|
@ -7,6 +7,7 @@ import android.graphics.ImageFormat
|
||||||
import android.graphics.Point
|
import android.graphics.Point
|
||||||
import android.graphics.SurfaceTexture
|
import android.graphics.SurfaceTexture
|
||||||
import android.hardware.camera2.*
|
import android.hardware.camera2.*
|
||||||
|
import android.hardware.camera2.params.MeteringRectangle
|
||||||
import android.media.ImageReader
|
import android.media.ImageReader
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
@ -24,9 +25,11 @@ import java.util.*
|
||||||
import java.util.concurrent.Semaphore
|
import java.util.concurrent.Semaphore
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
|
||||||
// based on the Android Camera2 sample at https://github.com/googlesamples/android-Camera2Basic
|
// based on the Android Camera2 sample at https://github.com/googlesamples/android-Camera2Basic
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPreview {
|
class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPreview {
|
||||||
|
private val FOCUS_TAG = "focus_tag"
|
||||||
private val MAX_PREVIEW_WIDTH = 1920
|
private val MAX_PREVIEW_WIDTH = 1920
|
||||||
private val MAX_PREVIEW_HEIGHT = 1080
|
private val MAX_PREVIEW_HEIGHT = 1080
|
||||||
|
|
||||||
|
@ -265,8 +268,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
mPreviewRequestBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
|
mPreviewRequestBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
|
||||||
mPreviewRequestBuilder!!.addTarget(surface)
|
mPreviewRequestBuilder!!.addTarget(surface)
|
||||||
|
|
||||||
mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface),
|
val stateCallback = object : CameraCaptureSession.StateCallback() {
|
||||||
object : CameraCaptureSession.StateCallback() {
|
|
||||||
override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
|
override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
|
||||||
if (mCameraDevice == null) {
|
if (mCameraDevice == null) {
|
||||||
return
|
return
|
||||||
|
@ -283,10 +285,10 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onConfigureFailed(cameraCaptureSession: CameraCaptureSession) {
|
override fun onConfigureFailed(cameraCaptureSession: CameraCaptureSession) {}
|
||||||
}
|
}
|
||||||
}, null
|
|
||||||
)
|
mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null)
|
||||||
} catch (e: CameraAccessException) {
|
} catch (e: CameraAccessException) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -354,7 +356,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
set(CaptureRequest.JPEG_ORIENTATION, mSensorOrientation)
|
set(CaptureRequest.JPEG_ORIENTATION, mSensorOrientation)
|
||||||
}
|
}
|
||||||
|
|
||||||
val CaptureCallback = object : CameraCaptureSession.CaptureCallback() {
|
val captureCallback = object : CameraCaptureSession.CaptureCallback() {
|
||||||
override fun onCaptureCompleted(session: CameraCaptureSession, request: CaptureRequest, result: TotalCaptureResult) {
|
override fun onCaptureCompleted(session: CameraCaptureSession, request: CaptureRequest, result: TotalCaptureResult) {
|
||||||
unlockFocus()
|
unlockFocus()
|
||||||
mActivity.toggleBottomButtons(false)
|
mActivity.toggleBottomButtons(false)
|
||||||
|
@ -369,14 +371,57 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie
|
||||||
mCaptureSession!!.apply {
|
mCaptureSession!!.apply {
|
||||||
stopRepeating()
|
stopRepeating()
|
||||||
abortCaptures()
|
abortCaptures()
|
||||||
capture(captureBuilder.build(), CaptureCallback, null)
|
capture(captureBuilder.build(), captureCallback, null)
|
||||||
}
|
}
|
||||||
} catch (e: CameraAccessException) {
|
} catch (e: CameraAccessException) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// inspired by https://gist.github.com/royshil/8c760c2485257c85a11cafd958548482
|
||||||
private fun focusArea() {
|
private fun focusArea() {
|
||||||
mActivity.drawFocusCircle(mLastClickX, mLastClickY)
|
mActivity.drawFocusCircle(mLastClickX, mLastClickY)
|
||||||
|
|
||||||
|
val manager = mActivity.getSystemService(Context.CAMERA_SERVICE) as CameraManager
|
||||||
|
val characteristics = manager.getCameraCharacteristics(mCameraId)
|
||||||
|
val sensorArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)
|
||||||
|
|
||||||
|
val x = (mLastClickX / mTextureView.width) * sensorArraySize.height()
|
||||||
|
val y = (mLastClickY / mTextureView.height) * sensorArraySize.width()
|
||||||
|
val halfTouchWidth = 150
|
||||||
|
val halfTouchHeight = 150
|
||||||
|
val focusAreaTouch = MeteringRectangle(Math.max(x - halfTouchWidth, 0f).toInt(),
|
||||||
|
Math.max(y - halfTouchHeight, 0f).toInt(),
|
||||||
|
halfTouchWidth * 2,
|
||||||
|
halfTouchHeight * 2,
|
||||||
|
MeteringRectangle.METERING_WEIGHT_MAX - 1)
|
||||||
|
|
||||||
|
val captureCallbackHandler = object : CameraCaptureSession.CaptureCallback() {
|
||||||
|
override fun onCaptureCompleted(session: CameraCaptureSession, request: CaptureRequest, result: TotalCaptureResult) {
|
||||||
|
super.onCaptureCompleted(session, request, result)
|
||||||
|
|
||||||
|
if (request.tag === FOCUS_TAG) {
|
||||||
|
mPreviewRequestBuilder!!.set(CaptureRequest.CONTROL_AF_TRIGGER, null)
|
||||||
|
mCaptureSession!!.setRepeatingRequest(mPreviewRequestBuilder!!.build(), null, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mCaptureSession!!.stopRepeating()
|
||||||
|
mPreviewRequestBuilder!!.apply {
|
||||||
|
set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL)
|
||||||
|
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF)
|
||||||
|
mCaptureSession!!.capture(build(), captureCallbackHandler, mBackgroundHandler)
|
||||||
|
|
||||||
|
if (characteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF) >= 1) {
|
||||||
|
set(CaptureRequest.CONTROL_AF_REGIONS, arrayOf(focusAreaTouch))
|
||||||
|
}
|
||||||
|
|
||||||
|
set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO)
|
||||||
|
set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO)
|
||||||
|
set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START)
|
||||||
|
setTag(FOCUS_TAG)
|
||||||
|
mCaptureSession!!.capture(build(), captureCallbackHandler, mBackgroundHandler)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun lockFocus() {
|
private fun lockFocus() {
|
||||||
|
|
Loading…
Reference in New Issue