mirror of
https://github.com/status-im/react-native-camera.git
synced 2025-02-24 01:38:18 +00:00
whitespace
This commit is contained in:
parent
65b5033596
commit
1d437adb88
@ -78,24 +78,24 @@ RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
|
||||
}
|
||||
|
||||
- (id)init {
|
||||
|
||||
|
||||
if ((self = [super init])) {
|
||||
|
||||
|
||||
self.session = [AVCaptureSession new];
|
||||
self.session.sessionPreset = AVCaptureSessionPresetHigh;
|
||||
|
||||
|
||||
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
|
||||
self.previewLayer.needsDisplayOnBoundsChange = YES;
|
||||
|
||||
|
||||
self.sessionQueue = dispatch_queue_create("cameraManagerQueue", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
|
||||
dispatch_async(self.sessionQueue, ^{
|
||||
|
||||
|
||||
|
||||
|
||||
if (self.presetCamera == AVCaptureDevicePositionUnspecified) {
|
||||
self.presetCamera = AVCaptureDevicePositionBack;
|
||||
}
|
||||
|
||||
|
||||
AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
|
||||
if ([self.session canAddOutput:stillImageOutput])
|
||||
{
|
||||
@ -103,14 +103,14 @@ RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
|
||||
[self.session addOutput:stillImageOutput];
|
||||
self.stillImageOutput = stillImageOutput;
|
||||
}
|
||||
|
||||
|
||||
AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
|
||||
if ([self.session canAddOutput:movieFileOutput])
|
||||
{
|
||||
[self.session addOutput:movieFileOutput];
|
||||
self.movieFileOutput = movieFileOutput;
|
||||
}
|
||||
|
||||
|
||||
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
|
||||
if ([self.session canAddOutput:metadataOutput]) {
|
||||
[metadataOutput setMetadataObjectsDelegate:self queue:self.sessionQueue];
|
||||
@ -118,7 +118,7 @@ RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
|
||||
[metadataOutput setMetadataObjectTypes:metadataOutput.availableMetadataObjectTypes];
|
||||
self.metadataOutput = metadataOutput;
|
||||
}
|
||||
|
||||
|
||||
__weak RCTCameraManager *weakSelf = self;
|
||||
[self setRuntimeErrorHandlingObserver:[NSNotificationCenter.defaultCenter addObserverForName:AVCaptureSessionRuntimeErrorNotification object:self.session queue:nil usingBlock:^(NSNotification *note) {
|
||||
RCTCameraManager *strongSelf = weakSelf;
|
||||
@ -127,7 +127,7 @@ RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
|
||||
[strongSelf.session startRunning];
|
||||
});
|
||||
}]];
|
||||
|
||||
|
||||
[self.session startRunning];
|
||||
});
|
||||
}
|
||||
@ -137,7 +137,7 @@ RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
|
||||
RCT_EXPORT_METHOD(checkDeviceAuthorizationStatus:(RCTResponseSenderBlock) callback)
|
||||
{
|
||||
NSString *mediaType = AVMediaTypeVideo;
|
||||
|
||||
|
||||
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
|
||||
callback(@[[NSNull null], @(granted)]);
|
||||
}];
|
||||
@ -148,32 +148,32 @@ RCT_EXPORT_METHOD(changeCamera:(NSInteger)camera) {
|
||||
AVCaptureDevice *currentCaptureDevice = [self.videoCaptureDeviceInput device];
|
||||
AVCaptureDevicePosition position = (AVCaptureDevicePosition)camera;
|
||||
AVCaptureDevice *captureDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:(AVCaptureDevicePosition)position];
|
||||
|
||||
|
||||
if (captureDevice == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
self.presetCamera = camera;
|
||||
|
||||
|
||||
NSError *error = nil;
|
||||
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
|
||||
|
||||
|
||||
if (error || captureDeviceInput == nil)
|
||||
{
|
||||
NSLog(@"%@", error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
[self.session beginConfiguration];
|
||||
|
||||
|
||||
[self.session removeInput:self.videoCaptureDeviceInput];
|
||||
|
||||
|
||||
if ([self.session canAddInput:captureDeviceInput])
|
||||
{
|
||||
[self.session addInput:captureDeviceInput];
|
||||
|
||||
|
||||
[NSNotificationCenter.defaultCenter removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:currentCaptureDevice];
|
||||
|
||||
|
||||
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice];
|
||||
self.videoCaptureDeviceInput = captureDeviceInput;
|
||||
}
|
||||
@ -181,7 +181,7 @@ RCT_EXPORT_METHOD(changeCamera:(NSInteger)camera) {
|
||||
{
|
||||
[self.session addInput:self.videoCaptureDeviceInput];
|
||||
}
|
||||
|
||||
|
||||
[self.session commitConfiguration];
|
||||
});
|
||||
}
|
||||
@ -193,7 +193,7 @@ RCT_EXPORT_METHOD(changeAspect:(NSString *)aspect) {
|
||||
RCT_EXPORT_METHOD(changeFlashMode:(NSInteger)flashMode) {
|
||||
AVCaptureDevice *device = [self.videoCaptureDeviceInput device];
|
||||
NSError *error = nil;
|
||||
|
||||
|
||||
if (![device hasFlash]) return;
|
||||
if (![device lockForConfiguration:&error]) {
|
||||
NSLog(@"%@", error);
|
||||
@ -210,7 +210,7 @@ RCT_EXPORT_METHOD(changeOrientation:(NSInteger)orientation) {
|
||||
RCT_EXPORT_METHOD(changeTorchMode:(NSInteger)torchMode) {
|
||||
AVCaptureDevice *device = [self.videoCaptureDeviceInput device];
|
||||
NSError *error = nil;
|
||||
|
||||
|
||||
if (![device hasTorch]) return;
|
||||
if (![device lockForConfiguration:&error]) {
|
||||
NSLog(@"%@", error);
|
||||
@ -223,7 +223,7 @@ RCT_EXPORT_METHOD(changeTorchMode:(NSInteger)torchMode) {
|
||||
RCT_EXPORT_METHOD(capture:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) {
|
||||
NSInteger captureMode = [[options valueForKey:@"mode"] intValue];
|
||||
NSInteger captureTarget = [[options valueForKey:@"target"] intValue];
|
||||
|
||||
|
||||
if (captureMode == RCTCameraCaptureModeStill) {
|
||||
[self captureStill:captureTarget options:options callback:callback];
|
||||
}
|
||||
@ -245,26 +245,26 @@ RCT_EXPORT_METHOD(stopCapture) {
|
||||
if (captureDevice == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
|
||||
|
||||
|
||||
if (error || captureDeviceInput == nil) {
|
||||
NSLog(@"%@", error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
[self.session beginConfiguration];
|
||||
|
||||
|
||||
if (type == AVMediaTypeAudio) {
|
||||
[self.session removeInput:self.audioCaptureDeviceInput];
|
||||
}
|
||||
else if (type == AVMediaTypeVideo) {
|
||||
[self.session removeInput:self.videoCaptureDeviceInput];
|
||||
}
|
||||
|
||||
|
||||
if ([self.session canAddInput:captureDeviceInput]) {
|
||||
[self.session addInput:captureDeviceInput];
|
||||
|
||||
|
||||
if (type == AVMediaTypeAudio) {
|
||||
self.audioCaptureDeviceInput = captureDeviceInput;
|
||||
}
|
||||
@ -272,27 +272,27 @@ RCT_EXPORT_METHOD(stopCapture) {
|
||||
self.videoCaptureDeviceInput = captureDeviceInput;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[self.session commitConfiguration];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)captureStill:(NSInteger)target options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback {
|
||||
if ([[[UIDevice currentDevice].model lowercaseString] rangeOfString:@"simulator"].location != NSNotFound){
|
||||
|
||||
|
||||
CGSize size = CGSizeMake(720, 1280);
|
||||
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
|
||||
[[UIColor whiteColor] setFill];
|
||||
UIRectFill(CGRectMake(0, 0, size.width, size.height));
|
||||
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
|
||||
[self storeImage:image target:target callback:callback];
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
[[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:self.previewLayer.connection.videoOrientation];
|
||||
|
||||
|
||||
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
|
||||
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
|
||||
UIImage *image = [UIImage imageWithData:imageData];
|
||||
@ -309,9 +309,9 @@ RCT_EXPORT_METHOD(stopCapture) {
|
||||
|
||||
- (void)storeImage:(UIImage*)image target:(NSInteger)target callback:(RCTResponseSenderBlock)callback {
|
||||
UIImage *rotatedImage = [image resizedImage:CGSizeMake(image.size.width, image.size.height) interpolationQuality:kCGInterpolationDefault];
|
||||
|
||||
|
||||
NSString *responseString;
|
||||
|
||||
|
||||
if (target == RCTCameraCaptureTargetMemory) {
|
||||
responseString = [UIImageJPEGRepresentation(rotatedImage, 1.0) base64EncodedStringWithOptions:0];
|
||||
}
|
||||
@ -333,26 +333,26 @@ RCT_EXPORT_METHOD(stopCapture) {
|
||||
}
|
||||
|
||||
-(void)captureVideo:(NSInteger)target options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback {
|
||||
|
||||
|
||||
if (self.movieFileOutput.recording) {
|
||||
callback(@[RCTMakeError(@"Already Recording", nil, nil)]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if ([options valueForKey:@"audio"]) {
|
||||
[self initializeCaptureSessionInput:AVMediaTypeAudio];
|
||||
}
|
||||
|
||||
|
||||
Float64 totalSeconds = [[options valueForKey:@"totalSeconds"] floatValue];
|
||||
if (totalSeconds > -1) {
|
||||
int32_t preferredTimeScale = [[options valueForKey:@"preferredTimeScale"] intValue];
|
||||
CMTime maxDuration = CMTimeMakeWithSeconds(totalSeconds, preferredTimeScale);
|
||||
self.movieFileOutput.maxRecordedDuration = maxDuration;
|
||||
}
|
||||
|
||||
|
||||
dispatch_async(self.sessionQueue, ^{
|
||||
[[self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:self.previewLayer.connection.videoOrientation];
|
||||
|
||||
|
||||
//Create temporary URL to record to
|
||||
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
|
||||
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
|
||||
@ -364,10 +364,10 @@ RCT_EXPORT_METHOD(stopCapture) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Start recording
|
||||
[self.movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
|
||||
|
||||
|
||||
self.videoCallback = callback;
|
||||
self.videoTarget = target;
|
||||
});
|
||||
@ -378,7 +378,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
fromConnections:(NSArray *)connections
|
||||
error:(NSError *)error
|
||||
{
|
||||
|
||||
|
||||
BOOL recordSuccess = YES;
|
||||
if ([error code] != noErr) {
|
||||
// A problem occurred: Find out if the recording was successful.
|
||||
@ -391,7 +391,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
self.videoCallback(@[RCTMakeError(@"Error while recording", nil, nil)]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (self.videoTarget == RCTCameraCaptureTargetCameraRoll) {
|
||||
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
|
||||
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL]) {
|
||||
@ -401,7 +401,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
self.videoCallback(@[RCTMakeError(error.description, nil, nil)]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
self.videoCallback(@[[NSNull null], [assetURL absoluteString]]);
|
||||
}];
|
||||
}
|
||||
@ -410,10 +410,10 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||||
NSString *documentsDirectory = [paths firstObject];
|
||||
NSString *fullPath = [[documentsDirectory stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]] stringByAppendingPathExtension:@"mov"];
|
||||
|
||||
|
||||
NSFileManager * fileManager = [NSFileManager defaultManager];
|
||||
NSError * error = nil;
|
||||
|
||||
|
||||
//copying destination
|
||||
if (!([fileManager copyItemAtPath:[outputFileURL path] toPath:fullPath error:&error])) {
|
||||
self.videoCallback(@[RCTMakeError(error.description, nil, nil)]);
|
||||
@ -429,17 +429,17 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
- (NSString *)saveImage:(UIImage *)image withName:(NSString *)name {
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||||
NSString *documentsDirectory = [paths firstObject];
|
||||
|
||||
|
||||
NSData *data = UIImageJPEGRepresentation(image, 1.0);
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSString *fullPath = [[documentsDirectory stringByAppendingPathComponent:name] stringByAppendingPathExtension:@"jpg"];
|
||||
|
||||
|
||||
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
|
||||
|
||||
|
||||
NSArray *barcodeTypes = @[
|
||||
AVMetadataObjectTypeUPCECode,
|
||||
AVMetadataObjectTypeCode39Code,
|
||||
@ -452,11 +452,11 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
AVMetadataObjectTypeQRCode,
|
||||
AVMetadataObjectTypeAztecCode
|
||||
];
|
||||
|
||||
|
||||
for (AVMetadataMachineReadableCodeObject *metadata in metadataObjects) {
|
||||
for (id barcodeType in barcodeTypes) {
|
||||
if (metadata.type == barcodeType) {
|
||||
|
||||
|
||||
[self.bridge.eventDispatcher sendDeviceEventWithName:@"CameraBarCodeRead"
|
||||
body:@{
|
||||
@"type": metadata.type,
|
||||
@ -482,7 +482,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
{
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType:mediaType];
|
||||
AVCaptureDevice *captureDevice = [devices firstObject];
|
||||
|
||||
|
||||
for (AVCaptureDevice *device in devices)
|
||||
{
|
||||
if ([device position] == position)
|
||||
@ -491,7 +491,7 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return captureDevice;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user