Skip to content
Newer
Older
100644 417 lines (357 sloc) 14.8 KB
62c72ec @jeradesign Initial Commit
authored
1 //
2 // CVFMainViewController.m
3 // CVFunhouse
4 //
5 // Created by John Brewer on 3/7/12.
6 // Copyright (c) 2012 Jera Design LLC. All rights reserved.
7 //
8
9 #import "CVFMainViewController.h"
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
10 #import <AVFoundation/AVFoundation.h>
2068c3e @jeradesign Snapshot on touch and hold (currently broken).
authored
11 #import <AssetsLibrary/AssetsLibrary.h>
df23aa8 @jeradesign Added Farneback dense optical flow demo.
authored
12 #import "CVFFlipsideViewController.h"
13
78bfbd7 @jeradesign Factored base class CVFImageProcessor and subclass CVFCannyDemo.
authored
14 #import "CVFCannyDemo.h"
3f5686a @jeradesign Added Face Recognition example. Added TODO file. Cleaned up project.
authored
15 #import "CVFFaceDetect.h"
df23aa8 @jeradesign Added Farneback dense optical flow demo.
authored
16 #import "CVFFarneback.h"
763322c @jeradesign Added Laplace demo.
authored
17 #import "CVFLaplace.h"
7842ab8 @jeradesign Fixed typo in name of Lucas-Kanade demo.
authored
18 #import "CVFLucasKanade.h"
aeae42d @jeradesign Added Motion Templates demo.
authored
19 #import "CVFMotionTemplates.h"
20
05426f5 @mshopsin Sephia Demo
mshopsin authored
21 #import "CVFSephiaDemo.h"
945ab85 @jeradesign Added a simple (but well-documented) PassThur example as a starting p…
authored
22 #import "CVFPassThru.h"
62c72ec @jeradesign Initial Commit
authored
23
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
24 @implementation CVFMainViewController {
25 AVCaptureDevice *_cameraDevice;
26 AVCaptureSession *_session;
27 AVCaptureVideoPreviewLayer *_previewLayer;
28 CVFImageProcessor *_imageProcessor;
32ccd7a @jeradesign Add FPS display
authored
29 NSDate *_lastFrameTime;
4c627a6 @jeradesign Initial description support.
authored
30 CGPoint _descriptionOffScreenCenter;
31 CGPoint _descriptionOnScreenCenter;
3d9319f @jeradesign Camera choice is now persistent.
authored
32 bool _useBackCamera;
589f169 @jeradesign Demo classes and descriptions now load from plist.
authored
33 NSArray *_demoList;
2068c3e @jeradesign Snapshot on touch and hold (currently broken).
authored
34 UIImage *_snapshotImage; // In case we need to hang onto it for ARC
35 ALAssetsLibrary *_library;
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
36 }
62c72ec @jeradesign Initial Commit
authored
37
32ccd7a @jeradesign Add FPS display
authored
38 @synthesize fpsLabel = _fpsLabel;
1e23af6 @jeradesign Disable "switch camera" button for devices with only one camera.
authored
39 @synthesize flipCameraButton = _flipCameraButton;
df6b300 @jeradesign Initial description UI.
authored
40 @synthesize descriptionView = _descriptionView;
62c72ec @jeradesign Initial Commit
authored
41 @synthesize flipsidePopoverController = _flipsidePopoverController;
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
42 @synthesize imageView = _imageView;
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
43 //@synthesize imageProcessor = _imageProcessor;
62c72ec @jeradesign Initial Commit
authored
44
45 - (void)viewDidLoad
46 {
47 [super viewDidLoad];
55676ae @mshopsin Flip Button
mshopsin authored
48
589f169 @jeradesign Demo classes and descriptions now load from plist.
authored
49 NSString *demoListPath = [[NSBundle mainBundle] pathForResource:@"Demos" ofType:@"plist"];
50 _demoList = [NSArray arrayWithContentsOfFile:demoListPath];
51
21c7129 @jeradesign Add show/hide FPS control.
authored
52 [self showHideFPS];
4c627a6 @jeradesign Initial description support.
authored
53 [self initializeDescription];
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
54 [self resetImageProcessor];
3d9319f @jeradesign Camera choice is now persistent.
authored
55 _useBackCamera = [[NSUserDefaults standardUserDefaults] boolForKey:@"useBackCamera"];
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
56 [self setupCamera];
57 [self turnCameraOn];
4c627a6 @jeradesign Initial description support.
authored
58
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
59 [[NSNotificationCenter defaultCenter] addObserver:self
60 selector:@selector(resetImageProcessor)
61 name:@"demoNumber"
62 object:nil];
21c7129 @jeradesign Add show/hide FPS control.
authored
63 [[NSNotificationCenter defaultCenter] addObserver:self
64 selector:@selector(showHideFPS)
65 name:@"showFPS"
66 object:nil];
df6b300 @jeradesign Initial description UI.
authored
67 [[NSNotificationCenter defaultCenter] addObserver:self
68 selector:@selector(showHideDescription)
69 name:@"showDescription"
70 object:nil];
2068c3e @jeradesign Snapshot on touch and hold (currently broken).
authored
71
72 _library = [[ALAssetsLibrary alloc] init];
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
73 }
74
1df3d82 @jeradesign Flash the scroll bars when showing description.
authored
75 - (void)viewDidAppear:(BOOL)animated {
76 #pragma unused(animated)
77 bool showDescription = [[NSUserDefaults standardUserDefaults] boolForKey:@"showDescription"];
78 if (showDescription) {
79 double delayInSeconds = 1.0;
80 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
81 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
82 [self.descriptionView.scrollView flashScrollIndicators];
83 });
84 }
85 }
86
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
87 - (void)resetImageProcessor {
586f6fc @jeradesign 64-bit cleanliness.
authored
88 NSInteger demoNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"demoNumber"];
a36763c @jeradesign Fix crash on invalid stored demo number.
authored
89 if (demoNumber >= (int)_demoList.count) {
90 demoNumber = _demoList.count - 1; // Force to last demo.
91 }
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
92
d572156 @jeradesign Convert to modern Objective-C syntax.
authored
93 NSArray *demoInfo = _demoList[demoNumber];
94 NSString *className = demoInfo[1];
589f169 @jeradesign Demo classes and descriptions now load from plist.
authored
95 Class class = NSClassFromString(className);
96 self.imageProcessor = [[class alloc] init];
df6b300 @jeradesign Initial description UI.
authored
97
83f8e81 @jeradesign Move descriptions to html files.
authored
98 NSURL *descriptionUrl = [[NSBundle mainBundle] URLForResource:className withExtension:@"html"];
589f169 @jeradesign Demo classes and descriptions now load from plist.
authored
99 if (descriptionUrl == nil) {
100 descriptionUrl = [[NSBundle mainBundle] URLForResource:@"NoDescription"
101 withExtension:@"html"];
102 }
83f8e81 @jeradesign Move descriptions to html files.
authored
103 NSURLRequest *request = [NSURLRequest requestWithURL:descriptionUrl];
104 [self.descriptionView loadRequest:request];
55676ae @mshopsin Flip Button
mshopsin authored
105 }
106
21c7129 @jeradesign Add show/hide FPS control.
authored
107 - (void)showHideFPS {
108 bool showFPS = [[NSUserDefaults standardUserDefaults] boolForKey:@"showFPS"];
109 [self.fpsLabel setHidden:!showFPS];
110 }
111
4c627a6 @jeradesign Initial description support.
authored
112 - (void)initializeDescription {
f29cf4f @jeradesign Close button on description.
authored
113 self.descriptionContainer.layer.borderColor = [UIColor blackColor].CGColor;
114 self.descriptionContainer.layer.borderWidth = 1.0;
4c627a6 @jeradesign Initial description support.
authored
115
f29cf4f @jeradesign Close button on description.
authored
116 _descriptionOnScreenCenter = self.descriptionContainer.center;
117 _descriptionOffScreenCenter = self.descriptionContainer.center;
118 int descriptionTopY = self.descriptionContainer.center.y -
119 self.descriptionContainer.bounds.size.height / 2;
4c627a6 @jeradesign Initial description support.
authored
120 _descriptionOffScreenCenter.y += self.view.bounds.size.height - descriptionTopY;
121
0600dd2 @jeradesign Show description by default.
authored
122 if ([[NSUserDefaults standardUserDefaults] objectForKey:@"showDescription"] == nil) {
123 [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"showDescription"];
124 [[NSUserDefaults standardUserDefaults] synchronize];
125 }
4c627a6 @jeradesign Initial description support.
authored
126 bool showDescription = [[NSUserDefaults standardUserDefaults] boolForKey:@"showDescription"];
f29cf4f @jeradesign Close button on description.
authored
127 self.descriptionContainer.hidden = !showDescription;
1df3d82 @jeradesign Flash the scroll bars when showing description.
authored
128 if (showDescription) {
129 double delayInSeconds = 2.0;
130 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
131 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
132 [self.descriptionView.scrollView flashScrollIndicators];
133 });
134 }
4c627a6 @jeradesign Initial description support.
authored
135 }
136
df6b300 @jeradesign Initial description UI.
authored
137 - (void)showHideDescription {
138 bool showDescription = [[NSUserDefaults standardUserDefaults] boolForKey:@"showDescription"];
f29cf4f @jeradesign Close button on description.
authored
139 if (showDescription && self.descriptionContainer.isHidden) {
140 self.descriptionContainer.center = _descriptionOffScreenCenter;
141 [self.descriptionContainer setHidden:false];
4c627a6 @jeradesign Initial description support.
authored
142 [UIView animateWithDuration:0.5 animations:^{
f29cf4f @jeradesign Close button on description.
authored
143 self.descriptionContainer.center = _descriptionOnScreenCenter;
1df3d82 @jeradesign Flash the scroll bars when showing description.
authored
144 } completion:^(BOOL finished) {
145 #pragma unused(finished)
146 [self.descriptionView.scrollView flashScrollIndicators];
4c627a6 @jeradesign Initial description support.
authored
147 }];
f29cf4f @jeradesign Close button on description.
authored
148 } else if (!showDescription && !self.descriptionContainer.isHidden) {
4c627a6 @jeradesign Initial description support.
authored
149 [UIView animateWithDuration:0.5 animations:^{
f29cf4f @jeradesign Close button on description.
authored
150 self.descriptionContainer.center = _descriptionOffScreenCenter;
4c627a6 @jeradesign Initial description support.
authored
151 } completion:^(BOOL finished) {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
152 #pragma unused(finished)
f29cf4f @jeradesign Close button on description.
authored
153 self.descriptionContainer.hidden = true;
4c627a6 @jeradesign Initial description support.
authored
154 }];
155 }
df6b300 @jeradesign Initial description UI.
authored
156 }
157
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
158 - (void)setImageProcessor:(CVFImageProcessor *)imageProcessor
159 {
df6b300 @jeradesign Initial description UI.
authored
160 if (_imageProcessor != imageProcessor) {
161 _imageProcessor.delegate = nil;
162 _imageProcessor = imageProcessor;
163 _imageProcessor.delegate = self;
164 }
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
165 }
55676ae @mshopsin Flip Button
mshopsin authored
166
b9ac4a3 @jeradesign Changes to the demo choice in the popover now persist, and better yet…
authored
167 - (CVFImageProcessor *)imageProcessor {
168 return _imageProcessor;
169 }
55676ae @mshopsin Flip Button
mshopsin authored
170
171
62c72ec @jeradesign Initial Commit
authored
172 - (void)viewDidUnload
173 {
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
174 [self turnCameraOff];
175 [self setImageView:nil];
32ccd7a @jeradesign Add FPS display
authored
176 [self setFpsLabel:nil];
1e23af6 @jeradesign Disable "switch camera" button for devices with only one camera.
authored
177 [self setFlipCameraButton:nil];
df6b300 @jeradesign Initial description UI.
authored
178 [self setDescriptionView:nil];
f29cf4f @jeradesign Close button on description.
authored
179 [self setDescriptionContainer:nil];
0aefe6f @jeradesign Add GLKView for AR demo.
authored
180 [self setArView:nil];
62c72ec @jeradesign Initial Commit
authored
181 [super viewDidUnload];
182 // Release any retained subviews of the main view.
183 }
184
185 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
186 {
a6573f0 @jeradesign Changed supported orientations to Portrait only.
authored
187 return (interfaceOrientation == UIInterfaceOrientationPortrait);
62c72ec @jeradesign Initial Commit
authored
188 }
189
190 #pragma mark - Flipside View Controller
191
192 - (void)flipsideViewControllerDidFinish:(CVFFlipsideViewController *)controller
193 {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
194 #pragma unused(controller)
62c72ec @jeradesign Initial Commit
authored
195 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
196 [self dismissModalViewControllerAnimated:YES];
197 } else {
198 [self.flipsidePopoverController dismissPopoverAnimated:YES];
199 self.flipsidePopoverController = nil;
200 }
201 }
202
203 - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
204 {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
205 #pragma unused(popoverController)
62c72ec @jeradesign Initial Commit
authored
206 self.flipsidePopoverController = nil;
207 }
208
209 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
210 {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
211 #pragma unused(sender)
62c72ec @jeradesign Initial Commit
authored
212 if ([[segue identifier] isEqualToString:@"showAlternate"]) {
093c37c @jeradesign Xcode 6.1 compatibility.
authored
213 CVFFlipsideViewController *flipside = [segue destinationViewController];
214 [flipside setDelegate:self];
215 if ([flipside respondsToSelector:@selector(setDemoList:)]) {
216 [flipside performSelector:@selector(setDemoList:)
589f169 @jeradesign Demo classes and descriptions now load from plist.
authored
217 withObject:_demoList];
218 }
219
62c72ec @jeradesign Initial Commit
authored
220 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
221 UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
222 self.flipsidePopoverController = popoverController;
223 popoverController.delegate = self;
224 }
225 }
226 }
227
6f82477 @jeradesign Make hyperlinks launch Safari.
authored
228 #pragma mark - UIWebViewDelegate methods
229
230 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
231 navigationType:(UIWebViewNavigationType)navigationType
232 {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
233 #pragma unused(webView)
234 #pragma unused(navigationType)
235
6f82477 @jeradesign Make hyperlinks launch Safari.
authored
236 NSURL *url = [request URL];
237 if ([[url scheme] isEqual: @"about"]) {
238 return YES;
239 }
83f8e81 @jeradesign Move descriptions to html files.
authored
240 if ([[url scheme] isEqual:@"file"]) {
241 return YES;
242 }
6f82477 @jeradesign Make hyperlinks launch Safari.
authored
243
244 [[UIApplication sharedApplication] openURL:url];
245 return NO;
246 }
247
248 #pragma mark - IBAction methods
249
3d9319f @jeradesign Camera choice is now persistent.
authored
250 - (IBAction)flipAction:(id)sender
251 {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
252 #pragma unused(sender)
3d9319f @jeradesign Camera choice is now persistent.
authored
253 _useBackCamera = !_useBackCamera;
254 [[NSUserDefaults standardUserDefaults] setBool:_useBackCamera forKey:@"useBackCamera"];
8d99411 @jeradesign Synchronize defaults on changes.
authored
255 [[NSUserDefaults standardUserDefaults] synchronize];
3d9319f @jeradesign Camera choice is now persistent.
authored
256 [self turnCameraOff];
257 [self setupCamera];
258 [self turnCameraOn];
259 }
260
62c72ec @jeradesign Initial Commit
authored
261 - (IBAction)togglePopover:(id)sender
262 {
263 if (self.flipsidePopoverController) {
264 [self.flipsidePopoverController dismissPopoverAnimated:YES];
265 self.flipsidePopoverController = nil;
266 } else {
267 [self performSegueWithIdentifier:@"showAlternate" sender:sender];
268 }
269 }
270
4c627a6 @jeradesign Initial description support.
authored
271 - (IBAction)swipeUpAction:(id)sender {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
272 #pragma unused(sender)
4c627a6 @jeradesign Initial description support.
authored
273 [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"showDescription"];
8d99411 @jeradesign Synchronize defaults on changes.
authored
274 [[NSUserDefaults standardUserDefaults] synchronize];
4c627a6 @jeradesign Initial description support.
authored
275 [[NSNotificationCenter defaultCenter] postNotificationName:@"showDescription" object:nil];
276 }
277
278 - (IBAction)swipeDownAction:(id)sender {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
279 #pragma unused(sender)
4c627a6 @jeradesign Initial description support.
authored
280 [[NSUserDefaults standardUserDefaults] setBool:false forKey:@"showDescription"];
8d99411 @jeradesign Synchronize defaults on changes.
authored
281 [[NSUserDefaults standardUserDefaults] synchronize];
4c627a6 @jeradesign Initial description support.
authored
282 [[NSNotificationCenter defaultCenter] postNotificationName:@"showDescription" object:nil];
283 }
284
f29cf4f @jeradesign Close button on description.
authored
285 - (IBAction)closeDescription:(id)sender {
286 #pragma unused(sender)
287 [[NSUserDefaults standardUserDefaults] setBool:false forKey:@"showDescription"];
288 [[NSUserDefaults standardUserDefaults] synchronize];
289 [[NSNotificationCenter defaultCenter] postNotificationName:@"showDescription" object:nil];
290 }
291
2068c3e @jeradesign Snapshot on touch and hold (currently broken).
authored
292 - (IBAction)takeSnapshot:(UILongPressGestureRecognizer *)sender {
293 if (sender.state == UIGestureRecognizerStateBegan) {
294 _snapshotImage = self.imageView.image;
295 NSLog(@"_snapshotImage = %@", _snapshotImage);
296 NSLog(@"CGImage = %@", _snapshotImage.CGImage);
297 [_library writeImageToSavedPhotosAlbum:_snapshotImage.CGImage orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
64979ec @jeradesign More logging for writeImageToSavedPhotosAlbum.
authored
298 NSLog(@"library = %@, _snapshotImage = %@, assetURL = %@, error = %@", _library, _snapshotImage, assetURL, error);
2068c3e @jeradesign Snapshot on touch and hold (currently broken).
authored
299 _snapshotImage = nil;
300 }];
301 // UIImageWriteToSavedPhotosAlbum(_snapshotImage,
302 // self,
303 // @selector(image:didFinishSavingWithError:contextInfo:),
304 // nil);
305 }
306 }
307
308 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
309 {
310 (void)image;
311 (void)contextInfo;
312 NSLog(@"image:%@ didFinishSavingWithError:%@", image, error);
313 _snapshotImage = nil;
314 }
315
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
316 #pragma mark - CVFImageProcessorDelegate
317
318 -(void)imageProcessor:(CVFImageProcessor*)imageProcessor didCreateImage:(UIImage*)image
319 {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
320 #pragma unused(imageProcessor)
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
321 // NSLog(@"Image Received");
322 [self.imageView setImage:image];
32ccd7a @jeradesign Add FPS display
authored
323 NSDate *now = [NSDate date];
324 NSTimeInterval frameDelay = [now timeIntervalSinceDate:_lastFrameTime];
9dec807 @jeradesign Blank FPS when nan.
authored
325 double fps = 1.0/frameDelay;
326 if (fps != fps) {
327 self.fpsLabel.text = @"";
32ccd7a @jeradesign Add FPS display
authored
328 } else {
329 self.fpsLabel.text = [NSString stringWithFormat:@"%05.2f FPS", fps];
330 }
331 _lastFrameTime = now;
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
332 }
333
334 #pragma mark - Camera support
335
416e195 @jeradesign * Now implements a Canny edge detector, inverted for aesthetic reasons.
authored
336 - (void)setupCamera {
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
337 _cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
416e195 @jeradesign * Now implements a Canny edge detector, inverted for aesthetic reasons.
authored
338 NSArray *devices = [AVCaptureDevice devices];
1e23af6 @jeradesign Disable "switch camera" button for devices with only one camera.
authored
339 if (devices.count == 1) {
340
341 }
416e195 @jeradesign * Now implements a Canny edge detector, inverted for aesthetic reasons.
authored
342 for (AVCaptureDevice *device in devices) {
3d9319f @jeradesign Camera choice is now persistent.
authored
343 if (device.position == AVCaptureDevicePositionFront && !_useBackCamera) {
55676ae @mshopsin Flip Button
mshopsin authored
344
345 _cameraDevice = device;
346 break;
347 }
3d9319f @jeradesign Camera choice is now persistent.
authored
348 if (device.position == AVCaptureDevicePositionBack && _useBackCamera) {
55676ae @mshopsin Flip Button
mshopsin authored
349
416e195 @jeradesign * Now implements a Canny edge detector, inverted for aesthetic reasons.
authored
350 _cameraDevice = device;
351 break;
352 }
353 }
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
354 }
355
356 - (void)turnCameraOn {
357 NSError *error;
358 _session = [[AVCaptureSession alloc] init];
359 [_session beginConfiguration];
360 [_session setSessionPreset:AVCaptureSessionPresetMedium];
361 AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:_cameraDevice
362 error:&error];
363 if (input == nil) {
364 NSLog(@"%@", error);
365 }
366
367 [_session addInput:input];
368
369 // Create a VideoDataOutput and add it to the session
370 AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
371 [_session addOutput:output];
372
373 // Configure your output.
416e195 @jeradesign * Now implements a Canny edge detector, inverted for aesthetic reasons.
authored
374 dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
375 [output setSampleBufferDelegate:self queue:queue];
376 dispatch_release(queue);
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
377
378 // Specify the pixel format
379 output.videoSettings =
d572156 @jeradesign Convert to modern Objective-C syntax.
authored
380 @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
381 output.alwaysDiscardsLateVideoFrames = YES;
b2cd5d0 @mshopsin RGB support
mshopsin authored
382 //kCVPixelFormatType_32BGRA
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
383
416e195 @jeradesign * Now implements a Canny edge detector, inverted for aesthetic reasons.
authored
384 // _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
385 // _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
386 // AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeRight;
387 // [_previewLayer setOrientation:orientation];
388 // _previewLayer.frame = self.previewView.bounds;
389 // [self.previewView.layer addSublayer:_previewLayer];
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
390
391 // Start the session running to start the flow of data
392 [_session commitConfiguration];
393 [_session startRunning];
394 }
395
396 - (void)turnCameraOff {
397 [_previewLayer removeFromSuperlayer];
398 _previewLayer = nil;
399 [_session stopRunning];
400 _session = nil;
401 }
402
403 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
404 @autoreleasepool {
c4c68e4 @jeradesign Now builds cleanly with -Wall -Wextra -Wpedantic.
authored
405 #pragma unused(captureOutput)
406 #pragma unused(connection)
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
407 // Get a CMSampleBuffer's Core Video image buffer for the media data
408 CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
409
1e23af6 @jeradesign Disable "switch camera" button for devices with only one camera.
authored
410 [self.imageProcessor processImageBuffer:imageBuffer
411 withMirroring:(_cameraDevice.position ==
412 AVCaptureDevicePositionFront)];
5aa804f @jeradesign Now does video passthrough by way of OpenCV.
authored
413 }
414 }
415
62c72ec @jeradesign Initial Commit
authored
416 @end
Something went wrong with that request. Please try again.