diff --git a/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt b/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt index 850e561bf..ce741b52f 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/sources/video/Camera2Source.kt @@ -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 @@ -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 @@ -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() + } } \ No newline at end of file diff --git a/encoder/src/main/java/com/pedro/encoder/input/video/Camera2ApiManager.kt b/encoder/src/main/java/com/pedro/encoder/input/video/Camera2ApiManager.kt index bac783ba7..811e23a8c 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/video/Camera2ApiManager.kt +++ b/encoder/src/main/java/com/pedro/encoder/input/video/Camera2ApiManager.kt @@ -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) @@ -778,11 +777,11 @@ 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() { @@ -790,11 +789,11 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() { 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) {