2015-04-01 01:02:57 +00:00
|
|
|
#import "RCTCameraManager.h"
|
|
|
|
#import "RCTCamera.h"
|
|
|
|
#import "RCTBridge.h"
|
2015-04-02 09:40:03 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-04-01 01:02:57 +00:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
static void * CapturingStillImageContext = &CapturingStillImageContext;
|
|
|
|
static void * RecordingContext = &RecordingContext;
|
|
|
|
static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDeviceAuthorizedContext;
|
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
@implementation RCTCameraManager
|
|
|
|
|
2015-04-02 09:40:03 +00:00
|
|
|
+ (id)sharedManager {
|
|
|
|
static RCTCameraManager *sharedCameraManager = nil;
|
|
|
|
@synchronized(self) {
|
|
|
|
if (sharedCameraManager == nil)
|
|
|
|
sharedCameraManager = [[self alloc] init];
|
|
|
|
}
|
|
|
|
return sharedCameraManager;
|
|
|
|
}
|
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
- (UIView *)view
|
|
|
|
{
|
2015-04-02 09:40:03 +00:00
|
|
|
[self setCurrentCamera:[[RCTCamera alloc] init]];
|
|
|
|
return _currentCamera;
|
2015-04-01 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 02:27:18 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(aspect, NSString);
|
2015-04-01 17:32:53 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(camera, NSInteger);
|
2015-04-01 01:02:57 +00:00
|
|
|
RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
|
|
|
|
|
2015-04-01 02:27:18 +00:00
|
|
|
- (NSDictionary *)constantsToExport
|
|
|
|
{
|
|
|
|
return @{
|
|
|
|
@"aspects": @{
|
|
|
|
@"Stretch": AVLayerVideoGravityResize,
|
|
|
|
@"Fit": AVLayerVideoGravityResizeAspect,
|
|
|
|
@"Fill": AVLayerVideoGravityResizeAspectFill
|
|
|
|
},
|
2015-04-01 17:32:53 +00:00
|
|
|
@"cameras": @{
|
|
|
|
@"Front": @(AVCaptureDevicePositionFront),
|
|
|
|
@"Back": @(AVCaptureDevicePositionBack)
|
|
|
|
},
|
2015-04-01 02:27:18 +00:00
|
|
|
@"orientations": @{
|
|
|
|
@"LandscapeLeft": @(AVCaptureVideoOrientationLandscapeLeft),
|
|
|
|
@"LandscapeRight": @(AVCaptureVideoOrientationLandscapeRight),
|
|
|
|
@"Portrait": @(AVCaptureVideoOrientationPortrait),
|
|
|
|
@"PortraitUpsideDown": @(AVCaptureVideoOrientationPortraitUpsideDown)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-02 09:40:03 +00:00
|
|
|
- (id)init {
|
|
|
|
if ((self = [super init])) {
|
|
|
|
[self setSession:[[AVCaptureSession alloc] init]];
|
2015-04-03 01:07:22 +00:00
|
|
|
[[self session] setSessionPreset:AVCaptureSessionPresetHigh];
|
|
|
|
|
|
|
|
dispatch_queue_t sessionQueue = dispatch_queue_create("cameraManagerQueue", DISPATCH_QUEUE_SERIAL);
|
|
|
|
[self setSessionQueue:sessionQueue];
|
|
|
|
|
|
|
|
dispatch_async(sessionQueue, ^{
|
|
|
|
NSError *error = nil;
|
|
|
|
|
|
|
|
AVCaptureDevice *captureDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
|
|
|
|
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
NSLog(@"%@", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([[self session] canAddInput:captureDeviceInput])
|
|
|
|
{
|
|
|
|
[[self session] addInput:captureDeviceInput];
|
|
|
|
[self setCaptureDeviceInput:captureDeviceInput];
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
|
|
|
|
if ([[self session] canAddOutput:stillImageOutput])
|
|
|
|
{
|
|
|
|
[stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}];
|
|
|
|
[[self session] addOutput:stillImageOutput];
|
|
|
|
[self setStillImageOutput:stillImageOutput];
|
|
|
|
}
|
2015-04-03 03:05:28 +00:00
|
|
|
|
|
|
|
[[self session] startRunning];
|
2015-04-03 01:07:22 +00:00
|
|
|
});
|
2015-04-02 09:40:03 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)checkDeviceAuthorizationStatus:(RCTResponseSenderBlock) callback {
|
2015-04-01 01:02:57 +00:00
|
|
|
RCT_EXPORT();
|
|
|
|
NSString *mediaType = AVMediaTypeVideo;
|
2015-04-01 02:27:18 +00:00
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
|
|
|
|
callback(@[[NSNull null], @(granted)]);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-04-02 09:40:03 +00:00
|
|
|
|
|
|
|
- (void)setCamera:(NSInteger)camera
|
|
|
|
{
|
2015-04-03 01:07:22 +00:00
|
|
|
dispatch_async([self sessionQueue], ^{
|
|
|
|
AVCaptureDevice *currentCaptureDevice = [[self captureDeviceInput] device];
|
|
|
|
AVCaptureDevicePosition position = (AVCaptureDevicePosition)camera;
|
|
|
|
AVCaptureDevice *captureDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:(AVCaptureDevicePosition)position];
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
NSError *error = nil;
|
|
|
|
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
NSLog(@"%@", error);
|
|
|
|
}
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
[[self session] beginConfiguration];
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
[[self session] removeInput:[self captureDeviceInput]];
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
if ([[self session] canAddInput:captureDeviceInput])
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:currentCaptureDevice];
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
[self setFlashMode:AVCaptureFlashModeAuto forDevice:captureDevice];
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice];
|
|
|
|
[[self session] addInput:captureDeviceInput];
|
|
|
|
[self setCaptureDeviceInput:captureDeviceInput];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[[self session] addInput:[self captureDeviceInput]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[self session] commitConfiguration];
|
|
|
|
});
|
2015-04-02 09:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setOrientation:(NSInteger)orientation
|
|
|
|
{
|
2015-04-03 01:07:22 +00:00
|
|
|
[[(AVCaptureVideoPreviewLayer *)[[[self currentCamera] viewfinder] layer] connection] setVideoOrientation:orientation];
|
2015-04-02 09:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)takePicture:(RCTResponseSenderBlock) callback {
|
|
|
|
RCT_EXPORT();
|
2015-04-03 01:07:22 +00:00
|
|
|
dispatch_async([self sessionQueue], ^{
|
|
|
|
// Update the orientation on the still image output video connection before capturing.
|
|
|
|
[[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[[self currentCamera] viewfinder] layer] connection] videoOrientation]];
|
|
|
|
|
|
|
|
// Flash set to Auto for Still Capture
|
|
|
|
[self setFlashMode:AVCaptureFlashModeAuto forDevice:[[self captureDeviceInput] device]];
|
|
|
|
|
|
|
|
// Capture a still image.
|
|
|
|
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
|
|
|
|
|
|
|
|
if (imageDataSampleBuffer)
|
|
|
|
{
|
|
|
|
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
|
|
|
|
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
|
|
|
NSString *imageBase64 = [UIImageJPEGRepresentation(image, 1.0) base64EncodedStringWithOptions:0];
|
|
|
|
callback(@[[NSNull null], imageBase64]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(@[RCTMakeError([error description], nil, nil)]);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
});
|
2015-04-02 09:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position
|
|
|
|
{
|
|
|
|
NSArray *devices = [AVCaptureDevice devicesWithMediaType:mediaType];
|
|
|
|
AVCaptureDevice *captureDevice = [devices firstObject];
|
|
|
|
|
|
|
|
for (AVCaptureDevice *device in devices)
|
|
|
|
{
|
|
|
|
if ([device position] == position)
|
|
|
|
{
|
|
|
|
captureDevice = device;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return captureDevice;
|
|
|
|
}
|
|
|
|
|
2015-04-03 01:07:22 +00:00
|
|
|
- (void)setFlashMode:(AVCaptureFlashMode)flashMode forDevice:(AVCaptureDevice *)device
|
|
|
|
{
|
|
|
|
if ([device hasFlash] && [device isFlashModeSupported:flashMode])
|
|
|
|
{
|
|
|
|
NSError *error = nil;
|
|
|
|
if ([device lockForConfiguration:&error])
|
|
|
|
{
|
|
|
|
[device setFlashMode:flashMode];
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"%@", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)subjectAreaDidChange:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
CGPoint devicePoint = CGPointMake(.5, .5);
|
|
|
|
[self focusWithMode:AVCaptureFocusModeContinuousAutoFocus exposeWithMode:AVCaptureExposureModeContinuousAutoExposure atDevicePoint:devicePoint monitorSubjectAreaChange:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)focusWithMode:(AVCaptureFocusMode)focusMode exposeWithMode:(AVCaptureExposureMode)exposureMode atDevicePoint:(CGPoint)point monitorSubjectAreaChange:(BOOL)monitorSubjectAreaChange
|
|
|
|
{
|
|
|
|
dispatch_async([self sessionQueue], ^{
|
|
|
|
AVCaptureDevice *device = [[self captureDeviceInput] device];
|
|
|
|
NSError *error = nil;
|
|
|
|
if ([device lockForConfiguration:&error])
|
|
|
|
{
|
|
|
|
if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode])
|
|
|
|
{
|
|
|
|
[device setFocusMode:focusMode];
|
|
|
|
[device setFocusPointOfInterest:point];
|
|
|
|
}
|
|
|
|
if ([device isExposurePointOfInterestSupported] && [device isExposureModeSupported:exposureMode])
|
|
|
|
{
|
|
|
|
[device setExposureMode:exposureMode];
|
|
|
|
[device setExposurePointOfInterest:point];
|
|
|
|
}
|
|
|
|
[device setSubjectAreaChangeMonitoringEnabled:monitorSubjectAreaChange];
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"%@", error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
@end
|