Using Camera2ArmstringImpl you can take an image with camera2 api using only 4 lines of code:
In project level build.gradle file add maven { url 'https://jitpack.io' } as the following
allprojects {
repositories {
.
.
.
.
maven { url 'https://jitpack.io' }
}
}
In app level build.gradle file add:
implementation 'com.github.KingArmstring:Camera2ArmstringImpl:1.0'
- Create activity for example CameraActivity
- Define an instance of the class PictureCameraImpl
private PictureCameraImpl pictureCamera;
- Create an XML element of TextureView
<TextureView
android:id="@+id/texture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_takepicture"
android:layout_alignParentTop="true"/>
- Refrence the element from the CameraActivity (yourActivity you created in step 1)
TextureView textureView = findViewById(R.id.textureTest);
- Initialize the PictureCameraImple in the CameraActivity and start the camera in the onCreate()
pictureCamera = new Camera2Builder()
.setCameraConsumer(this)
.setCameraView(camera2View)
.setImageFileName("placeholder file name")
.build();
pictureCamera.start();
remember that filePath is the path of the image you will shot, if you want it to be in the root of your device storage
String filePath = Environment.getExternalStorageDirectory()+"/picc.jpg";
- Call the method startBackgroundThread() in the onResume() method of your activity
pictureCamera.startBackgroundThread();
- Call the method stopBackgroundThread() in the onPause() method of your activity
pictureCamera.stopBackgroundThread();
- add a button of your choice and call the method takePicture() in its click handler
btnTakePicture.setOnClickListener((v) -> pictureCamera.takePicture());