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-09-10 13:03:24 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
0c020875053cf33879e94a1f8770a66204d46646
0c020875
1 parent
94bbad4f
implement analyze image for MacOS
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
1 deletions
macos/mobile_scanner/Sources/mobile_scanner/MobileScannerPlugin.swift
macos/mobile_scanner/Sources/mobile_scanner/MobileScannerPlugin.swift
View file @
0c02087
...
...
@@ -71,6 +71,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
stop
(
result
)
case
"updateScanWindow"
:
updateScanWindow
(
call
,
result
)
case
"analyzeImage"
:
analyzeImage
(
call
,
result
)
default
:
result
(
FlutterMethodNotImplemented
)
}
...
...
@@ -124,7 +126,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
VTCreateCGImageFromCVPixelBuffer
(
self
!.
latestBuffer
,
options
:
nil
,
imageOut
:
&
cgImage
)
let
imageRequestHandler
=
VNImageRequestHandler
(
cgImage
:
cgImage
!
)
do
{
let
barcodeRequest
:
VNDetectBarcodesRequest
=
VNDetectBarcodesRequest
(
completionHandler
:
{
[
weak
self
]
(
request
,
error
)
in
let
barcodeRequest
:
VNDetectBarcodesRequest
=
VNDetectBarcodesRequest
(
completionHandler
:
{
[
weak
self
]
(
request
,
error
)
in
self
?
.
imagesCurrentlyBeingProcessed
=
false
if
error
!=
nil
{
...
...
@@ -452,6 +454,68 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
result
(
nil
)
}
func
analyzeImage
(
_
call
:
FlutterMethodCall
,
_
result
:
@escaping
FlutterResult
)
{
let
argReader
=
MapArgumentReader
(
call
.
arguments
as?
[
String
:
Any
])
let
symbologies
:[
VNBarcodeSymbology
]
=
argReader
.
toSymbology
()
guard
let
fileUrl
:
URL
=
URL
(
string
:
argReader
.
string
(
key
:
"filePath"
)
??
""
)
else
{
// TODO: fix error code
result
(
FlutterError
(
code
:
"MobileScanner"
,
message
:
"No image found in analyzeImage!"
,
details
:
nil
))
return
}
guard
let
ciImage
=
CIImage
(
contentsOf
:
fileUrl
)
else
{
// TODO: fix error code
result
(
FlutterError
(
code
:
"MobileScanner"
,
message
:
"No image found in analyzeImage!"
,
details
:
nil
))
return
}
let
imageRequestHandler
=
VNImageRequestHandler
(
ciImage
:
ciImage
,
orientation
:
CGImagePropertyOrientation
.
up
,
options
:
[:])
do
{
let
barcodeRequest
:
VNDetectBarcodesRequest
=
VNDetectBarcodesRequest
(
completionHandler
:
{
[]
(
request
,
error
)
in
if
error
!=
nil
{
DispatchQueue
.
main
.
async
{
// TODO: fix error code
result
(
FlutterError
(
code
:
"MobileScanner"
,
message
:
error
?
.
localizedDescription
,
details
:
nil
))
}
return
}
guard
let
barcodes
:
[
VNBarcodeObservation
]
=
request
.
results
as?
[
VNBarcodeObservation
]
else
{
return
}
if
barcodes
.
isEmpty
{
return
}
result
([
"name"
:
"barcode"
,
"data"
:
barcodes
.
map
({
$0
.
toMap
()
}),
])
})
if
!
symbologies
.
isEmpty
{
// Add the symbologies the user wishes to support.
barcodeRequest
.
symbologies
=
symbologies
}
try
imageRequestHandler
.
perform
([
barcodeRequest
])
}
catch
let
e
{
// TODO: fix error code
DispatchQueue
.
main
.
async
{
result
(
FlutterError
(
code
:
"MobileScanner"
,
message
:
e
.
localizedDescription
,
details
:
nil
))
}
}
}
// Observer for torch state
public
override
func
observeValue
(
forKeyPath
keyPath
:
String
?,
of
object
:
Any
?,
change
:
[
NSKeyValueChangeKey
:
Any
]?,
context
:
UnsafeMutableRawPointer
?)
{
switch
keyPath
{
...
...
Please
register
or
login
to post a comment