Java | Android | jetpack

Dependencies & Permissions

def camerax_version = “1.0.0-beta07”
implementation “androidx.camera:camera-camera2:$camerax_version”
implementation “androidx.camera:camera-lifecycle:$camerax_version”
implementation “androidx.camera:camera-view:1.0.0-alpha14”
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />

Prompt User-Permissions

private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};private int REQUEST_CODE_PERMISSIONS = 1001;private boolean allPermissionsGranted(){

for(String permission : REQUIRED_PERMISSIONS){
if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if(requestCode == REQUEST_CODE_PERMISSIONS){
if(allPermissionsGranted()){
startCamera();
} else{
Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
this.finish();
}
}
}

startCamera()

PreviewView mPreviewView;
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
private void startCamera() {

final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);

cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
// No errors need to be handled for this Future.
// This should never be reached.
}

}, ContextCompat.getMainExecutor(this));
}

bindPreview(ProcessCameraProvider)

void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder()
.build();

CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(camInstance)
//.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();

ImageAnalysis imageAnalysis = new ImageAnalysis.Builder().build();

ImageCapture.Builder builder = new ImageCapture.Builder();

ImageCapture imageCapture = builder.build();

preview.setSurfaceProvider(mPreviewView.getSurfaceProvider());

Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview, imageCapture);
}

Image Capture

File file = new File(mDateFormat.format(new Date())+ ".jpg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(file).build();
imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback () {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//do whatever you want with the image saved in <file>.
}
});
}
@Override
public void onError(@NonNull ImageCaptureException error) {
error.printStackTrace();
}
});

Hello Readers! I am a CSE Undergraduate from IIIT Kalyani, with interests in Development, ML, and poetry ofcourse :)