Compare commits

..

7 Commits

2 changed files with 83 additions and 47 deletions

View File

@@ -235,10 +235,19 @@ class PersistentCameraCaptureSession(private val cameraManager: CameraManager, p
// 1. Run a precapture sequence for AF, AE and AWB. // 1. Run a precapture sequence for AF, AE and AWB.
focusJob = coroutineScope.launch { focusJob = coroutineScope.launch {
val request = repeatingRequest.createCaptureRequest(device, deviceDetails, outputs) try {
val options = val request = repeatingRequest.createCaptureRequest(device, deviceDetails, outputs)
PrecaptureOptions(listOf(PrecaptureTrigger.AF, PrecaptureTrigger.AE), Flash.OFF, listOf(point), false, FOCUS_RESET_TIMEOUT) val options =
session.precapture(request, deviceDetails, options) PrecaptureOptions(listOf(PrecaptureTrigger.AF, PrecaptureTrigger.AE), Flash.OFF, listOf(point), false, FOCUS_RESET_TIMEOUT)
session.precapture(request, deviceDetails, options)
} catch (e: CaptureTimedOutError) {
// Focus timed out - this is non-fatal, just log and continue
Log.w(TAG, "Focus timed out at point $point, continuing without focus lock")
} catch (e: IllegalStateException) {
Log.w(TAG, "Focus failed, camera device was already closed: ${e.message}")
} catch (e: CameraAccessException) {
Log.w(TAG, "Focus failed, camera not accessible: ${e.message}")
}
} }
focusJob?.join() focusJob?.join()
@@ -254,8 +263,14 @@ class PersistentCameraCaptureSession(private val cameraManager: CameraManager, p
return@launch return@launch
} }
Log.i(TAG, "Resetting focus to auto-focus...") Log.i(TAG, "Resetting focus to auto-focus...")
repeatingRequest.createCaptureRequest(device, deviceDetails, outputs).also { request -> try {
session.setRepeatingRequest(request.build(), null, null) repeatingRequest.createCaptureRequest(device, deviceDetails, outputs).also { request ->
session.setRepeatingRequest(request.build(), null, null)
}
} catch (e: IllegalStateException) {
Log.w(TAG, "Failed to reset focus, camera device was already closed: ${e.message}")
} catch (e: CameraAccessException) {
Log.w(TAG, "Failed to reset focus, camera not accessible: ${e.message}")
} }
} }
} }

View File

@@ -5,7 +5,7 @@ import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Point import android.graphics.Point
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.HandlerThread
import android.util.Log import android.util.Log
import android.util.Size import android.util.Size
import android.view.PixelCopy import android.view.PixelCopy
@@ -25,58 +25,72 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Matrix
fun rotateBitmap90CounterClockwise(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
// Create a new Bitmap with swapped width and height
val rotatedBitmap = Bitmap.createBitmap(height, width, source.config ?: Bitmap.Config.ARGB_8888)
for (y in 0 until height) {
for (x in 0 until width) {
// Set the pixel in the new position
rotatedBitmap.setPixel(y, width - 1 - x, source.getPixel(x, y))
}
}
return rotatedBitmap
}
fun Bitmap.transformBitmap(orientation: Orientation): Bitmap { fun Bitmap.transformBitmap(orientation: Orientation): Bitmap {
return when (orientation) { return when (orientation) {
Orientation.PORTRAIT -> this // No transformation needed Orientation.PORTRAIT -> this // No transformation needed
Orientation.LANDSCAPE_LEFT -> { Orientation.LANDSCAPE_LEFT -> {
// Transpose (swap width and height) val srcWidth = width
val transposedBitmap = Bitmap.createBitmap(height, width, config ?: Bitmap.Config.ARGB_8888) val srcHeight = height
for (y in 0 until height) { val sourcePixels = IntArray(srcWidth * srcHeight)
for (x in 0 until width) { getPixels(sourcePixels, 0, srcWidth, 0, 0, srcWidth, srcHeight)
transposedBitmap.setPixel(y, width - 1 - x, getPixel(x, y))
val dstWidth = srcHeight
val dstHeight = srcWidth
val destinationPixels = IntArray(dstWidth * dstHeight)
for (y in 0 until srcHeight) {
for (x in 0 until srcWidth) {
val dstX = y
val dstY = srcWidth - 1 - x
destinationPixels[dstY * dstWidth + dstX] = sourcePixels[y * srcWidth + x]
} }
} }
transposedBitmap
val transformedBitmap = Bitmap.createBitmap(dstWidth, dstHeight, config ?: Bitmap.Config.ARGB_8888)
transformedBitmap.setPixels(destinationPixels, 0, dstWidth, 0, 0, dstWidth, dstHeight)
transformedBitmap
} }
Orientation.PORTRAIT_UPSIDE_DOWN -> { Orientation.PORTRAIT_UPSIDE_DOWN -> {
// Invert vertically and horizontally (180-degree rotation) val srcWidth = width
val invertedBitmap = Bitmap.createBitmap(width, height, config ?: Bitmap.Config.ARGB_8888) val srcHeight = height
for (y in 0 until height) { val sourcePixels = IntArray(srcWidth * srcHeight)
for (x in 0 until width) { getPixels(sourcePixels, 0, srcWidth, 0, 0, srcWidth, srcHeight)
invertedBitmap.setPixel(width - 1 - x, height - 1 - y, getPixel(x, y))
val dstWidth = srcWidth
val dstHeight = srcHeight
val destinationPixels = IntArray(dstWidth * dstHeight)
for (y in 0 until srcHeight) {
for (x in 0 until srcWidth) {
val dstX = srcWidth - 1 - x
val dstY = srcHeight - 1 - y
destinationPixels[dstY * dstWidth + dstX] = sourcePixels[y * srcWidth + x]
} }
} }
invertedBitmap
val transformedBitmap = Bitmap.createBitmap(dstWidth, dstHeight, config ?: Bitmap.Config.ARGB_8888)
transformedBitmap.setPixels(destinationPixels, 0, dstWidth, 0, 0, dstWidth, dstHeight)
transformedBitmap
} }
Orientation.LANDSCAPE_RIGHT -> { Orientation.LANDSCAPE_RIGHT -> {
// Transpose (swap width and height) and invert vertically val srcWidth = width
val transposedBitmap = Bitmap.createBitmap(height, width, config ?: Bitmap.Config.ARGB_8888) val srcHeight = height
for (y in 0 until height) { val sourcePixels = IntArray(srcWidth * srcHeight)
for (x in 0 until width) { getPixels(sourcePixels, 0, srcWidth, 0, 0, srcWidth, srcHeight)
transposedBitmap.setPixel(height - 1 - y, x, getPixel(x, y))
val dstWidth = srcHeight
val dstHeight = srcWidth
val destinationPixels = IntArray(dstWidth * dstHeight)
for (y in 0 until srcHeight) {
for (x in 0 until srcWidth) {
val dstX = srcHeight - 1 - y
val dstY = x
destinationPixels[dstY * dstWidth + dstX] = sourcePixels[y * srcWidth + x]
} }
} }
transposedBitmap
val transformedBitmap = Bitmap.createBitmap(dstWidth, dstHeight, config ?: Bitmap.Config.ARGB_8888)
transformedBitmap.setPixels(destinationPixels, 0, dstWidth, 0, 0, dstWidth, dstHeight)
transformedBitmap
} }
} }
} }
@@ -186,7 +200,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
) )
} }
}, },
Handler(Looper.getMainLooper()) pixelCopyHandler
) )
} }
} }
@@ -198,8 +212,10 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
val viewOrientation = Orientation.PORTRAIT val viewOrientation = Orientation.PORTRAIT
val rotated = point.rotatedBy(viewSize, cameraSize, viewOrientation, sensorOrientation) val rotated = point.rotatedBy(viewSize, cameraSize, viewOrientation, sensorOrientation)
Log.i(TAG, "Converted layer point $point to camera point $rotated! ($sensorOrientation, $cameraSize -> $viewSize)") // Clamp to valid camera coordinates (must be non-negative for MeteringRectangle)
return rotated val clamped = Point(maxOf(0, rotated.x), maxOf(0, rotated.y))
Log.i(TAG, "Converted layer point $point to camera point $clamped! ($sensorOrientation, $cameraSize -> $viewSize)")
return clamped
} }
private fun updateLayout() { private fun updateLayout() {
@@ -254,5 +270,10 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
companion object { companion object {
private const val TAG = "PreviewView" private const val TAG = "PreviewView"
private val pixelCopyHandler: Handler by lazy {
val handlerThread = HandlerThread("VisionCamera.PixelCopy")
handlerThread.start()
Handler(handlerThread.looper)
}
} }
} }