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-08 13:17:25 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
979d7f23bfc428f6af23c8f4b9428732c1882971
979d7f23
1 parent
31f6d12a
convert to passive cropping
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
80 deletions
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
ios/Classes/SwiftMobileScannerPlugin.swift
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
View file @
979d7f2
...
...
@@ -103,24 +103,16 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
// when (analyzeMode) {
// AnalyzeMode.BARCODE -> {
val mediaImage = imageProxy.image ?: return@Analyzer
var inputImage: InputImage?;
if(scanWindow != null) {
val croppedImage = croppedNV21(mediaImage, scanWindow!!)
inputImage = InputImage.fromByteArray(
croppedImage,
scanWindow!!.width(),
scanWindow!!.height(),
imageProxy.imageInfo.rotationDegrees,
IMAGE_FORMAT_NV21,
)
} else {
inputImage = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
}
var inputImage = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
scanner.process(inputImage)
.addOnSuccessListener { barcodes ->
for (barcode in barcodes) {
if(scanWindow != null) {
val boundingBox = barcode.getBoundingBox()
if(boundingBox == null || !scanWindow!!.contains(boundingBox!!)) continue
}
val event = mapOf("name" to "barcode", "data" to barcode.data)
sink?.success(event)
}
...
...
@@ -132,51 +124,6 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
// }
}
private fun croppedNV21(mediaImage: Image, cropRect: Rect): ByteArray {
val yBuffer = mediaImage.planes[0].buffer // Y
val vuBuffer = mediaImage.planes[2].buffer // VU
val ySize = yBuffer.remaining()
val vuSize = vuBuffer.remaining()
val nv21 = ByteArray(ySize + vuSize)
yBuffer.get(nv21, 0, ySize)
vuBuffer.get(nv21, ySize, vuSize)
return cropByteArray(nv21, mediaImage.width, mediaImage.height, cropRect)
}
private fun cropByteArray(src: ByteArray, width: Int, height: Int, cropRect: Rect, ): ByteArray {
val x = cropRect.left * 2 / 2
val y = cropRect.top * 2 / 2
val w = cropRect.width() * 2 / 2
val h = cropRect.height() * 2 / 2
val yUnit = w * h
val uv = yUnit / 2
val nData = ByteArray(yUnit + uv)
val uvIndexDst = w * h - y / 2 * w
val uvIndexSrc = width * height + x
var srcPos0 = y * width
var destPos0 = 0
var uvSrcPos0 = uvIndexSrc
var uvDestPos0 = uvIndexDst
for (i in y until y + h) {
System.arraycopy(src, srcPos0 + x, nData, destPos0, w) //y memory block copy
srcPos0 += width
destPos0 += w
if (i and 1 == 0) {
System.arraycopy(src, uvSrcPos0, nData, uvDestPos0, w) //uv memory block copy
uvSrcPos0 += width
uvDestPos0 += w
}
}
return nData
}
private var scanner = BarcodeScanning.getClient()
@ExperimentalGetImage
...
...
@@ -305,6 +252,11 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
scanner.process(inputImage)
.addOnSuccessListener { barcodes ->
for (barcode in barcodes) {
if(scanWindow != null) {
val boundingBox = barcode.getBoundingBox()
if(boundingBox == null || !scanWindow!!.contains(boundingBox!!)) continue
}
barcodeFound = true
sink?.success(mapOf("name" to "barcode", "data" to barcode.data))
}
...
...
ios/Classes/SwiftMobileScannerPlugin.swift
View file @
979d7f2
...
...
@@ -27,6 +27,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
var
analyzing
:
Bool
=
false
var
position
=
AVCaptureDevice
.
Position
.
back
var
scanWindow
:
CGRect
?
var
scanner
=
BarcodeScanner
.
barcodeScanner
()
public
static
func
register
(
with
registrar
:
FlutterPluginRegistrar
)
{
...
...
@@ -110,6 +112,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
scanner
.
process
(
image
)
{
[
self
]
barcodes
,
error
in
if
error
==
nil
&&
barcodes
!=
nil
{
for
barcode
in
barcodes
!
{
if
scanWindow
!=
nil
{
let
boundingBox
=
barcode
.
frame
if
!
scanWindow
!.
contains
(
boundingBox
)
{
continue
}
}
let
event
:
[
String
:
Any
?]
=
[
"name"
:
"barcode"
,
"data"
:
barcode
.
data
]
sink
?(
event
)
}
...
...
@@ -174,6 +184,20 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
let
torch
:
Bool
=
argReader
.
bool
(
key
:
"torch"
)
??
false
let
facing
:
Int
=
argReader
.
int
(
key
:
"facing"
)
??
1
let
formats
:
Array
=
argReader
.
intArray
(
key
:
"formats"
)
??
[]
let
scanWindowData
:
Array
?
=
argReader
.
floatArray
(
key
:
"scanWindow"
)
if
(
scanWindowData
!=
nil
)
{
let
minX
=
scanWindowData
!
[
0
]
let
minY
=
scanWindowData
!
[
1
]
let
width
=
scanWindowData
!
[
2
]
-
minX
let
height
=
scanWindowData
!
[
3
]
-
minY
scanWindow
=
CGRect
(
x
:
minX
,
y
:
minY
,
width
:
width
,
height
:
height
)
}
let
formatList
:
NSMutableArray
=
[]
for
index
in
formats
{
...
...
@@ -243,27 +267,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
captureSession
.
startRunning
()
let
demensions
=
CMVideoFormatDescriptionGetDimensions
(
device
.
activeFormat
.
formatDescription
)
/// limit captureSession area of interest to the scanWindow if provided
let
scanWindowData
:
Array
?
=
argReader
.
floatArray
(
key
:
"scanWindow"
)
if
(
scanWindowData
!=
nil
)
{
let
captureMetadataOutput
=
AVCaptureMetadataOutput
()
let
minX
=
scanWindowData
!
[
0
]
/
CGFloat
(
demensions
.
width
)
let
minY
=
scanWindowData
!
[
1
]
/
CGFloat
(
demensions
.
height
)
let
maxX
=
scanWindowData
!
[
2
]
/
CGFloat
(
demensions
.
width
)
let
maxY
=
scanWindowData
!
[
3
]
/
CGFloat
(
demensions
.
height
)
let
width
=
maxX
-
minX
let
height
=
maxY
-
minY
captureMetadataOutput
.
rectOfInterest
=
CGRect
(
x
:
minX
,
y
:
minY
,
width
:
width
,
height
:
height
)
captureSession
.
addOutput
(
captureMetadataOutput
)
}
let
width
=
Double
(
demensions
.
height
)
let
height
=
Double
(
demensions
.
width
)
let
size
=
[
"width"
:
width
,
"height"
:
height
]
...
...
@@ -314,6 +317,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
scanner
.
process
(
image
)
{
[
self
]
barcodes
,
error
in
if
error
==
nil
&&
barcodes
!=
nil
{
for
barcode
in
barcodes
!
{
if
scanWindow
!=
nil
{
let
boundingBox
=
barcode
.
frame
if
!
scanWindow
!.
contains
(
boundingBox
)
{
continue
}
}
barcodeFound
=
true
let
event
:
[
String
:
Any
?]
=
[
"name"
:
"barcode"
,
"data"
:
barcode
.
data
]
sink
?(
event
)
...
...
Please
register
or
login
to post a comment