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:22:07 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c5449517a88e63eeb57bc4f0fe08115c2c202048
c5449517
1 parent
986f130e
reimplement toggle torch on iOS
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
20 deletions
ios/Classes/MobileScanner.swift
ios/Classes/MobileScannerPlugin.swift
ios/Classes/MobileScanner.swift
View file @
c544951
...
...
@@ -259,12 +259,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
// as they interact with the hardware camera.
if
(
torch
)
{
DispatchQueue
.
main
.
async
{
do
{
try
self
.
toggleTorch
(
.
on
)
}
catch
{
// If the torch does not turn on,
// continue with the capture session anyway.
}
self
.
turnTorchOn
()
}
}
...
...
@@ -323,23 +318,60 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
device
=
nil
}
///
Set the torch mode
.
///
Toggle the torch
.
///
/// This method should be called on the main DispatchQueue.
func
toggleTorch
(
_
torch
:
AVCaptureDevice
.
TorchMode
)
throws
{
func
toggleTorch
(
)
{
guard
let
device
=
self
.
device
else
{
return
}
if
(
!
device
.
hasTorch
||
!
device
.
isTorchAvailable
||
!
device
.
isTorchModeSupported
(
torch
)
)
{
if
(
!
device
.
hasTorch
||
!
device
.
isTorchAvailable
)
{
return
}
if
(
device
.
torchMode
!=
torch
)
{
var
newTorchMode
:
AVCaptureDevice
.
TorchMode
=
device
.
torchMode
switch
(
device
.
torchMode
)
{
case
AVCaptureDevice
.
TorchMode
.
auto
:
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
;
}
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
.
isTorchAvailable
||
!
device
.
isTorchModeSupported
(
.
on
)
||
device
.
torchMode
==
.
on
)
{
return
}
do
{
try
device
.
lockForConfiguration
()
device
.
torchMode
=
.
on
device
.
unlockForConfiguration
()
}
catch
(
_
)
{}
}
// Observer for torch state
...
...
ios/Classes/MobileScannerPlugin.swift
View file @
c544951
...
...
@@ -80,8 +80,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
start
(
call
,
result
)
case
"stop"
:
stop
(
result
)
case
"torch"
:
toggleTorch
(
call
,
result
)
case
"toggleTorch"
:
toggleTorch
(
result
)
case
"analyzeImage"
:
analyzeImage
(
call
,
result
)
case
"setScale"
:
...
...
@@ -157,13 +157,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
}
/// Toggles the torch.
private
func
toggleTorch
(
_
call
:
FlutterMethodCall
,
_
result
:
@escaping
FlutterResult
)
{
do
{
try
mobileScanner
.
toggleTorch
(
call
.
arguments
as?
Int
==
1
?
.
on
:
.
off
)
result
(
nil
)
}
catch
{
result
(
FlutterError
(
code
:
"MobileScanner"
,
message
:
error
.
localizedDescription
,
details
:
nil
))
}
private
func
toggleTorch
(
_
result
:
@escaping
FlutterResult
)
{
mobileScanner
.
toggleTorch
()
result
(
nil
)
}
/// Sets the zoomScale.
...
...
Please
register
or
login
to post a comment