Skip to content

Commit

Permalink
fix camera2 add and remove ImageListener
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 29, 2024
1 parent b51a9aa commit 2831a88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.pedro.encoder.input.sources.video

import android.content.Context
import android.graphics.ImageFormat
import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.os.Build
Expand All @@ -25,6 +26,7 @@ import android.util.Size
import android.view.MotionEvent
import androidx.annotation.RequiresApi
import com.pedro.encoder.input.video.Camera2ApiManager
import com.pedro.encoder.input.video.Camera2ApiManager.ImageCallback
import com.pedro.encoder.input.video.CameraHelper
import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback

Expand Down Expand Up @@ -207,4 +209,15 @@ class Camera2Source(context: Context): VideoSource() {
}

fun isAutoExposureEnabled() = camera.isAutoExposureEnabled

@JvmOverloads
fun addImageListener(format: Int, maxImages: Int, autoClose: Boolean = true, listener: ImageCallback) {
val w = if (rotation == 90 || rotation == 270) height else width
val h = if (rotation == 90 || rotation == 270) width else height
camera.addImageListener(w, h, format, maxImages, autoClose, listener)
}

fun removeImageListener() {
camera.removeImageListener()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
fun addImageListener(width: Int, height: Int, format: Int, maxImages: Int, autoClose: Boolean, listener: ImageCallback) {
val wasRunning = isRunning
closeCamera(false)
if (wasRunning) closeCamera(false)
removeImageListener()
this.imageReader?.close()
val imageThread = HandlerThread("$TAG imageThread")
imageThread.start()
val imageReader = ImageReader.newInstance(width, height, format, maxImages)
Expand All @@ -778,23 +777,23 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
if (autoClose) image.close()
}
}, Handler(imageThread.looper))
this.imageReader = imageReader
if (wasRunning) {
prepareCamera(surfaceEncoder, fps)
openLastCamera()
}
this.imageReader = imageReader
}

fun removeImageListener() {
val imageReader = this.imageReader ?: return
val wasRunning = isRunning
if (wasRunning) closeCamera(false)
imageReader.close()
this.imageReader = null
if (wasRunning) {
prepareCamera(surfaceEncoder, fps)
openLastCamera()
}
this.imageReader = null
}

override fun onOpened(cameraDevice: CameraDevice) {
Expand Down

0 comments on commit 2831a88

Please sign in to comment.