You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
I'd like to propose an extension to get a camera by direction.
the implementation would be something like that :
enum class FACING(val direction: Int) {
FRONT_CAMERA(CameraCharacteristics.LENS_FACING_FRONT), REAR_CAMERA(CameraCharacteristics.LENS_FACING_BACK)
}
@RequiresApi(21)
fun CameraManager.getCameraByDirection(direction: FACING): Pair<String, CameraCharacteristics> {
for (cameraId in this.cameraIdList) {
val characteristics = this.getCameraCharacteristics(cameraId)
val cameraDirection = characteristics.get(CameraCharacteristics.LENS_FACING)
if (cameraDirection != null && cameraDirection == direction.direction) {
return Pair(cameraId, characteristics)
}
}
throw RuntimeException("Camera Not Found")
}
Usage :
val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val (cameraId, cameraCharacteristics) = manager.getCameraByDirection(REAR_CAMERA)
The text was updated successfully, but these errors were encountered:
The Pair return types indicate that we are doing too many at once.
I would suggest separating the implementation into two part.
Get all the Characteristics and return them as a Sequence.
fun CameraManager.getCameraCharacteristics() : Sequence<CameraCharacteristics> =
cameraIdList.asSequence().map{ getCameraCharacteristics(cameraId) }
Now anyone can use their own algorithm to find the best fitting camera for their usecase.
And because Sequence is lazy evaluated it cost less to find the right element.
Extend CameraCharacteristics with is* and isNot* properties.
just one example:
val CameraCharacteristics.isFrontCamera: Boolean
get() {
val cameraDirection = get(CameraCharacteristics.LENS_FACING)
return cameraDirection != null && cameraDirection == LENS_FACING_FRONT
}
I'd like to propose an extension to get a camera by direction.
the implementation would be something like that :
Usage :
The text was updated successfully, but these errors were encountered: