Skip to content

Instantly share code, notes, and snippets.

@heaversm
Created March 6, 2019 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heaversm/0e65c939b5e23ddcb88b3e6145a69db4 to your computer and use it in GitHub Desktop.
Save heaversm/0e65c939b5e23ddcb88b3e6145a69db4 to your computer and use it in GitHub Desktop.
#import "OpenCvCameraManager.h"
#import <React/RCTBridge.h>
#import <opencv2/videoio/cap_ios.h>
using namespace cv;
@implementation OpenCvCameraManager
@synthesize camera, img, cameraId;
RCT_EXPORT_VIEW_PROPERTY(onCodeDetected, RCTBubblingEventBlock);
RCT_CUSTOM_VIEW_PROPERTY(cameraId, NSInteger, RNTUIImageView)
{
if ([RCTConvert NSInteger:json] != self.camera.defaultAVCaptureDevicePosition ) {
[self hideCamera];
if ([RCTConvert NSInteger:json] == AVCaptureDevicePositionBack) {
self.cameraId = AVCaptureDevicePositionBack;
} else {
self.cameraId = AVCaptureDevicePositionFront;
}
[self displayCamera];
}
}
RCT_EXPORT_MODULE()
- (UIView *)view
{
NSLog(@"----------- init OpenCvCamera view ----------");
CGRect screenRect = [[UIScreen mainScreen] bounds];
self.img = [[RNTUIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
return img;
}
- (void)displayCamera {
// set orientation portrait
dispatch_async(dispatch_get_main_queue(), ^{
[self.img setHidden: NO];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
self.camera = [[CvVideoCamera alloc] initWithParentView:self.img];
self.camera.delegate = self;
self.camera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.camera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh;
self.camera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.camera.defaultFPS = 30;
self.camera.grayscaleMode = NO;
NSLog(@"----------- camera start ----------");
[self.camera start];
});
}
#pragma mark CvVideoCameraDelegate
- (void)layoutPreviewLayer;
{
NSLog(@"LayoutPreview");
//if (self.parentView != nil)
if (self.img != nil)
{
CALayer* layer = self.camera.customPreviewLayer;
CGRect bounds = self.camera.customPreviewLayer.bounds;
NSLog(@"[FixedCvVideoCamera]Custom Preview Layer bounds %fx%f", bounds.size.width, bounds.size.height);
float previewAspectRatio = bounds.size.height / bounds.size.width;
NSLog(@"[FixedCvVideoCamera]Preview aspect ratio %f", previewAspectRatio);
//int rotation_angle = 0;
//layer.position = CGPointMake(self.parentView.frame.size.width/2., self.parentView.frame.size.height/2.);
layer.position = CGPointMake(self.img.frame.size.width/2., self.img.frame.size.height/2.);
//layer.affineTransform = CGAffineTransformMakeRotation( DEGREES_RADIANS(rotation_angle) );
// Get video feed's resolutions
NSArray* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice* device = nil;
for (AVCaptureDevice *d in devices) {
// Get the default camera device - should be either front of back camera device
if ([d position] == self.defaultAVCaptureDevicePosition) {
device = d;
}
}
// Set the default device if not found
if (!device) {
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription);
CGSize resolution = CGSizeMake(dimensions.width, dimensions.height);
if (self.defaultAVCaptureVideoOrientation == AVCaptureVideoOrientationPortrait || self.defaultAVCaptureVideoOrientation == AVCaptureVideoOrientationPortraitUpsideDown) {
resolution = CGSizeMake(resolution.height, resolution.width);
}
NSLog(@"[FixedCvVideoCamera]Video feed resolution is %fx%f", resolution.width, resolution.height);
float videoFeedAspectRatio = resolution.height / resolution.width;
NSLog(@"[FixedCvVideoCamera]Video feed's aspect ratio is %f", videoFeedAspectRatio);
// Set layer bounds to ASPECT FILL by expanding either the width or the height
if (previewAspectRatio > videoFeedAspectRatio) {
NSLog(@"[FixedCvVideoCamera] Preview is more rectangular than the video feed aspect ratio. Expanding width to maintain aspect ratio.");
float newWidth = bounds.size.height / videoFeedAspectRatio;
layer.bounds = CGRectMake(0, 0, newWidth, bounds.size.height);
} else {
NSLog(@"[FixedCvVideoCamera] Preview is equally or less rectangular (wider) than the video feed's aspect ratio. Expanding height bound to maintain aspect ratio.");
float newHeight = bounds.size.width * videoFeedAspectRatio;
layer.bounds = CGRectMake(0, 0, bounds.size.width, newHeight);
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment