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-04-30 13:11:49 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
986f130e083f570946d6b96a8e0a246e26e7b7c5
986f130e
1 parent
2e0160cb
reimplement toggle torch on MacOS
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
21 deletions
macos/Classes/MobileScannerPlugin.swift
macos/Classes/MobileScannerPlugin.swift
View file @
986f130
...
...
@@ -59,8 +59,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
requestPermission
(
call
,
result
)
case
"start"
:
start
(
call
,
result
)
case
"torch"
:
toggleTorch
(
call
,
result
)
case
"toggleTorch"
:
toggleTorch
(
result
)
case
"setScale"
:
setScale
(
call
,
result
)
case
"resetScale"
:
...
...
@@ -288,12 +288,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
// Turn on the torch if requested.
if
(
torch
)
{
do
{
try
self
.
toggleTorchInternal
(
.
on
)
}
catch
{
// If the torch could not be turned on,
// continue the capture session.
}
self
.
turnTorchOn
()
}
device
.
addObserver
(
self
,
forKeyPath
:
#
keyPath
(
AVCaptureDevice
.
torchMode
),
options
:
.
new
,
context
:
nil
)
...
...
@@ -336,12 +331,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
// TODO: this method should be removed when iOS and MacOS share their implementation.
private
func
toggleTorchInternal
(
_
torch
:
AVCaptureDevice
.
TorchMode
)
throws
{
private
func
toggleTorchInternal
(
)
{
guard
let
device
=
self
.
device
else
{
return
}
if
(
!
device
.
hasTorch
||
!
device
.
isTorchModeSupported
(
torch
)
)
{
if
(
!
device
.
hasTorch
)
{
return
}
...
...
@@ -350,12 +345,57 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
return
}
}
var
newTorchMode
:
AVCaptureDevice
.
TorchMode
=
device
.
torchMode
switch
(
device
.
torchMode
)
{
case
AVCaptureDevice
.
TorchMode
.
auto
:
if
#available(macOS 10.15, *)
{
newTorchMode
=
device
.
isTorchActive
?
AVCaptureDevice
.
TorchMode
.
off
:
AVCaptureDevice
.
TorchMode
.
on
}
break
;
case
AVCaptureDevice
.
TorchMode
.
off
:
newTorchMode
=
AVCaptureDevice
.
TorchMode
.
on
break
;
case
AVCaptureDevice
.
TorchMode
.
on
:
newTorchMode
=
AVCaptureDevice
.
TorchMode
.
off
break
;
default
:
return
;
}
if
(
!
device
.
isTorchModeSupported
(
newTorchMode
)
||
device
.
torchMode
==
newTorchMode
)
{
return
;
}
if
(
device
.
torchMode
!=
torch
)
{
do
{
try
device
.
lockForConfiguration
()
device
.
torchMode
=
torch
device
.
torchMode
=
newTorchMode
device
.
unlockForConfiguration
()
}
catch
(
_
)
{}
}
/// Turn the torch on.
private
func
turnTorchOn
()
{
guard
let
device
=
self
.
device
else
{
return
}
if
(
!
device
.
hasTorch
||
!
device
.
isTorchModeSupported
(
.
on
)
||
device
.
torchMode
==
.
on
)
{
return
}
if
#available(macOS 15.0, *)
{
if
(
!
device
.
isTorchAvailable
)
{
return
}
}
do
{
try
device
.
lockForConfiguration
()
device
.
torchMode
=
.
on
device
.
unlockForConfiguration
()
}
catch
(
_
)
{}
}
/// Reset the zoom scale.
...
...
@@ -370,15 +410,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
result
(
nil
)
}
private
func
toggleTorch
(
_
call
:
FlutterMethodCall
,
_
result
:
@escaping
FlutterResult
)
{
let
requestedTorchMode
:
AVCaptureDevice
.
TorchMode
=
call
.
arguments
as!
Int
==
1
?
.
on
:
.
off
do
{
try
self
.
toggleTorchInternal
(
requestedTorchMode
)
result
(
nil
)
}
catch
{
result
(
FlutterError
(
code
:
"MobileScanner"
,
message
:
error
.
localizedDescription
,
details
:
nil
))
}
private
func
toggleTorch
(
_
result
:
@escaping
FlutterResult
)
{
self
.
toggleTorchInternal
()
result
(
nil
)
}
// func switchAnalyzeMode(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
...
...
Please
register
or
login
to post a comment