-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUUCameraView.m
More file actions
415 lines (351 loc) · 13.1 KB
/
UUCameraView.m
File metadata and controls
415 lines (351 loc) · 13.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// UUCameraView.m
// Useful Utilities - UUCameraView custom camera drop in view
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
#import "UUCameraView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <CoreVideo/CoreVideo.h>
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
#if __has_feature(objc_arc)
#define UU_RELEASE(x) (void)(0)
#define UU_RETAIN(x) x
#define UU_AUTORELEASE(x) x
#define UU_BLOCK_RELEASE(x) (void)(0)
#define UU_BLOCK_COPY(x) [x copy]
#define UU_NATIVE_CAST(x) (__bridge x)
#else
#define UU_RELEASE(x) [x release]
#define UU_RETAIN(x) [x retain]
#define UU_AUTORELEASE(x) [(x) autorelease]
#define UU_BLOCK_RELEASE(x) Block_release(x)
#define UU_BLOCK_COPY(x) Block_copy(x)
#define UU_NATIVE_CAST(x) (x)
#endif
//If you want to provide your own logging mechanism, define UUDebugLog in your .pch
#ifndef UUDebugLog
#ifdef DEBUG
#define UUDebugLog(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
#else
#define UUDebugLog(fmt, ...)
#endif
#endif
@interface UUCameraView()
@property (nonatomic, retain) AVCaptureSession* captureSession;
@property (nonatomic, retain) AVCaptureVideoPreviewLayer* previewLayer;
@property (nonatomic, retain) AVCaptureConnection* connection;
@property (nonatomic, retain) AVCaptureDevice* device;
@property (nonatomic, retain) AVCaptureStillImageOutput* stillOutput;
@property (assign) CATransform3D transform;
@property (nonatomic, assign) CGFloat cameraZoomValue;
@end
@implementation UUCameraView
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
self.zoomValue = 1.0;
self.captureSession = UU_AUTORELEASE([[AVCaptureSession alloc] init]);
NSString* setting = AVCaptureSessionPresetPhoto;
self.captureSession.sessionPreset = setting;
self.previewLayer = UU_AUTORELEASE([[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]);
[self.previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
[self.previewLayer setFrame:self.frame];
[self.layer addSublayer:self.previewLayer];
self.transform = CATransform3DIdentity;
[self setupSession:false];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.zoomValue = 1.0;
self.captureSession = UU_AUTORELEASE([[AVCaptureSession alloc] init]);
NSString* setting = AVCaptureSessionPresetPhoto;
self.captureSession.sessionPreset = setting;
self.previewLayer = UU_AUTORELEASE([[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]);
[self.previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.previewLayer setFrame:frame];
[self.layer addSublayer:self.previewLayer];
self.transform = CATransform3DIdentity;
[self setupSession:false];
}
return self;
}
- (void) startSession
{
[self.captureSession startRunning];
}
- (void) stopSession
{
[self.captureSession stopRunning];
}
- (void) setCameraFrame:(CGRect)frame
{
self.previewLayer.frame = frame;
}
- (void) setCameraOrientation:(UIInterfaceOrientation)orientation;
{
if (orientation == UIDeviceOrientationPortrait)
self.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
if (orientation == UIDeviceOrientationPortraitUpsideDown)
self.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
if (orientation == UIDeviceOrientationLandscapeLeft)
self.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
if (orientation == UIDeviceOrientationLandscapeRight)
self.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
}
// Delegate routine that is called when a sample buffer was written
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (sampleBuffer)
{
CFRetain(sampleBuffer); //This gets CFRelease'd in processImage...
[self processImage:[NSValue valueWithPointer:sampleBuffer]];
//[NSThread detachNewThreadSelector:@selector(processImage:) toTarget:self withObject:[NSValue valueWithPointer:sampleBuffer]];
}
else
{
UUDebugLog(@"captureOutput called with no sample buffer");
}
[self.captureSession startRunning];
}
- (AVCaptureDevice *)createDevice:(bool)frontFacing
{
if (!frontFacing)
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// look at all the video devices and get the first one that's on the front
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice* aDevice in videoDevices)
{
if (aDevice.position == AVCaptureDevicePositionFront)
return aDevice;
}
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
- (void) setupSession:(bool)frontFacing
{
//Create the device...
NSError *error = nil;
self.device = [self createDevice:frontFacing];
[self.device lockForConfiguration:&error];
if ([self.device isFlashModeSupported:AVCaptureFlashModeAuto])
self.device.flashMode = AVCaptureFlashModeAuto;
[self.device unlockForConfiguration];
//Hook an input up to the device...
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];
if (input)
{
[self.captureSession addInput:input];
//Create the still image output...
self.stillOutput = UU_AUTORELEASE([[AVCaptureStillImageOutput alloc] init]);
//Add the outputs to the capture session...
[self.captureSession addOutput:self.stillOutput];
//Configure our connection...
self.connection = [self.stillOutput connectionWithMediaType:AVMediaTypeVideo];
//self.connection.videoMaxFrameDuration= CMTimeMake(1, 10); //kCMTimeZero
self.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
}
}
/*
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
int x = 0;
int y = 0;
int scaledWidth = width / self.cameraZoomValue;
int scaledHeight = height / self.cameraZoomValue;
x += (width - scaledWidth) / 2;
y += (height - scaledHeight) / 2;
baseAddress += (y * bytesPerRow) + (4 * x);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(baseAddress, scaledWidth, scaledHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationRight];
CGImageRelease(quartzImage);
return image;
}
*/
- (void) processImage:(NSValue*)sampleBuffer
{
CMSampleBufferRef buffer = [sampleBuffer pointerValue];
NSData* jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buffer];
if (self.delegate && [self.delegate respondsToSelector:@selector(photoReady:)])
[self.delegate performSelectorOnMainThread:@selector(photoReady:) withObject:jpegData waitUntilDone:YES];
CFRelease(buffer);
}
- (void) setZoomValue:(CGFloat)zoom
{
self.cameraZoomValue = zoom;
self.transform = CATransform3DMakeScale(self.cameraZoomValue, self.cameraZoomValue, 1.0);
self.layer.transform = self.transform;
}
- (void) setFocusPoint:(CGPoint)focalPoint
{
NSError* error;
if ([self.device isFocusPointOfInterestSupported] && [self.device lockForConfiguration:&error])
{
self.device.focusPointOfInterest = focalPoint;
if ([self.device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
self.device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
[self.device unlockForConfiguration];
}
if ([self.device isExposurePointOfInterestSupported] && [self.device lockForConfiguration:&error])
{
self.device.exposurePointOfInterest = focalPoint;
if ([self.device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure])
self.device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
[self.device unlockForConfiguration];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Torch
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (bool) torchOn
{
bool on = false;
NSError* error;
if ([self.device hasTorch] && [self.device lockForConfiguration:&error])
{
on = (self.device.torchMode == AVCaptureTorchModeOn);
[self.device unlockForConfiguration];
}
return on;
}
- (void) setTorchOn:(bool)torchActive
{
NSError* error;
if ([self.device hasTorch] && [self.device lockForConfiguration:&error])
{
if (torchActive)
{
self.device.torchMode = AVCaptureTorchModeOn;
}
else
{
self.device.torchMode = AVCaptureTorchModeOff;
}
[self.device unlockForConfiguration];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Flash
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (int) flashMode
{
AVCaptureFlashMode flashMode = AVCaptureFlashModeOff;
NSError* error;
if ([self.device isFlashAvailable] && [self.device lockForConfiguration:&error])
{
flashMode = self.device.flashMode;
[self.device unlockForConfiguration];
}
if (flashMode == AVCaptureFlashModeAuto)
return kUUFlashAuto;
if (flashMode == AVCaptureFlashModeOn)
return kUUFlashOn;
return kUUFlashOff;
}
- (void) setFlashMode:(int)inFlashMode
{
NSError* error;
if ([self.device isFlashAvailable] && [self.device lockForConfiguration:&error])
{
AVCaptureFlashMode flashMode = AVCaptureFlashModeOff;
if (inFlashMode == kUUFlashAuto)
flashMode = AVCaptureFlashModeAuto;
else if (inFlashMode == kUUFlashOn)
flashMode = AVCaptureFlashModeOn;
if ([self.device isFlashModeSupported:flashMode])
self.device.flashMode = flashMode;
[self.device unlockForConfiguration];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Toggling front/back camera
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setToFrontFacingCamera
{
if (self.device.position == AVCaptureDevicePositionFront)
return;
AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionFront;
for (AVCaptureDevice* aDevice in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo])
{
if ([aDevice position] == desiredPosition)
{
[[self.previewLayer session] beginConfiguration];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:aDevice error:nil];
for (AVCaptureInput *oldInput in [[self.previewLayer session] inputs])
{
[[self.previewLayer session] removeInput:oldInput];
}
self.device = aDevice;
[[self.previewLayer session] addInput:input];
[[self.previewLayer session] commitConfiguration];
break;
}
}
}
- (void) setToBackFacingCamera
{
if (self.device.position == AVCaptureDevicePositionBack)
return;
AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionBack;
for (AVCaptureDevice* aDevice in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo])
{
if ([aDevice position] == desiredPosition)
{
[[self.previewLayer session] beginConfiguration];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:aDevice error:nil];
for (AVCaptureInput *oldInput in [[self.previewLayer session] inputs])
{
[[self.previewLayer session] removeInput:oldInput];
}
self.device = aDevice;
[[self.previewLayer session] addInput:input];
[[self.previewLayer session] commitConfiguration];
break;
}
}
}
- (void) takePicture
{
[self.stillOutput captureStillImageAsynchronouslyFromConnection:self.connection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
[self.captureSession stopRunning];
if (!error)
{
[self captureOutput:self.stillOutput didOutputSampleBuffer:imageDataSampleBuffer fromConnection:self.connection];
}
else
{
UUDebugLog(@"Received error from capture = %@", [error localizedDescription]);
}
}];
}
@end