Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/content/docs/barcode-scanner.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ async function scanBarcodeImage() {
</Tab>
</Tabs>

### Zoom suggestions (Android)

When a barcode is detected but too far away to be decoded, ML Kit suggests a zoom ratio. Pass [`onZoomSuggested`](/api/react-native-vision-camera-barcode-scanner/interfaces/BarcodeScannerOutputOptions#onzoomsuggested) to apply it to the Camera's [`zoom`](/api/react-native-vision-camera/interfaces/CameraViewProps#zoom), and [`maxZoomRatio`](/api/react-native-vision-camera-barcode-scanner/interfaces/BarcodeScannerOutputOptions#maxzoomratio) to cap suggestions at the device's maximum zoom:

```tsx
const device = useCameraDevice('back')
const [zoom, setZoom] = useState(1)
const scannerOutput = useBarcodeScannerOutput({
barcodeFormats: ['qr-code'],
onBarcodeScanned(barcodes) {
console.log(`Scanned ${barcodes.length} barcodes!`)
},
onError(error) {
console.error(`Failed to scan barcodes!`, error)
},
maxZoomRatio: device.maxZoom,
onZoomSuggested(zoomRatio) {
setZoom(zoomRatio)
},
})
```

> [!NOTE]
> Zoom suggestions are only available on Android - ML Kit on iOS does not provide them.

### Configuring Barcode Formats

The examples above are configured to detect **all** [`BarcodeFormat`](/api/react-native-vision-camera-barcode-scanner/type-aliases/BarcodeFormat)s.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.margelo.nitro.camera.barcodescanner.extensions

import com.google.mlkit.vision.barcode.ZoomSuggestionOptions
import com.margelo.nitro.camera.barcodescanner.BarcodeScannerOptions
import com.margelo.nitro.camera.barcodescanner.BarcodeScannerOutputOptions
import com.margelo.nitro.camera.barcodescanner.TargetBarcodeFormat
Expand All @@ -23,8 +24,19 @@ fun BarcodeScannerOptions.toMLBarcodeScannerOptions(): com.google.mlkit.vision.b
}

fun BarcodeScannerOutputOptions.toMLBarcodeScannerOptions(): com.google.mlkit.vision.barcode.BarcodeScannerOptions {
return com.google.mlkit.vision.barcode.BarcodeScannerOptions
.Builder()
.setBarcodeFormats(this.barcodeFormats)
.build()
val builder =
com.google.mlkit.vision.barcode.BarcodeScannerOptions
.Builder()
.setBarcodeFormats(this.barcodeFormats)
val onZoomSuggested = this.onZoomSuggested
if (onZoomSuggested != null) {
val zoomSuggestionOptions =
ZoomSuggestionOptions.Builder { zoomRatio ->
onZoomSuggested(zoomRatio.toDouble())
true
}
this.maxZoomRatio?.let { zoomSuggestionOptions.setMaxSupportedZoomRatio(it.toFloat()) }
builder.setZoomSuggestionOptions(zoomSuggestionOptions.build())
}
return builder.build()
}

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

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

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

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

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

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

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

Loading