mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.2 KiB
Matlab
163 lines
4.2 KiB
Matlab
8 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
9 years ago
|
|
||
|
#import "OWSQRCodeScanningViewController.h"
|
||
8 years ago
|
#import "OWSBezierPathView.h"
|
||
9 years ago
|
#import "UIColor+OWS.h"
|
||
8 years ago
|
#import "UIView+OWS.h"
|
||
9 years ago
|
|
||
8 years ago
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
9 years ago
|
@interface OWSQRCodeScanningViewController ()
|
||
|
|
||
8 years ago
|
@property (atomic) ZXCapture *capture;
|
||
9 years ago
|
@property (nonatomic) BOOL captureEnabled;
|
||
8 years ago
|
@property (nonatomic) UIView *maskingView;
|
||
5 years ago
|
@property (nonatomic) dispatch_queue_t captureQueue;
|
||
9 years ago
|
|
||
|
@end
|
||
9 years ago
|
|
||
8 years ago
|
#pragma mark -
|
||
|
|
||
9 years ago
|
@implementation OWSQRCodeScanningViewController
|
||
|
|
||
9 years ago
|
- (void)dealloc
|
||
9 years ago
|
{
|
||
9 years ago
|
[self.capture.layer removeFromSuperlayer];
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
- (instancetype)init
|
||
|
{
|
||
|
self = [super init];
|
||
|
if (!self) {
|
||
|
return self;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
_captureEnabled = NO;
|
||
5 years ago
|
_captureQueue = dispatch_get_main_queue();
|
||
9 years ago
|
|
||
|
return self;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
|
||
9 years ago
|
{
|
||
|
self = [super initWithCoder:aDecoder];
|
||
|
if (!self) {
|
||
|
return self;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
_captureEnabled = NO;
|
||
5 years ago
|
_captureQueue = dispatch_get_main_queue();
|
||
9 years ago
|
|
||
9 years ago
|
return self;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
- (void)loadView
|
||
9 years ago
|
{
|
||
8 years ago
|
[super loadView];
|
||
|
|
||
|
OWSBezierPathView *maskingView = [OWSBezierPathView new];
|
||
|
self.maskingView = maskingView;
|
||
|
[maskingView setConfigureShapeLayerBlock:^(CAShapeLayer *layer, CGRect bounds) {
|
||
|
// Add a circular mask
|
||
|
UIBezierPath *path = [UIBezierPath bezierPathWithRect:bounds];
|
||
6 years ago
|
CGFloat margin = ScaleFromIPhone5To7Plus(24.f, 48.f);
|
||
8 years ago
|
CGFloat radius = MIN(bounds.size.width, bounds.size.height) * 0.5f - margin;
|
||
8 years ago
|
|
||
|
// Center the circle's bounding rectangle
|
||
|
CGRect circleRect = CGRectMake(
|
||
|
bounds.size.width * 0.5f - radius, bounds.size.height * 0.5f - radius, radius * 2.f, radius * 2.f);
|
||
6 years ago
|
UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:circleRect cornerRadius:16.f];
|
||
8 years ago
|
[path appendPath:circlePath];
|
||
|
[path setUsesEvenOddFillRule:YES];
|
||
|
|
||
|
layer.path = path.CGPath;
|
||
|
layer.fillRule = kCAFillRuleEvenOdd;
|
||
6 years ago
|
layer.fillColor = UIColor.lokiDarkestGray.CGColor;
|
||
|
layer.opacity = 0.32f;
|
||
8 years ago
|
}];
|
||
|
[self.view addSubview:maskingView];
|
||
7 years ago
|
[maskingView ows_autoPinToSuperviewEdges];
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
- (void)viewWillAppear:(BOOL)animated
|
||
|
{
|
||
|
[super viewWillAppear:animated];
|
||
9 years ago
|
|
||
|
if (self.captureEnabled) {
|
||
|
[self startCapture];
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
- (void)viewWillDisappear:(BOOL)animated
|
||
9 years ago
|
{
|
||
9 years ago
|
[super viewWillDisappear:animated];
|
||
|
[self stopCapture];
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
6 years ago
|
- (void)viewWillLayoutSubviews
|
||
|
{
|
||
|
self.capture.layer.frame = self.view.bounds;
|
||
|
}
|
||
|
|
||
9 years ago
|
- (void)startCapture
|
||
|
{
|
||
|
self.captureEnabled = YES;
|
||
|
if (!self.capture) {
|
||
5 years ago
|
dispatch_async(self.captureQueue, ^{
|
||
9 years ago
|
self.capture = [[ZXCapture alloc] init];
|
||
|
self.capture.camera = self.capture.back;
|
||
|
self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
|
||
|
self.capture.delegate = self;
|
||
8 years ago
|
|
||
9 years ago
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
6 years ago
|
self.capture.layer.frame = self.view.bounds;
|
||
9 years ago
|
[self.view.layer addSublayer:self.capture.layer];
|
||
|
[self.view bringSubviewToFront:self.maskingView];
|
||
9 years ago
|
[self.capture start];
|
||
9 years ago
|
});
|
||
|
});
|
||
9 years ago
|
} else {
|
||
|
[self.capture start];
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
- (void)stopCapture
|
||
|
{
|
||
9 years ago
|
self.captureEnabled = NO;
|
||
5 years ago
|
dispatch_async(self.captureQueue, ^{
|
||
9 years ago
|
[self.capture stop];
|
||
|
});
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result
|
||
|
{
|
||
8 years ago
|
if (!self.captureEnabled) {
|
||
9 years ago
|
return;
|
||
8 years ago
|
}
|
||
9 years ago
|
[self stopCapture];
|
||
|
|
||
|
// Vibrate
|
||
|
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
|
||
|
|
||
9 years ago
|
if (self.scanDelegate) {
|
||
9 years ago
|
if ([self.scanDelegate respondsToSelector:@selector(controller:didDetectQRCodeWithData:)]) {
|
||
7 years ago
|
OWSLogInfo(@"Scanned Data Code.");
|
||
9 years ago
|
ZXByteArray *byteArray = result.resultMetadata[@(kResultMetadataTypeByteSegments)][0];
|
||
|
NSData *decodedData = [NSData dataWithBytes:byteArray.array length:byteArray.length];
|
||
|
|
||
|
[self.scanDelegate controller:self didDetectQRCodeWithData:decodedData];
|
||
|
}
|
||
|
|
||
|
if ([self.scanDelegate respondsToSelector:@selector(controller:didDetectQRCodeWithString:)]) {
|
||
7 years ago
|
OWSLogInfo(@"Scanned String Code.");
|
||
9 years ago
|
[self.scanDelegate controller:self didDetectQRCodeWithString:result.text];
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
@end
|
||
8 years ago
|
|
||
|
NS_ASSUME_NONNULL_END
|