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-07 12:29:28 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
95be5663a94811744470b359cfd4a1c8bf8c6b6f
95be5663
1 parent
07db7ab6
refactor ios to use rectOfInterest
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
46 deletions
ios/Classes/SwiftMobileScannerPlugin.swift
ios/Classes/SwiftMobileScannerPlugin.swift
View file @
95be566
...
...
@@ -27,9 +27,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
var
analyzing
:
Bool
=
false
var
position
=
AVCaptureDevice
.
Position
.
back
// optional frame to crop camera image before barcode detection
var
scanWindow
:
CGRect
?
=
nil
;
var
scanner
=
BarcodeScanner
.
barcodeScanner
()
public
static
func
register
(
with
registrar
:
FlutterPluginRegistrar
)
{
...
...
@@ -103,22 +100,14 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
}
analyzing
=
true
let
buffer
=
CMSampleBufferGetImageBuffer
(
sampleBuffer
)
var
image
=
VisionImage
(
image
:
buffer
!.
image
)
var
image
:
VisionImage
?
if
(
scanWindow
!=
nil
)
{
let
cropped
=
cropSampleBuffer
(
imageBuffer
:
buffer
!
,
cropRect
:
scanWindow
!
)
image
=
VisionImage
(
image
:
cropped
)
}
else
{
image
=
VisionImage
(
image
:
buffer
!.
image
)
}
image
!.
orientation
=
imageOrientation
(
image
.
orientation
=
imageOrientation
(
deviceOrientation
:
UIDevice
.
current
.
orientation
,
defaultOrientation
:
.
portrait
)
scanner
.
process
(
image
!
)
{
[
self
]
barcodes
,
error
in
scanner
.
process
(
image
)
{
[
self
]
barcodes
,
error
in
if
error
==
nil
&&
barcodes
!=
nil
{
for
barcode
in
barcodes
!
{
let
event
:
[
String
:
Any
?]
=
[
"name"
:
"barcode"
,
"data"
:
barcode
.
data
]
...
...
@@ -132,29 +121,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
// }
}
public
func
cropSampleBuffer
(
imageBuffer
:
CVImageBuffer
,
cropRect
:
CGRect
)
->
UIImage
{
CVPixelBufferLockBaseAddress
(
imageBuffer
,
.
readOnly
)
let
baseAddress
=
CVPixelBufferGetBaseAddress
(
imageBuffer
)
!
let
bytesPerRow
=
CVPixelBufferGetBytesPerRow
(
imageBuffer
)
let
cropX
=
Int
(
cropRect
.
minX
)
let
cropY
=
Int
(
cropRect
.
minY
)
let
cropWidth
=
Int
(
cropRect
.
width
)
let
cropHeight
=
Int
(
cropRect
.
height
)
let
colorSpace
=
CGColorSpaceCreateDeviceRGB
()
// calculate start position
let
bytesPerPixel
=
4
let
startAddress
=
baseAddress
+
cropY
*
bytesPerRow
+
cropX
*
bytesPerPixel
let
context
=
CGContext
(
data
:
startAddress
,
width
:
cropWidth
,
height
:
cropHeight
,
bitsPerComponent
:
8
,
bytesPerRow
:
bytesPerRow
,
space
:
colorSpace
,
bitmapInfo
:
CGImageAlphaInfo
.
noneSkipFirst
.
rawValue
|
CGBitmapInfo
.
byteOrder32Little
.
rawValue
)
CVPixelBufferUnlockBaseAddress
(
imageBuffer
,
.
readOnly
)
// create image
let
cgImage
:
CGImage
=
context
!.
makeImage
()
!
return
UIImage
(
cgImage
:
cgImage
)
}
func
imageOrientation
(
deviceOrientation
:
UIDeviceOrientation
,
defaultOrientation
:
UIDeviceOrientation
...
...
@@ -209,15 +175,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
let
facing
:
Int
=
argReader
.
int
(
key
:
"facing"
)
??
1
let
formats
:
Array
=
argReader
.
intArray
(
key
:
"formats"
)
??
[]
let
scanWindowData
:
Array
?
=
argReader
.
intArray
(
key
:
"scanWindow"
)
if
(
scanWindowData
!=
nil
)
{
scanWindow
=
CGRect
(
x
:
scanWindowData
!
[
0
],
y
:
scanWindowData
!
[
1
],
width
:
scanWindowData
!
[
2
]
-
scanWindowData
!
[
0
],
height
:
scanWindowData
!
[
3
]
-
scanWindowData
!
[
1
])
}
let
formatList
:
NSMutableArray
=
[]
for
index
in
formats
{
formatList
.
add
(
BarcodeFormat
(
rawValue
:
index
))
...
...
@@ -283,6 +240,23 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
}
captureSession
.
commitConfiguration
()
captureSession
.
startRunning
()
/// limit captureSession area of interest to the scanWindow if provided
let
scanWindowData
:
Array
?
=
argReader
.
intArray
(
key
:
"scanWindow"
)
if
(
scanWindowData
!=
nil
)
{
let
captureMetadataOutput
=
AVCaptureMetadataOutput
()
captureMetadataOutput
.
rectOfInterest
=
CGRect
(
x
:
scanWindowData
!
[
0
],
y
:
scanWindowData
!
[
1
],
width
:
scanWindowData
!
[
2
]
-
scanWindowData
!
[
0
],
height
:
scanWindowData
!
[
3
]
-
scanWindowData
!
[
1
])
captureSession
.
addOutput
(
captureMetadataOutput
)
}
let
demensions
=
CMVideoFormatDescriptionGetDimensions
(
device
.
activeFormat
.
formatDescription
)
let
width
=
Double
(
demensions
.
height
)
let
height
=
Double
(
demensions
.
width
)
...
...
Please
register
or
login
to post a comment