Skip to content

Conversation

RafaRuiz
Copy link
Contributor

Started on #1214 to improve #1071 but with a few enhancements:

  • No usage of OpenCV for Android, MLKit would do the inversion.
  • Set intervalInvertImage instead of choosing whether to invert the image constantly or not. This way we can read both types, for example:
Both types of codes (example)

image

  • Kept to latest version 6.0.1 instead from the first one which was taken from 5.0.1

@RafaRuiz RafaRuiz changed the title feature: Ability to scan both normal codes and inverted codes feat: Ability to scan both normal codes and inverted codes Oct 12, 2024
Copy link
Collaborator

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks okay, but I have feedback for the implementation.

Will you be looking into color inversion for the Vision API (for MacOS) as well?


func invertImage(image: UIImage) -> UIImage {
let ciImage = CIImage(image: image)
let filter = CIFilter(name: "CIColorInvert")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use a magic string here, you should be using https://developer.apple.com/documentation/coreimage/cifilter/3228292-colorinvert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@navaronbracke can you give me a hand here?

I'm trying to make it API available, as I've seen there are a few statements for iOS 13 somewhere in the project, but despite setting my project deployment target to 15, I keep seeing this:

image

I've done just a little Swift in my life, it's been more Android

@RafaRuiz RafaRuiz marked this pull request as draft October 18, 2024 09:40
# Conflicts:
#	android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt
Copy link
Collaborator

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juliansteenbakker This entire implementation would omit MacOS, even though that should be supported there also, considering the API's we use.

Since this is a draft, can we close this PR and reopen this on the dev branch instead?

throw UnimplementedError('updateScanWindow() has not been implemented.');
}

/// Set inverting image colors in intervals (for negative Data Matrices).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Set inverting image colors in intervals (for negative Data Matrices).
/// Enable or disable the inverting of image colors.
///
/// This is useful when working with negative-color Data Matrices.
/// See also: https://en.wikipedia.org/wiki/Negative_(photography)

///
/// Defaults to false and is only supported on Android and iOS.
///
/// Defaults to false.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second Defaults to false. can be removed.

Future<void> setShouldConsiderInvertedImages(bool shouldConsiderInvertedImages) async {
await methodChannel.invokeMethod<void>(
'setShouldConsiderInvertedImages',
{'shouldConsiderInvertedImages': shouldConsiderInvertedImages},
Copy link
Collaborator

@navaronbracke navaronbracke Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just pass in a boolean flag instead of a Map here? The native implementations will need to be updated accordingly.

val mediaImage = imageProxy.image ?: return@Analyzer
val inputImage = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)

// Invert every other frame.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Invert every other frame.
// Invert every other frame, by flipping the flag on every input frame.


// Invert every other frame.
if (shouldConsiderInvertedImages) {
invertCurrentImage = !invertCurrentImage // so we jump from one normal to one inverted and viceversa
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
invertCurrentImage = !invertCurrentImage // so we jump from one normal to one inverted and viceversa
invertCurrentImage = !invertCurrentImage

// Convert YUV_420_888 image to NV21 format
// based on our util helper
val bitmap = Bitmap.createBitmap(image.width, image.height, Bitmap.Config.ARGB_8888)
YuvToRgbConverter(activity).yuvToRgb(image, bitmap)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds another usage of the deprecated RenderScript API's. Can we avoid this?

See #1142

/// Sets the zoomScale.
private func setShouldConsiderInvertedImages(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
let shouldConsiderInvertedImages = call.arguments as? Bool
if (shouldConsiderInvertedImages == nil) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we guard mobileScanner.setShouldConsiderInvertedImages(shouldConsiderInvertedImages!) to not do anything if the argument is null ? Otherwise this does not match Android.

var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates

var shouldConsiderInvertedImages: Bool = false
// local variable to invert this image only this time,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be a one line comment.

Suggested change
// local variable to invert this image only this time,
// This flag is used to determine if the current frame should be color-inverted.

@juliansteenbakker juliansteenbakker changed the base branch from master to develop January 16, 2025 08:53
@juliansteenbakker
Copy link
Owner

Sounds good to me @navaronbracke. The android implementation can be a lot faster, i will work on it. I'll first merge the changes from master into develop, before fixing the merge conflicts on this PR.

juliansteenbakker and others added 4 commits January 17, 2025 13:44
# Conflicts:
#	android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
#	android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt
#	ios/Classes/MobileScanner.swift
#	ios/Classes/MobileScannerPlugin.swift
#	lib/src/mobile_scanner_controller.dart
#	lib/src/objects/start_options.dart
@juliansteenbakker
Copy link
Owner

@navaronbracke do you see any usecase for the setShouldConsiderInvertedImages function in the interface? I don't think we would want to set options via functions, it would be best to just let the MobileScannerController handle this with shouldConsiderInvertedImages parameter.

@navaronbracke
Copy link
Collaborator

@juliansteenbakker I agree, you'd probably want to set it up on the controller itself. That follows the same pattern for the other options.

@juliansteenbakker
Copy link
Owner

I have added some improvements, and i think this is the best form we will get with this implementation. Yes, android.renderscript is deprecated, but with the current implementation it's the best/fastest way of transforming images. I think we still need to migrate to something new, but that would be a new issue/pr to fix this. #1142

@juliansteenbakker juliansteenbakker marked this pull request as ready for review January 19, 2025 12:39
Copy link
Collaborator

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change overall is LGTM. I just have some minor feedback.

@juliansteenbakker juliansteenbakker merged commit 357c41c into juliansteenbakker:develop Jan 24, 2025
4 checks passed
@RafaRuiz
Copy link
Contributor Author

But no iOS implementation?

@navaronbracke
Copy link
Collaborator

@RafaRuiz iOS & MacOS use the Vision API. That API should already support color inverted barcodes IIRC.
See also https://developer.apple.com/documentation/vision/vnbarcodeobservation/iscolorinverted

@RafaRuiz
Copy link
Contributor Author

@RafaRuiz iOS & MacOS use the Vision API. That API should already support color inverted barcodes IIRC. See also https://developer.apple.com/documentation/vision/vnbarcodeobservation/iscolorinverted

I don't understand. Apparently the inverted images would be working on Android with this library and not on iOS because there hasn't been any code bridged to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants