Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Camera2 Extension  #472

Open
Open

Description

@Muhannad508

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)

Activity

  1. kioba commented on Aug 14, 2018

    @kioba

    As far as I know, there could be multiple LENS_FACING and LENS_FACING_BACK hardware but we always return the first one.
    we have to also take LENS_FACING_EXTERNAL into consideration https://developer.android.com/reference/android/hardware/camera2/CameraMetadata.html#LENS_FACING_EXTERNAL

    The Pair return types indicate that we are doing too many at once.

    I would suggest separating the implementation into two part.

    1. 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.

    1. 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
        }
    
  2. Muhannad508 commented on Aug 14, 2018

    @Muhannad508
    Author

    Thanks @kioba for your inputs . But it seems hardware APIs go beyond the scope of android-ktx .
    as stated in the below link :
    #373

  3. kioba commented on Aug 14, 2018

    @kioba

    @Muhannad508 thanks for the link! That is good to know 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions