Skip to content

Commit

Permalink
camera2 remove unused code and add fews improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 29, 2024
1 parent 2831a88 commit c1fdfc0
Showing 1 changed file with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
private val TAG = "Camera2ApiManager"

private var cameraDevice: CameraDevice? = null
private var surfaceEncoder: Surface? = null //input surfaceEncoder from videoEncoder
private var surfaceEncoder = Surface(SurfaceTexture(-1).apply { release() }) //input surfaceEncoder from videoEncoder
private val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
private var cameraHandler: Handler? = null
private var cameraCaptureSession: CameraCaptureSession? = null
var isPrepared: Boolean = false
private set
private var isPrepared: Boolean = false
private var cameraId: String = "0"
private var facing = Facing.BACK
private var builderInputSurface: CaptureRequest.Builder? = null
Expand Down Expand Up @@ -112,12 +111,6 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
cameraId = try { getCameraIdForFacing(Facing.BACK) } catch (e: Exception) { "0" }
}

fun prepareCamera(surface: Surface?, fps: Int) {
this.surfaceEncoder = surface
this.fps = fps
isPrepared = true
}

fun prepareCamera(surfaceTexture: SurfaceTexture, width: Int, height: Int, fps: Int) {
val optimalResolution = getOptimalResolution(Size(width, height), getCameraResolutions(facing))
Log.i(TAG, "optimal resolution set to: " + optimalResolution.width + "x" + optimalResolution.height)
Expand All @@ -133,14 +126,21 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
}

fun prepareCamera(surfaceTexture: SurfaceTexture, width: Int, height: Int, fps: Int, cameraId: String) {
this.cameraId = cameraId
this.facing = getFacingByCameraId(cameraManager, cameraId)
prepareCamera(surfaceTexture, width, height, fps)
}

private fun prepareCamera(surface: Surface, fps: Int) {
this.surfaceEncoder = surface
this.fps = fps
isPrepared = true
}

private fun startPreview(cameraDevice: CameraDevice) {
try {
val listSurfaces = mutableListOf<Surface>()
surfaceEncoder?.let { listSurfaces.add(it) }
listSurfaces.add(surfaceEncoder)
imageReader?.let { listSurfaces.add(it.surface) }
val captureRequest = drawSurface(cameraDevice, listSurfaces)
createCaptureSession(
Expand Down Expand Up @@ -210,10 +210,6 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
return characteristics.secureGet(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) ?: -1
}

fun openCamera() {
openCameraBack()
}

fun openCameraBack() {
openCameraFacing(Facing.BACK)
}
Expand Down Expand Up @@ -628,7 +624,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
Log.e(TAG, "Error", e)
}
} else {
Log.e(TAG, "Camera2ApiManager need be prepared, Camera2ApiManager not enabled")
throw IllegalStateException("You need prepare camera before open it")
}
}

Expand Down Expand Up @@ -735,16 +731,6 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
fingerSpacing = currentFingerSpacing
}

fun stopRepeatingEncoder() {
val cameraCaptureSession = this.cameraCaptureSession ?: return
try {
cameraCaptureSession.stopRepeating()
surfaceEncoder = null
} catch (e: Exception) {
Log.e(TAG, "Error", e)
}
}

@JvmOverloads
fun closeCamera(resetSurface: Boolean = true) {
isLanternEnabled = false
Expand All @@ -756,7 +742,7 @@ class Camera2ApiManager(context: Context) : CameraDevice.StateCallback() {
cameraHandler?.looper?.quitSafely()
cameraHandler = null
if (resetSurface) {
surfaceEncoder = null
surfaceEncoder = Surface(SurfaceTexture(-1).apply { release() })
builderInputSurface = null
}
isPrepared = false
Expand Down

0 comments on commit c1fdfc0

Please sign in to comment.