An open API service indexing awesome lists of open source software.

https://github.com/androidcrypto/cameraxinjavaupdate


https://github.com/androidcrypto/cameraxinjavaupdate

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# CameraX-In-Java

## Update to grant permissions inside the app

The basic code is NOT MINE but written by Faisal-FS and is published here:

Source: https://github.com/Faisal-FS/CameraX-In-Java

Video tutorial:

Youtube: https://www.youtube.com/watch?v=IrwhjDtpIU0

This update asks the user to grant neccessary permissions inside the app - you do
not need to set the permissions in Settings/find app/etc.

These permissions are captured: access to the Camera, Record Audio and External Storage.

To append the changes to an existing app add the following code lines:

in class definition:
```plaintext
// new for check and grant permissions within the app
private boolean isReadPermissionGranted = false;
private boolean isWritePermissionGranted = false;
private boolean isCameraPermissionGranted = false;
private boolean isRecordAudioPermissionGranted = false;
ActivityResultLauncher myPermissionResultLauncher;
```

at the end of onCreate method:
```plaintext
myPermissionResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback>() {
@Override
public void onActivityResult(Map result) {
if (result.get(Manifest.permission.READ_EXTERNAL_STORAGE) != null) {
isReadPermissionGranted = result.get(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (result.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) != null) {
isWritePermissionGranted = result.get(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (result.get(Manifest.permission.CAMERA) != null) {
isCameraPermissionGranted = result.get(Manifest.permission.CAMERA);
}
if (result.get(Manifest.permission.RECORD_AUDIO) != null) {
isRecordAudioPermissionGranted = result.get(Manifest.permission.RECORD_AUDIO);
}
}
});

// check and request read and write permissions
// don't place this above
// mPermissionResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback>()
requestPermission();
```

below the onCreate method:
```plaintext
private void requestPermission() {
boolean minSDK = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
isReadPermissionGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED;
isWritePermissionGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED;
isWritePermissionGranted = isWritePermissionGranted || minSDK;
isCameraPermissionGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED;
isRecordAudioPermissionGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO
) == PackageManager.PERMISSION_GRANTED;
List permissionRequest = new ArrayList();
if (!isReadPermissionGranted) {
permissionRequest.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (!isWritePermissionGranted) {
permissionRequest.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!isCameraPermissionGranted) {
permissionRequest.add(Manifest.permission.CAMERA);
}
if (!isRecordAudioPermissionGranted) {
permissionRequest.add(Manifest.permission.RECORD_AUDIO);
}
if (!permissionRequest.isEmpty()) {
myPermissionResultLauncher.launch(permissionRequest.toArray(new String[0]));
}
```

I'm for shure this can get shortened but I leave it as it is for better readability.

additional note: DO NOT UPDATE androidx.camera:camera-view higher than cameraxviewVersion = "1.0.0-alpha25".
The newer versions doesn't contain "PreviewView" and you get a lot of hassle to get the code to run again :-)

### Tutorial
This project is a reference code made for the video 'Camera X in Java | Image Capture, Video Capture, Image Analysis'.

Link: https://youtu.be/IrwhjDtpIU0

## Getting Started

These instructions will get you a copy of the project up and running on your local machine.

### Prerequisites

The things you need before installing the software.

* Android Studio

## Installation
Clone this repository and import into **Android Studio**
```bash
git clone [email protected]:Faisal-FS/CameraX-In-Java.git
```

## Configuration
### Gradle Dependencies:
```
dependencies {
def cameraxVersion = "1.1.0-alpha05"
implementation "androidx.camera:camera-core:${cameraxVersion}"
implementation "androidx.camera:camera-camera2:${cameraxVersion}"
implementation "androidx.camera:camera-lifecycle:${cameraxVersion}"

// CameraX View class
implementation 'androidx.camera:camera-view:1.0.0-alpha25'
}
```
### Permissions:
First enable permissions from the app info in device settings.

```
* Camera
* Microphone
* Read/Write External Storage
```

Did I help you out? Consider buying me a coffee 😎

https://www.buymeacoffee.com/codingreel