How to access programmatically wide angle camera in android studio?

889 views Asked by At

Im quite new to Android and I'm looking to find a way how to access the wide angle camera. I know how to list all my cameras on a certain device and I know how to see which camera I'm opening. I need to know how to set my camera to open the wide angle one. Thanks in advance ! I also read everything in android developers but there is nothing mentioning about the wide angle camera. And this is how I'm opening/activating my camera.

public class CameraActivity extends AppCompatActivity {

    private static final String TAG = "cam";
    private String lastImageUri;
    private ListenableFuture<ProcessCameraProvider> cameraProviderListenableFuture;
    private ImageCapture imageCapture;
    private PreviewView previewView;
    private PermissionManager permissionManager;
    private final String[] permissions = {Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    private Uri fileUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        Button buttonCameraSelectToDeleteImage = (Button) findViewById(R.id.buttonDeleteImage);
        Button button = (Button) findViewById(R.id.buttonMain);
        previewView = findViewById(R.id.previewView);
        permissionManager = PermissionManager.getInstance(CameraActivity.this);
        cameraProviderListenableFuture = ProcessCameraProvider.getInstance(getApplicationContext());

        cameraProviderListenableFuture.addListener(() -> {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderListenableFuture.get();
                startCameraX(cameraProvider);
            } catch (ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }
        }, getExecutor());

        Button buttonCameraSelectToMakeAndSavePhoto = (Button) findViewById(R.id.buttonCameraSelectToMakeAndSavePhoto);
        buttonCameraSelectToMakeAndSavePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!permissionManager.checkPermissions(permissions)) {
                    permissionManager.askPermission(CameraActivity.this, permissions, 100);
                } else {
                    capturePhoto();
                }
            }
        });

        buttonCameraSelectToDeleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonPropertiesArray.arrayOfUriStrings.remove(lastImageUri);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CameraActivity.this, AddInternActivity.class);
                startActivity(intent);
            }
        });

//        Context context = CameraActivity.this;
//        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
//        try {
//            String[] cameraIds = manager.getCameraIdList();
//            Log.d("cameraIds", Arrays.toString(cameraIds));
//        } catch (CameraAccessException e) {
//            e.printStackTrace();
//        }
    }

    private Executor getExecutor() {
        return ContextCompat.getMainExecutor(this);
    }

    private void startCameraX(ProcessCameraProvider cameraProvider) {
        cameraProvider.unbindAll();
        // Camera Selector use case
        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();
        // Preview use case
        Preview preview = new Preview.Builder().build();
        preview.setSurfaceProvider(previewView.getSurfaceProvider());
        // Image capture use case
        imageCapture = new ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                .build();
        cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview, imageCapture);
    }

    private void capturePhoto() {

        long timeStamp = System.currentTimeMillis();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, timeStamp);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
        imageCapture.takePicture(
                new ImageCapture.OutputFileOptions.Builder(
                        getContentResolver(),
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        contentValues
                ).build(),
                getExecutor(),
                new ImageCapture.OnImageSavedCallback() {
                    @Override
                    public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                        if (CommonPropertiesArray.arrayOfUriStrings.size() < 3) {
                            fileUri = (outputFileResults.getSavedUri());
                            lastImageUri = String.valueOf(fileUri);
                            CommonPropertiesArray.arrayOfUriStrings.add(lastImageUri);
                            Toast.makeText(CameraActivity.this, "Photo has been save successfully", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(CameraActivity.this, "You can make max 3 picture", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull ImageCaptureException exception) {
                        Toast.makeText(CameraActivity.this, "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            permissionManager.handlePermissionResult(CameraActivity.this, 100, permissions,
                    grantResults);
        }
    }
0

There are 0 answers