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-03 15:23:26 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
2de0f69c4c77b6099e95a8e86b39dd4079e1ba3e
2de0f69c
1 parent
de533d51
implemented ios
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
3 deletions
ios/Classes/SwiftMobileScannerPlugin.swift
ios/Classes/SwiftMobileScannerPlugin.swift
View file @
2de0f69
...
...
@@ -2,6 +2,7 @@ import AVFoundation
import
Flutter
import
MLKitVision
import
MLKitBarcodeScanning
import
UIKit
public
class
SwiftMobileScannerPlugin
:
NSObject
,
FlutterPlugin
,
FlutterStreamHandler
,
FlutterTexture
,
AVCaptureVideoDataOutputSampleBufferDelegate
{
...
...
@@ -25,6 +26,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
// var analyzeMode: Int = 0
var
analyzing
:
Bool
=
false
var
position
=
AVCaptureDevice
.
Position
.
back
// optional frame to crop camera image before barcode detection
var
scanWindow
:
CGRect
?
var
scanner
=
BarcodeScanner
.
barcodeScanner
()
...
...
@@ -99,13 +103,22 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
}
analyzing
=
true
let
buffer
=
CMSampleBufferGetImageBuffer
(
sampleBuffer
)
let
image
=
VisionImage
(
image
:
buffer
!.
image
)
image
.
orientation
=
imageOrientation
(
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
(
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
]
...
...
@@ -118,6 +131,29 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
// break
// }
}
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
,
...
...
@@ -172,6 +208,15 @@ 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
.
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
{
...
...
Please
register
or
login
to post a comment