Android Front Camera

How to switch to front camera on CameraX?

It looks like the recommended way to achieve this is to store the LensFacing position as an instance variable and then call bindToLifecycle() to switch the camera.

Here is a code snippet that worked for me:

private var lensFacing = CameraX.LensFacing.BACK
private var imageCapture: ImageCapture? = null

@SuppressLint("RestrictedApi")
private fun startCamera() {
bindCameraUseCases()

// Listener for button used to switch cameras
switchButton = view.findViewById(R.id.switch_button)
switchButton.setOnClickListener {
lensFacing = if (CameraX.LensFacing.FRONT == lensFacing) {
CameraX.LensFacing.BACK
} else {
CameraX.LensFacing.FRONT
}
try {
// Only bind use cases if we can query a camera with this orientation
CameraX.getCameraWithLensFacing(lensFacing)
bindCameraUseCases()
} catch (exc: Exception) {
// Do nothing
}
}
}

private fun bindCameraUseCases() {
// Make sure that there are no other use cases bound to CameraX
CameraX.unbindAll()

val previewConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
}.build()
val preview = Preview(previewConfig)

val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
setLensFacing(lensFacing)
}.build()
imageCapture = ImageCapture(imageCaptureConfig)

// Apply declared configs to CameraX using the same lifecycle owner
CameraX.bindToLifecycle(this, preview, imageCapture)
}

Filter app for front camera devices only

I think you need to add this feature:

<uses-feature
android:name="android.hardware.camera.front"
android:required="true" />

But this ALSO assumes that your app uses a android.hardware.camera. And that's usually not a problem and devices with front camera usually have the back camera. But if you do not want that then add:

<uses-feature android:name="android.hardware.camera" android:required="false"/>

Opening the front facing camera

You can use ACTION_IMAGE_CAPTURE Intent to use default camera app using startActivityForResult().

Or you can use this library https://github.com/CameraKit/camerakit-android.

Or this one https://github.com/RedApparat/Fotoapparat.

Naturally, original Camera API (version 1) is also an option.

Android Camera2 front camera

First of all, find out the id of your front camera (if it has one of course)

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
return manager.getCameraIdList();
} catch (CameraAccessException e) {
return null;
}

Then find the faceCamera like this:

CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);

if (cameraCharacteristics == null)
throw new NullPointerException("No camera with id " + cameraId);

return cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;

Lastly, you have to set the camera with that id:

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
characteristics = manager.getCameraCharacteristics(mCameraId);
} catch (CameraAccessException e) {
e.printStackTrace();
}

Note, these are just tips on how to do what you wanna do.
For details on how to start a preview and more, refer to:
http://developer.android.com/samples/Camera2Basic/index.html



Related Topics



Leave a reply



Submit