-
Notifications
You must be signed in to change notification settings - Fork 1
/
AVCaptureSession+Convenience.m
211 lines (152 loc) · 7.31 KB
/
AVCaptureSession+Convenience.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// AVCaptureSession+Convenience.m
// Scans
//
// Created by Alexander Ivanov on 29.03.2018.
// Copyright © 2018 Alexander Ivanov. All rights reserved.
//
#import "AVCaptureSession+Convenience.h"
@implementation AVCaptureSession (Convenience)
+ (instancetype)sessionWithPreset:(AVCaptureSessionPreset)sessionPreset {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
if (!sessionPreset)
return session;
if ([session canSetSessionPreset:sessionPreset])
session.sessionPreset = sessionPreset;
else
return Nil;
return session;
}
@end
@implementation AVCaptureDeviceInput (Convenience)
+ (instancetype)deviceInputWithDevice:(AVCaptureDevice *)device {
if (!device)
return Nil;
NSError *error = Nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[error log:@"deviceInputWithDevice:"];
return input;
}
+ (instancetype)deviceInputWithMediaType:(AVMediaType)mediaType {
return mediaType ? [self deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:mediaType]] : Nil;
}
+ (instancetype)deviceInputWithUniqueID:(NSString *)deviceUniqueID {
return deviceUniqueID ? [self deviceInputWithDevice:[AVCaptureDevice deviceWithUniqueID:deviceUniqueID]] : Nil;
}
+ (instancetype)deviceInputWithDeviceType:(AVCaptureDeviceType)deviceType mediaType:(AVMediaType)mediaType position:(AVCaptureDevicePosition)position API_AVAILABLE(ios(10.0)) {
return deviceType ? [self deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithDeviceType:deviceType mediaType:mediaType position:position]] : Nil;
}
@end
@implementation AVCaptureVideoDataOutput (Convenience)
+ (instancetype)videoDataOutputWithSampleBufferDelegate:(id<AVCaptureVideoDataOutputSampleBufferDelegate>)sampleBufferDelegate queue:(dispatch_queue_t)sampleBufferCallbackQueue {
AVCaptureVideoDataOutput *output = [[self alloc] init];
[output setSampleBufferDelegate:sampleBufferDelegate queue:sampleBufferCallbackQueue ?: dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
return output;
}
@end
@implementation AVCaptureMetadataOutput (Convenience)
+ (instancetype)metadataOutputWithMetadataObjectsDelegate:(id<AVCaptureMetadataOutputObjectsDelegate>)objectsDelegate queue:(dispatch_queue_t)objectsCallbackQueue {
AVCaptureMetadataOutput *output = [[self alloc] init];
[output setMetadataObjectsDelegate:objectsDelegate queue:objectsCallbackQueue ?: dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
return output;
}
- (void)setAvailableMetadataObjectTypes:(NSArray<AVMetadataObjectType> *)metadataObjectTypes {
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:metadataObjectTypes.count];
for (NSString *type in metadataObjectTypes)
if ([self.availableMetadataObjectTypes containsObject:type])
[arr addObject:type];
self.metadataObjectTypes = arr;
}
@end
#if __has_include(<UIKit/UIKit.h>)
@implementation AVCapturePhoto (Convenience)
UIImageOrientation UIImageOrientationForCGImagePropertyOrientation(CGImagePropertyOrientation cgOrientation) {
switch (cgOrientation) {
case kCGImagePropertyOrientationUp: return UIImageOrientationUp;
case kCGImagePropertyOrientationDown: return UIImageOrientationDown;
case kCGImagePropertyOrientationLeft: return UIImageOrientationLeft;
case kCGImagePropertyOrientationRight: return UIImageOrientationRight;
case kCGImagePropertyOrientationUpMirrored: return UIImageOrientationUpMirrored;
case kCGImagePropertyOrientationDownMirrored: return UIImageOrientationDownMirrored;
case kCGImagePropertyOrientationLeftMirrored: return UIImageOrientationLeftMirrored;
case kCGImagePropertyOrientationRightMirrored: return UIImageOrientationRightMirrored;
}
return UIImageOrientationUp;
}
- (UIImage *)image {
return [UIImage imageWithCGImage:self.CGImageRepresentation scale:0.0 orientation:UIImageOrientationForCGImagePropertyOrientation([self.metadata[(NSString *)kCGImagePropertyOrientation] intValue])];
}
@end
@implementation UIButton (Convenience)
+ (instancetype)buttonWithFrame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action {
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
button.layer.borderColor = button.currentTitleColor.CGColor;
button.layer.borderWidth = 2.0;
button.layer.cornerRadius = 22.0;
return button;
}
@end
@interface AVCapturePhotoViewController () <AVCapturePhotoCaptureDelegate>
@property (strong, nonatomic, readonly) AVCaptureSession *session;
@property (strong, nonatomic, readonly) AVCaptureDeviceInput *deviceInput;
@property (strong, nonatomic, readonly) AVCapturePhotoOutput *photoOutput;
@end
@implementation AVCapturePhotoViewController
__synthesize(UIButton *, doneButton, [UIButton buttonWithFrame:CGRectMake(20.0, self.view.bounds.size.height - 44.0 - 30.0/* - 68.0*/, 88.0, 44.0) title:@"Done" target:self action:@selector(done:)])
- (IBAction)done:(UIButton *)sender {
[self.photoOutput capturePhotoWithSettings:[AVCapturePhotoSettings photoSettings] delegate:self];
}
__synthesize(UIButton *, cancelButton, [UIButton buttonWithFrame:CGRectMake(self.view.bounds.size.width - 88.0 - 20.0, self.view.bounds.size.height - 44.0 - 30.0/* - 68.0*/, 88.0, 44.0) title:@"Cancel" target:self action:@selector(cancel:)])
- (IBAction)cancel:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:Nil];
}
__synthesize(AVCaptureSession *, session, [AVCaptureSession sessionWithPreset:AVCaptureSessionPresetPhoto])
__synthesize(AVCaptureDeviceInput *, deviceInput, [AVCaptureDeviceInput deviceInputWithMediaType:AVMediaTypeVideo])
__synthesize(AVCapturePhotoOutput *, photoOutput, [[AVCapturePhotoOutput alloc] init])
__synthesize(AVCaptureVideoPreviewLayer *, previewLayer, ({
AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
layer.frame = self.view.bounds;
layer;
}))
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.session addInput:self.deviceInput];
[self.session addOutput:self.photoOutput];
[self.session startRunning];
[self.view.layer insertSublayer:self.previewLayer atIndex:0];
[self.view addSubview:self.doneButton];
[self.view addSubview:self.cancelButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[self.session stopRunning];
}
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
if (self.capturePhotoHandler)
self.capturePhotoHandler(photo);
[self dismissViewControllerAnimated:YES completion:Nil];
[error log:@"didFinishProcessingPhoto:"];
}
@end
#endif
#if __has_include("UIApplication+Convenience.h")
@implementation AVCaptureDevice (Convenience)
+ (void)requestAccessIfNeededForMediaType:(AVMediaType)mediaType completionHandler:(void (^)(BOOL))handler {
AVAuthorizationStatus status = [self authorizationStatusForMediaType:mediaType];
if (status == AVAuthorizationStatusAuthorized) {
if (handler)
handler(YES);
} else if (status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusRestricted) {
[UIApplication openSettings];
if (handler)
handler(NO);
} else {
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:handler];
}
}
@end
#endif