Skip to content

Commit

Permalink
wip: android
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Dec 20, 2023
1 parent 9a10f64 commit 7b2326a
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 138 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

206 changes: 184 additions & 22 deletions packages/barcode-scanning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ const isTorchAvailable = async () => {
return available;
};

const setZoomRatio = async () => {
await BarcodeScanner.setZoomRatio({ zoomRatio: 0.5 });
};

const getZoomRatio = async () => {
const { zoomRatio } = await BarcodeScanner.getZoomRatio();
return zoomRatio;
};

const getMinZoomRatio = async () => {
const { zoomRatio } = await BarcodeScanner.getMinZoomRatio();
return zoomRatio;
};

const getMaxZoomRatio = async () => {
const { zoomRatio } = await BarcodeScanner.getMaxZoomRatio();
return zoomRatio;
};

const openSettings = async () => {
await BarcodeScanner.openSettings();
};
Expand Down Expand Up @@ -240,28 +259,72 @@ If you can't see the camera view, make sure all elements in the DOM are not visi

<docgen-index>

* [`startScan(...)`](#startscan)
* [`stopScan()`](#stopscan)
* [`readBarcodesFromImage(...)`](#readbarcodesfromimage)
* [`scan(...)`](#scan)
* [`isSupported()`](#issupported)
* [`enableTorch()`](#enabletorch)
* [`disableTorch()`](#disabletorch)
* [`toggleTorch()`](#toggletorch)
* [`isTorchEnabled()`](#istorchenabled)
* [`isTorchAvailable()`](#istorchavailable)
* [`openSettings()`](#opensettings)
* [`isGoogleBarcodeScannerModuleAvailable()`](#isgooglebarcodescannermoduleavailable)
* [`installGoogleBarcodeScannerModule()`](#installgooglebarcodescannermodule)
* [`checkPermissions()`](#checkpermissions)
* [`requestPermissions()`](#requestpermissions)
* [`addListener('barcodeScanned', ...)`](#addlistenerbarcodescanned)
* [`addListener('scanError', ...)`](#addlistenerscanerror)
* [`addListener('googleBarcodeScannerModuleInstallProgress', ...)`](#addlistenergooglebarcodescannermoduleinstallprogress)
* [`removeAllListeners()`](#removealllisteners)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)
* [Enums](#enums)
- [@capacitor-mlkit/barcode-scanning](#capacitor-mlkitbarcode-scanning)
- [Features](#features)
- [Demo](#demo)
- [Guides](#guides)
- [Installation](#installation)
- [Android](#android)
- [Variables](#variables)
- [iOS](#ios)
- [Configuration](#configuration)
- [Demo](#demo-1)
- [Usage](#usage)
- [API](#api)
- [startScan(...)](#startscan)
- [stopScan()](#stopscan)
- [readBarcodesFromImage(...)](#readbarcodesfromimage)
- [scan(...)](#scan)
- [isSupported()](#issupported)
- [enableTorch()](#enabletorch)
- [disableTorch()](#disabletorch)
- [toggleTorch()](#toggletorch)
- [isTorchEnabled()](#istorchenabled)
- [isTorchAvailable()](#istorchavailable)
- [setZoomRatio(...)](#setzoomratio)
- [getZoomRatio()](#getzoomratio)
- [getMinZoomRatio()](#getminzoomratio)
- [getMaxZoomRatio()](#getmaxzoomratio)
- [openSettings()](#opensettings)
- [isGoogleBarcodeScannerModuleAvailable()](#isgooglebarcodescannermoduleavailable)
- [installGoogleBarcodeScannerModule()](#installgooglebarcodescannermodule)
- [checkPermissions()](#checkpermissions)
- [requestPermissions()](#requestpermissions)
- [addListener('barcodeScanned', ...)](#addlistenerbarcodescanned-)
- [addListener('scanError', ...)](#addlistenerscanerror-)
- [addListener('googleBarcodeScannerModuleInstallProgress', ...)](#addlistenergooglebarcodescannermoduleinstallprogress-)
- [removeAllListeners()](#removealllisteners)
- [Interfaces](#interfaces)
- [StartScanOptions](#startscanoptions)
- [ReadBarcodesFromImageResult](#readbarcodesfromimageresult)
- [Barcode](#barcode)
- [ReadBarcodesFromImageOptions](#readbarcodesfromimageoptions)
- [ScanResult](#scanresult)
- [ScanOptions](#scanoptions)
- [IsSupportedResult](#issupportedresult)
- [IsTorchEnabledResult](#istorchenabledresult)
- [IsTorchAvailableResult](#istorchavailableresult)
- [SetZoomRatioOptions](#setzoomratiooptions)
- [GetZoomRatioResult](#getzoomratioresult)
- [GetMinZoomRatioResult](#getminzoomratioresult)
- [GetMaxZoomRatioResult](#getmaxzoomratioresult)
- [IsGoogleBarcodeScannerModuleAvailableResult](#isgooglebarcodescannermoduleavailableresult)
- [PermissionStatus](#permissionstatus)
- [PluginListenerHandle](#pluginlistenerhandle)
- [BarcodeScannedEvent](#barcodescannedevent)
- [ScanErrorEvent](#scanerrorevent)
- [GoogleBarcodeScannerModuleInstallProgressEvent](#googlebarcodescannermoduleinstallprogressevent)
- [Type Aliases](#type-aliases)
- [CameraPermissionState](#camerapermissionstate)
- [PermissionState](#permissionstate)
- [Enums](#enums)
- [BarcodeFormat](#barcodeformat)
- [LensFacing](#lensfacing)
- [BarcodeValueType](#barcodevaluetype)
- [GoogleBarcodeScannerModuleInstallState](#googlebarcodescannermoduleinstallstate)
- [Terms \& Privacy](#terms--privacy)
- [Changelog](#changelog)
- [License](#license)

</docgen-index>

Expand Down Expand Up @@ -446,6 +509,76 @@ Only available on Android and iOS.
--------------------


### setZoomRatio(...)

```typescript
setZoomRatio(options: SetZoomRatioOptions) => Promise<void>
```

Set the zoom ratio of the camera.

Only available on Android and iOS.

| Param | Type |
| ------------- | ------------------------------------------------------------------- |
| **`options`** | <code><a href="#setzoomratiooptions">SetZoomRatioOptions</a></code> |

**Since:** 5.4.0

--------------------


### getZoomRatio()

```typescript
getZoomRatio() => Promise<GetZoomRatioResult>
```

Get the zoom ratio of the camera.

Only available on Android and iOS.

**Returns:** <code>Promise&lt;<a href="#getzoomratioresult">GetZoomRatioResult</a>&gt;</code>

**Since:** 5.4.0

--------------------


### getMinZoomRatio()

```typescript
getMinZoomRatio() => Promise<GetMinZoomRatioResult>
```

Get the minimum zoom ratio of the camera.

Only available on Android and iOS.

**Returns:** <code>Promise&lt;<a href="#getminzoomratioresult">GetMinZoomRatioResult</a>&gt;</code>

**Since:** 5.4.0

--------------------


### getMaxZoomRatio()

```typescript
getMaxZoomRatio() => Promise<GetMaxZoomRatioResult>
```

Get the maximum zoom ratio of the camera.

Only available on Android and iOS.

**Returns:** <code>Promise&lt;<a href="#getmaxzoomratioresult">GetMaxZoomRatioResult</a>&gt;</code>

**Since:** 5.4.0

--------------------


### openSettings()

```typescript
Expand Down Expand Up @@ -621,6 +754,7 @@ Remove all listeners for this plugin.
| ---------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------- | ----- |
| **`formats`** | <code>BarcodeFormat[]</code> | Improve the speed of the barcode scanner by configuring the barcode formats to scan for. | 0.0.1 |
| **`lensFacing`** | <code><a href="#lensfacing">LensFacing</a></code> | Configure the camera (front or back) to use. | 0.0.1 |
| **`zoomRatio`** | <code>number</code> | The initial zoom ratio of the camera. | 5.4.0 |


#### ReadBarcodesFromImageResult
Expand Down Expand Up @@ -685,6 +819,34 @@ Remove all listeners for this plugin.
| **`available`** | <code>boolean</code> | Whether or not the torch is available. | 0.0.1 |


#### SetZoomRatioOptions

| Prop | Type | Description | Since |
| --------------- | ------------------- | ---------------------- | ----- |
| **`zoomRatio`** | <code>number</code> | The zoom ratio to set. | 5.4.0 |


#### GetZoomRatioResult

| Prop | Type | Description | Since |
| --------------- | ------------------- | --------------- | ----- |
| **`zoomRatio`** | <code>number</code> | The zoom ratio. | 5.4.0 |


#### GetMinZoomRatioResult

| Prop | Type | Description | Since |
| --------------- | ------------------- | ----------------------- | ----- |
| **`zoomRatio`** | <code>number</code> | The minimum zoom ratio. | 5.4.0 |


#### GetMaxZoomRatioResult

| Prop | Type | Description | Since |
| --------------- | ------------------- | ----------------------- | ----- |
| **`zoomRatio`** | <code>number</code> | The maximum zoom ratio. | 5.4.0 |


#### IsGoogleBarcodeScannerModuleAvailableResult

| Prop | Type | Description | Since |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.media.Image;
import android.net.Uri;
import android.provider.Settings;
import android.util.Size;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -26,11 +22,9 @@
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.CameraController;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import com.getcapacitor.Logger;
import com.getcapacitor.PermissionState;
import com.getcapacitor.PluginCall;
import com.google.android.gms.common.moduleinstall.InstallStatusListener;
Expand All @@ -46,6 +40,10 @@
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions;
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning;
import com.google.mlkit.vision.common.InputImage;
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.options.SetZoomRatioOptions;
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetMaxZoomRatioResult;
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetMinZoomRatioResult;
import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetZoomRatioResult;

public class BarcodeScanner implements ImageAnalysis.Analyzer {

Expand Down Expand Up @@ -113,6 +111,10 @@ public void startScan(ScanSettings scanSettings, StartScanResultCallback callbac
// Start the camera
camera =
processCameraProvider.bindToLifecycle((LifecycleOwner) plugin.getContext(), cameraSelector, preview, imageAnalysis);
if (this.scanSettings.zoomRatio != null) {
camera.getCameraControl().setZoomRatio(this.scanSettings.zoomRatio);
}

callback.success();
} catch (Exception exception) {
callback.error(exception);
Expand Down Expand Up @@ -263,6 +265,41 @@ public boolean isTorchAvailable() {
return plugin.getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}

public void setZoomRatio(SetZoomRatioOptions options) {
float zoomRatio = options.getZoomRatio();
if (camera == null) {
return;
}
camera.getCameraControl().setZoomRatio(zoomRatio);
}

@Nullable
public GetZoomRatioResult getZoomRatio() {
if (camera == null) {
return null;
}
float zoomRatio = camera.getCameraInfo().getZoomState().getValue().getZoomRatio();
return new GetZoomRatioResult(zoomRatio);
}

@Nullable
public GetMinZoomRatioResult getMinZoomRatio() {
if (camera == null) {
return null;
}
float minZoomRatio = camera.getCameraInfo().getZoomState().getValue().getMinZoomRatio();
return new GetMinZoomRatioResult(minZoomRatio);
}

@Nullable
public GetMaxZoomRatioResult getMaxZoomRatio() {
if (camera == null) {
return null;
}
float maxZoomRatio = camera.getCameraInfo().getZoomState().getValue().getMaxZoomRatio();
return new GetMaxZoomRatioResult(maxZoomRatio);
}

public void openSettings(PluginCall call) {
Uri uri = Uri.fromParts("package", plugin.getAppId(), null);
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri);
Expand All @@ -289,6 +326,10 @@ public boolean requestCameraPermissionIfNotDetermined(PluginCall call) throws Ex
}
}

public boolean isCameraActive() {
return camera != null;
}

@Override
public void analyze(@NonNull ImageProxy imageProxy) {
@SuppressLint("UnsafeOptInUsageError")
Expand Down
Loading

0 comments on commit 7b2326a

Please sign in to comment.