Skip to content

Commit

Permalink
feat(torch): add support for ML Kit Barcode Scanning (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Nov 24, 2024
1 parent 965d322 commit 4a2a439
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-emus-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-torch': minor
---

feat(android): add compatibility for the ML Kit Barcode Scanning plugin
14 changes: 10 additions & 4 deletions packages/torch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ This API requires the following permissions be added to your `AndroidManifest.xm
<uses-permission android:name="android.permission.FLASHLIGHT"/>
```

#### Variables

This plugin will use the following project variables (defined in your app’s `variables.gradle` file):

- `$androidxCameraCoreVersion` version of `androidx.camera:camera-core` (default: `1.1.0`)

## Configuration

No configuration required for this plugin.
Expand Down Expand Up @@ -80,7 +86,7 @@ enable() => Promise<void>

Enable the torch.

Only available on Android and iOS.
Only available on Android (SDK 23+) and iOS.

**Since:** 6.0.0

Expand All @@ -95,7 +101,7 @@ disable() => Promise<void>

Disable the torch.

Only available on Android and iOS.
Only available on Android (SDK 23+) and iOS.

**Since:** 6.0.0

Expand Down Expand Up @@ -144,7 +150,7 @@ toggle() => Promise<void>

Toggle the torch.

Only available on Android and iOS.
Only available on Android (SDK 23+) and iOS.

**Since:** 6.0.0

Expand Down Expand Up @@ -175,4 +181,4 @@ See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/mai

## License

See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/torch/LICENSE).
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/torch/LICENSE).
2 changes: 2 additions & 0 deletions packages/torch/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ext {
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1'
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
androidxCameraCoreVersion = project.hasProperty('androidxCameraCoreVersion') ? rootProject.ext.androidxCameraCoreVersion : '1.1.0'
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
}

Expand Down Expand Up @@ -52,6 +53,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':capacitor-android')
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
implementation "androidx.camera:camera-core:$androidxCameraCoreVersion"
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.CameraControl;
import java.lang.reflect.Method;

public class Torch {

Expand All @@ -16,26 +20,12 @@ public Torch(TorchPlugin plugin) {
}

public void enable() throws CameraAccessException {
CameraManager camera = (CameraManager) plugin.getContext().getSystemService(plugin.getContext().CAMERA_SERVICE);
String cameraId = camera.getCameraIdList()[0];
if (cameraId == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
camera.setTorchMode(cameraId, true);
}
setTorchMode(true);
isTorchEnabled = true;
}

public void disable() throws CameraAccessException {
CameraManager camera = (CameraManager) plugin.getContext().getSystemService(plugin.getContext().CAMERA_SERVICE);
String cameraId = camera.getCameraIdList()[0];
if (cameraId == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
camera.setTorchMode(cameraId, false);
}
setTorchMode(false);
isTorchEnabled = false;
}

Expand All @@ -57,4 +47,44 @@ public void toggle() throws CameraAccessException {
enable();
}
}

private void setTorchMode(boolean enabled) throws CameraAccessException {
CameraControl cameraControl = getCameraControl();
if (cameraControl == null) {
CameraManager cameraManager = getCameraManager();
setTorchMode(cameraManager, enabled);
} else {
setTorchMode(cameraControl, enabled);
}
}

private void setTorchMode(@NonNull CameraManager cameraManager, boolean enabled) throws CameraAccessException {
String cameraId = cameraManager.getCameraIdList()[0];
if (cameraId == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(cameraId, enabled);
}
}

private void setTorchMode(@NonNull CameraControl cameraControl, boolean enabled) {
cameraControl.enableTorch(enabled);
}

@Nullable
private CameraControl getCameraControl() {
try {
Class<?> cls = Class.forName("io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.BarcodeScanner");
Method method = cls.getMethod("getCameraControl");
return (CameraControl) method.invoke(null);
} catch (Exception e) {
return null;
}
}

@NonNull
private CameraManager getCameraManager() {
return (CameraManager) plugin.getContext().getSystemService(plugin.getContext().CAMERA_SERVICE);
}
}
6 changes: 3 additions & 3 deletions packages/torch/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ export interface TorchPlugin {
/**
* Enable the torch.
*
* Only available on Android and iOS.
* Only available on Android (SDK 23+) and iOS.
*
* @since 6.0.0
*/
enable(): Promise<void>;
/**
* Disable the torch.
*
* Only available on Android and iOS.
* Only available on Android (SDK 23+) and iOS.
*
* @since 6.0.0
*/
Expand All @@ -34,7 +34,7 @@ export interface TorchPlugin {
/**
* Toggle the torch.
*
* Only available on Android and iOS.
* Only available on Android (SDK 23+) and iOS.
*
* @since 6.0.0
*/
Expand Down

0 comments on commit 4a2a439

Please sign in to comment.