Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Navaron Bracke
2024-08-08 13:42:50 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ca71bdc5cdff4ee932622d3d8fb8d796ee341c8e
ca71bdc5
1 parent
35ce2fa5
fix crash when rounding NaN
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
3 deletions
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
android/src/test/kotlin/dev/steenbakker/mobile_scanner/MobileScannerTest.kt
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
View file @
ca71bdc
...
...
@@ -170,15 +170,19 @@ class MobileScanner(
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
// scales the scanWindow to the provided inputImage and checks if that scaled
// scanWindow contains the barcode
private fun isBarcodeInScanWindow(
// Scales the scanWindow to the provided inputImage and checks if that scaled
// scanWindow contains the barcode.
@VisibleForTesting
fun isBarcodeInScanWindow(
scanWindow: List<Float>,
barcode: Barcode,
inputImage: ImageProxy
): Boolean {
// TODO: use `cornerPoints` instead, since the bounding box is not bound to the coordinate system of the input image
// On iOS we do this correctly, so the calculation should match that.
val barcodeBoundingBox = barcode.boundingBox ?: return false
try {
val imageWidth = inputImage.height
val imageHeight = inputImage.width
...
...
@@ -188,7 +192,13 @@ class MobileScanner(
val bottom = (scanWindow[3] * imageHeight).roundToInt()
val scaledScanWindow = Rect(left, top, right, bottom)
return scaledScanWindow.contains(barcodeBoundingBox)
} catch (exception: IllegalArgumentException) {
// Rounding of the scan window dimensions can fail, due to encountering NaN.
// If we get NaN, rather than give a false positive, just return false.
return false
}
}
// Return the best resolution for the actual device orientation.
...
...
android/src/test/kotlin/dev/steenbakker/mobile_scanner/MobileScannerTest.kt
0 → 100644
View file @
ca71bdc
package dev.steenbakker.mobile_scanner
import android.app.Activity
import android.graphics.Rect
import androidx.camera.core.ImageProxy
import com.google.mlkit.vision.barcode.BarcodeScanner
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.common.Barcode
import kotlin.test.Test
import org.mockito.Mockito
import io.flutter.view.TextureRegistry
import kotlin.test.expect
/*
* This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation.
*
* Once you have built the plugin's example app, you can run these tests from the command
* line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or
* you can run them directly from IDEs that support JUnit such as Android Studio.
*/
internal class MobileScannerTest {
@Test
fun isBarcodeInScanWindow_canHandleNaNValues() {
val barcodeScannerMock = Mockito.mock(BarcodeScanner::class.java)
val mobileScanner = MobileScanner(
Mockito.mock(Activity::class.java),
Mockito.mock(TextureRegistry::class.java),
{ _: List<Map<String, Any?>>, _: ByteArray?, _: Int?, _: Int? -> },
{ _: String -> },
{ _: BarcodeScannerOptions? -> barcodeScannerMock }
)
// Intentional suppression for the mock value in the test,
// since there is no NaN constant.
@Suppress("DIVISION_BY_ZERO")
val notANumber = 0.0f / 0.0f
val barcodeMock: Barcode = Mockito.mock(Barcode::class.java)
val imageMock: ImageProxy = Mockito.mock(ImageProxy::class.java)
// TODO: use corner points instead of bounding box
// Bounding box that is 100 pixels offset from the left and top,
// and is 100 pixels in width and height.
Mockito.`when`(barcodeMock.boundingBox).thenReturn(
Rect(100, 100, 200, 300))
Mockito.`when`(imageMock.height).thenReturn(400)
Mockito.`when`(imageMock.width).thenReturn(400)
// Use a scan window that has an invalid value, but otherwise uses the entire image.
val scanWindow: List<Float> = listOf(0f, notANumber, 100f, 100f)
expect(false) {
mobileScanner.isBarcodeInScanWindow(scanWindow, barcodeMock, imageMock)
}
}
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment