Skip to content

Commit

Permalink
imp: remove deprecated resolution selector
Browse files Browse the repository at this point in the history
  • Loading branch information
juliansteenbakker committed Oct 28, 2024
1 parent 9f9ad4b commit c4ba195
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,6 @@ class MobileScanner(
}
}

// Return the best resolution for the actual device orientation.
//
// By default the resolution is 480x640, which is too low for ML Kit.
// If the given resolution is not supported by the display,
// the closest available resolution is used.
//
// The resolution should be adjusted for the display rotation, to preserve the aspect ratio.
@Suppress("deprecation")
private fun getResolution(cameraResolution: Size): Size {
val rotation = if (Build.VERSION.SDK_INT >= 30) {
activity.display!!.rotation
} else {
val windowManager = activity.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager

windowManager.defaultDisplay.rotation
}

val widthMaxRes = cameraResolution.width
val heightMaxRes = cameraResolution.height

val targetResolution = if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
Size(widthMaxRes, heightMaxRes) // Portrait mode
} else {
Size(heightMaxRes, widthMaxRes) // Landscape mode
}
return targetResolution
}

/**
* Start barcode scanning by initializing the camera and barcode scanner.
*/
Expand All @@ -245,8 +217,7 @@ class MobileScanner(
mobileScannerStartedCallback: MobileScannerStartedCallback,
mobileScannerErrorCallback: (exception: Exception) -> Unit,
detectionTimeout: Long,
cameraResolution: Size?,
newCameraResolutionSelector: Boolean
cameraResolutionWanted: Size?
) {
this.detectionSpeed = detectionSpeed
this.detectionTimeout = detectionTimeout
Expand Down Expand Up @@ -302,48 +273,37 @@ class MobileScanner(
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
val displayManager = activity.applicationContext.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager

if (cameraResolution != null) {
if (newCameraResolutionSelector) {
val selectorBuilder = ResolutionSelector.Builder()
selectorBuilder.setResolutionStrategy(
ResolutionStrategy(
cameraResolution,
ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER
)
)
analysisBuilder.setResolutionSelector(selectorBuilder.build()).build()
} else {
@Suppress("DEPRECATION")
analysisBuilder.setTargetResolution(getResolution(cameraResolution))
}
val cameraResolution = cameraResolutionWanted ?: Size(1920, 1080)

if (displayListener == null) {
displayListener = object : DisplayManager.DisplayListener {
override fun onDisplayAdded(displayId: Int) {}

override fun onDisplayRemoved(displayId: Int) {}

override fun onDisplayChanged(displayId: Int) {
if (newCameraResolutionSelector) {
val selectorBuilder = ResolutionSelector.Builder()
selectorBuilder.setResolutionStrategy(
ResolutionStrategy(
cameraResolution,
ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER
)
)
analysisBuilder.setResolutionSelector(selectorBuilder.build()).build()
} else {
@Suppress("DEPRECATION")
analysisBuilder.setTargetResolution(getResolution(cameraResolution))
}
}
}
val selectorBuilder = ResolutionSelector.Builder()
selectorBuilder.setResolutionStrategy(
ResolutionStrategy(
cameraResolution,
ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER
)
)
analysisBuilder.setResolutionSelector(selectorBuilder.build()).build()

if (displayListener == null) {
displayListener = object : DisplayManager.DisplayListener {
override fun onDisplayAdded(displayId: Int) {}

override fun onDisplayRemoved(displayId: Int) {}

displayManager.registerDisplayListener(
displayListener, null,
)
override fun onDisplayChanged(displayId: Int) {
val selector = ResolutionSelector.Builder().setResolutionStrategy(
ResolutionStrategy(
cameraResolution,
ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER_THEN_LOWER
)
)
analysisBuilder.setResolutionSelector(selector.build()).build()
}
}

displayManager.registerDisplayListener(
displayListener, null,
)
}

val analysis = analysisBuilder.build().apply { setAnalyzer(executor, captureOutput) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class MobileScannerHandler(
val speed: Int = call.argument<Int>("speed") ?: 1
val timeout: Int = call.argument<Int>("timeout") ?: 250
val cameraResolutionValues: List<Int>? = call.argument<List<Int>>("cameraResolution")
val useNewCameraSelector: Boolean = call.argument<Boolean>("useNewCameraSelector") ?: false
val cameraResolution: Size? = if (cameraResolutionValues != null) {
Size(cameraResolutionValues[0], cameraResolutionValues[1])
} else {
Expand Down Expand Up @@ -208,8 +207,7 @@ class MobileScannerHandler(
}
},
timeout.toLong(),
cameraResolution,
useNewCameraSelector
cameraResolution
)
}

Expand Down
1 change: 0 additions & 1 deletion example/lib/barcode_scanner_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class _BarcodeScannerWithControllerState
final MobileScannerController controller = MobileScannerController(
autoStart: false,
torchEnabled: true,
useNewCameraSelector: true,
);

Barcode? _barcode;
Expand Down
11 changes: 0 additions & 11 deletions lib/src/mobile_scanner_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
this.formats = const <BarcodeFormat>[],
this.returnImage = false,
this.torchEnabled = false,
this.useNewCameraSelector = false,
}) : detectionTimeoutMs =
detectionSpeed == DetectionSpeed.normal ? detectionTimeoutMs : 0,
assert(
Expand Down Expand Up @@ -87,15 +86,6 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
/// Defaults to false.
final bool torchEnabled;

/// Use the new resolution selector.
///
/// This feature is experimental and not fully tested yet.
/// Use caution when using this flag,
/// as the new resolution selector may produce unwanted or zoomed images.
///
/// Only supported on Android.
final bool useNewCameraSelector;

/// The internal barcode controller, that listens for detected barcodes.
final StreamController<BarcodeCapture> _barcodesController =
StreamController.broadcast();
Expand Down Expand Up @@ -282,7 +272,6 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
formats: formats,
returnImage: returnImage,
torchEnabled: torchEnabled,
useNewCameraSelector: useNewCameraSelector,
);

try {
Expand Down
7 changes: 0 additions & 7 deletions lib/src/objects/start_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class StartOptions {
required this.formats,
required this.returnImage,
required this.torchEnabled,
required this.useNewCameraSelector,
});

/// The direction for the camera.
Expand All @@ -38,11 +37,6 @@ class StartOptions {
/// Whether the torch should be turned on when the scanner starts.
final bool torchEnabled;

/// Whether the new resolution selector should be used.
///
/// This option is only supported on Android. Other platforms will ignore this option.
final bool useNewCameraSelector;

Map<String, Object?> toMap() {
return <String, Object?>{
if (cameraResolution != null)
Expand All @@ -57,7 +51,6 @@ class StartOptions {
'speed': detectionSpeed.rawValue,
'timeout': detectionTimeoutMs,
'torch': torchEnabled,
'useNewCameraSelector': useNewCameraSelector,
};
}
}

0 comments on commit c4ba195

Please sign in to comment.