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
casvanluijtelaar
2022-06-09 14:23:28 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
83df24265628a75769636ed4fabaf99d7d3ad8d5
83df2426
1 parent
b1d26967
working android implementation
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
24 deletions
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
example/lib/barcode_scanner_window.dart
lib/src/mobile_scanner.dart
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
View file @
83df242
...
...
@@ -5,6 +5,7 @@ import android.app.Activity
import android.content.pm.PackageManager
import android.graphics.Point
import android.graphics.Rect
import android.graphics.RectF
import android.net.Uri
import android.util.Log
import android.util.Size
...
...
@@ -27,6 +28,7 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import io.flutter.view.TextureRegistry
import java.io.File
import kotlin.math.roundToInt
class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry)
...
...
@@ -43,7 +45,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
private var camera: Camera? = null
private var preview: Preview? = null
private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null
private var scanWindow:
Rect
? = null;
private var scanWindow:
List<Float>
? = null;
// @AnalyzeMode
// private var analyzeMode: Int = AnalyzeMode.NONE
...
...
@@ -128,27 +130,23 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
private var scanner = BarcodeScanning.getClient()
private fun updateScanWindow(call: MethodCall) {
val scanWindowData: List<Int>? = call.argument("rect")
if(scanWindowData == null) return
scanWindow = Rect(scanWindowData!![0], scanWindowData!![1], scanWindowData!![2], scanWindowData!![3])
scanWindow = call.argument<List<Float>>("rect")
}
// scales the scanWindow to the provided inputImage and checks if that scaled
// scanWindow contains the barcode
private fun isbarCodeInScanWindow(scanWindow:
Rect
, barcode: Barcode, inputImage: InputImage): Boolean {
private fun isbarCodeInScanWindow(scanWindow:
List<Float>
, barcode: Barcode, inputImage: InputImage): Boolean {
val barcodeBoundingBox = barcode.getBoundingBox()
if(barcodeBoundingBox == null) return false
val imageWidth = inputImage.getWidth();
val imageHeight = inputImage.getHeight();
val left = scanWindow.left * imageWidth
val top = scanWindow.top * imageHeight
val right = scanWindow.right * imageWidth
val bottom = scanWindow.bottom * imageHeight
val left = (scanWindow[0] * imageWidth).roundToInt()
val top = (scanWindow[1] * imageHeight).roundToInt()
val right = (scanWindow[2] * imageWidth).roundToInt()
val bottom = (scanWindow[3] * imageHeight).roundToInt()
val scaledScanWindow = Rect(left, top, right, bottom)
return scaledScanWindow.contains(barcodeBoundingBox)
...
...
example/lib/barcode_scanner_window.dart
View file @
83df242
...
...
@@ -35,7 +35,7 @@ class _BarcodeScannerWithScanWindowState
);
controller
=
MobileScannerController
(
torchEnabled:
tru
e
,
torchEnabled:
fals
e
,
);
}
...
...
lib/src/mobile_scanner.dart
View file @
83df242
...
...
@@ -92,20 +92,20 @@ class _MobileScannerState extends State<MobileScanner>
/// map the texture size to get its new size after fitted to screen
final
fittedSizes
=
applyBoxFit
(
fit
,
textureSize
,
widgetSize
);
final
fittedTextureSize
=
fittedSizes
.
destination
;
/// create a new rectangle that represents the texture on the screen
final
minX
=
widgetSize
.
width
/
2
-
fittedTextureSize
.
width
/
2
;
final
minY
=
widgetSize
.
height
/
2
-
fittedTextureSize
.
height
/
2
;
final
width
=
fittedTextureSize
.
width
;
final
height
=
fittedTextureSize
.
height
;
final
textureWindow
=
Rect
.
fromLTWH
(
minX
,
minY
,
width
,
height
);
/// create a new scan window and with only the area of the rect intersecting the texture
/// create a new scan window and with only the area of the rect intersecting the texture
window
final
scanWindowInTexture
=
scanWindow
.
intersect
(
textureWindow
);
/// update the scanWindow left and top to be relative to the texture not the widget
final
newLeft
=
scanWindowInTexture
.
left
-
minX
;
final
newTop
=
scanWindowInTexture
.
top
-
minY
;
final
newLeft
=
scanWindowInTexture
.
left
-
textureWindow
.
left
;
final
newTop
=
scanWindowInTexture
.
top
-
textureWindow
.
top
;
final
newWidth
=
scanWindowInTexture
.
width
;
final
newHeight
=
scanWindowInTexture
.
height
;
...
...
@@ -114,8 +114,8 @@ class _MobileScannerState extends State<MobileScanner>
/// get the scanWindow as a percentage of the texture
final
percentageLeft
=
windowInTexture
.
left
/
fittedTextureSize
.
width
;
final
percentageRight
=
windowInTexture
.
right
/
fittedTextureSize
.
width
;
final
percentageTop
=
windowInTexture
.
top
/
fittedTextureSize
.
height
;
final
percentageRight
=
windowInTexture
.
right
/
fittedTextureSize
.
width
;
final
percentagebottom
=
windowInTexture
.
bottom
/
fittedTextureSize
.
height
;
/// this rectangle can be send to native code and used to cut out a rectangle of the scan image
...
...
@@ -145,16 +145,15 @@ class _MobileScannerState extends State<MobileScanner>
value
.
size
,
Size
(
constraints
.
maxWidth
,
constraints
.
maxHeight
),
);
print
(
window
);
controller
.
updateScanWindow
(
window
);
}
controller
.
barcodes
.
listen
((
barcode
)
{
if
(!
widget
.
allowDuplicates
)
{
if
(
lastScanned
!=
barcode
.
rawValue
)
{
lastScanned
=
barcode
.
rawValue
;
widget
.
onDetect
(
barcode
,
value
!
as
MobileScannerArguments
);
}
if
(
lastScanned
==
barcode
.
rawValue
)
return
;
lastScanned
=
barcode
.
rawValue
;
widget
.
onDetect
(
barcode
,
value
!
as
MobileScannerArguments
);
}
else
{
widget
.
onDetect
(
barcode
,
value
!
as
MobileScannerArguments
);
}
...
...
Please
register
or
login
to post a comment