Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
r72-engineering committed Aug 21, 2023
2 parents 04d5ce1 + 3767f65 commit 0410af9
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
11 changes: 11 additions & 0 deletions Classes/ios/Scanners/MTBBarcodeScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ typedef NS_ENUM(NSUInteger, MTBTorchMode) {
*/
- (BOOL)setTorchMode:(MTBTorchMode)torchMode error:(NSError **)error;

/**
* Attempts to set a new torch level.
*
* @return YES, if setting the new level was successful, and the torchMode
* property reflects the new state. NO if there was an error - use the
* error parameter to learn about the reason.
*
* @sa torchMode
*/
- (BOOL)setTorchLevel:(float)torchLevel error:(NSError **)error;

/**
* Freeze capture keeping the last frame on previewView.
* If this method is called before startScanning, it has no effect.
Expand Down
69 changes: 59 additions & 10 deletions Classes/ios/Scanners/MTBBarcodeScanner.m
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ - (void)stopRecognizingTaps {

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
if (!self.resultBlock) return;
if (self.isCapturingStillImage) return;

NSMutableArray *codes = [[NSMutableArray alloc] init];

Expand All @@ -464,12 +465,21 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:

#pragma mark - Rotation

- (void)handleApplicationDidChangeStatusBarNotification:(NSNotification *)notification {
- (void)handleOrientationChangeNotification:(NSNotification *)notification {
[self refreshVideoOrientation];
}

- (void)refreshVideoOrientation {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
UIInterfaceOrientation orientation;
if (@available(iOS 13.0, *)) {
orientation = self.previewView.window.windowScene.interfaceOrientation;
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
orientation = [UIApplication sharedApplication].statusBarOrientation;
#pragma GCC diagnostic pop
}

self.capturePreviewLayer.frame = self.previewView.bounds;
if ([self.capturePreviewLayer.connection isVideoOrientationSupported]) {
self.capturePreviewLayer.connection.videoOrientation = [self captureOrientationForInterfaceOrientation:orientation];
Expand Down Expand Up @@ -644,8 +654,8 @@ + (AVCaptureDevicePosition)devicePositionForCamera:(MTBCamera)camera {

- (void)addObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleApplicationDidChangeStatusBarNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
selector:@selector(handleOrientationChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
Expand Down Expand Up @@ -747,6 +757,16 @@ - (BOOL)setTorchMode:(MTBTorchMode)torchMode error:(NSError **)error {
return NO;
}

- (BOOL)setTorchLevel:(float)torchLevel error:(NSError **)error {
if ([self updateForTorchLevel:torchLevel error:error]) {
// we only update our internal state if setting the torch mode was successful
_torchMode = MTBTorchModeOn;
return YES;
}

return NO;
}

- (void)toggleTorch {
switch (self.torchMode) {
case MTBTorchModeOn:
Expand Down Expand Up @@ -784,6 +804,30 @@ - (BOOL)updateForTorchMode:(MTBTorchMode)preferredTorchMode error:(NSError **)er
return YES;
}

- (BOOL)updateForTorchLevel:(float)preferredTorchLevel error:(NSError **)error {
AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if (!([backCamera isTorchAvailable] && [backCamera isTorchModeSupported:AVCaptureTorchModeOn])) {
if (error) {
*error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeTorchModeUnavailable
userInfo:@{NSLocalizedDescriptionKey : @"Torch unavailable or mode not supported."}];
}

return NO;
}

if (![backCamera lockForConfiguration:error]) {
NSLog(@"Failed to acquire lock to update torch mode.");
return NO;
}

BOOL result = [backCamera setTorchModeOnWithLevel:preferredTorchLevel error:error];
[backCamera unlockForConfiguration];

return result;
}

- (BOOL)hasTorch {
AVCaptureDevice *captureDevice = [self newCaptureDeviceWithCamera:self.camera];
NSError *error = nil;
Expand Down Expand Up @@ -846,20 +890,24 @@ - (void)captureStillImage:(void (^)(UIImage *image, NSError *error))captureBlock
return;
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if (@available(iOS 10.0, *)) {
AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings];
settings.autoStillImageStabilizationEnabled = NO;
settings.flashMode = AVCaptureFlashModeOff;
settings.highResolutionPhotoEnabled = YES;


if (@available(iOS 13.0, *)) {
settings.photoQualityPrioritization = AVCapturePhotoQualityPrioritizationSpeed;
} else {
settings.autoStillImageStabilizationEnabled = NO;
}

dispatch_async(self.privateSessionQueue, ^{
[self.output capturePhotoWithSettings:settings delegate:self];
self.stillImageCaptureBlock = captureBlock;

});
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
AVCaptureConnection *stillConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if (stillConnection == nil) {
if (captureBlock) {
Expand All @@ -884,8 +932,9 @@ - (void)captureStillImage:(void (^)(UIImage *image, NSError *error))captureBlock
captureBlock(image, nil);
}
}];
#pragma GCC diagnostic pop
}
#pragma GCC diagnostic pop

}

#pragma mark - AVCapturePhotoCaptureDelegate
Expand Down
20 changes: 20 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "MTBBarcodeScanner",
platforms: [.iOS(.v9)],
products: [
.library(
name: "MTBBarcodeScanner",
targets: ["MTBBarcodeScanner"]
)
],
targets: [
.target(
name: "MTBBarcodeScanner",
path: "Classes/ios",
publicHeadersPath: "."
)
]
)
2 changes: 2 additions & 0 deletions Project/MTBBarcodeScanner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
<key>NSCameraUsageDescription</key>
<string>For testing purposes</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class SwiftExampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Scan all types of codes
scanner = MTBBarcodeScanner(previewView: previewView)

// If you want to scan only QR codes, use this instead!
// scanner = MTBBarcodeScanner(metadataObjectTypes: [AVMetadataObject.ObjectType.qr.rawValue], previewView: previewView)
}

override func viewDidAppear(_ animated: Bool) {
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This library is no longer actively maintained. If you are interested in maintaining it, please contact [the original author](mailto:[email protected]).

# MTBBarcodeScanner

[![Version](https://img.shields.io/cocoapods/v/MTBBarcodeScanner.svg?style=flat)](http://cocoadocs.org/docsets/MTBBarcodeScanner)
Expand Down Expand Up @@ -200,6 +202,9 @@ class SwiftExampleViewController: UIViewController {
super.viewDidLoad()

scanner = MTBBarcodeScanner(previewView: previewView)

// Alternatively, limit the type of codes you can scan:
// scanner = MTBBarcodeScanner(metadataObjectTypes: [AVMetadataObject.ObjectType.qr.rawValue], previewView: previewView)
}

override func viewDidAppear(_ animated: Bool) {
Expand Down

0 comments on commit 0410af9

Please sign in to comment.