From 296f1906459ce1610dcc515d9e0e0c2f673b86dc Mon Sep 17 00:00:00 2001 From: Lochlan Wansbrough Date: Tue, 31 Mar 2015 18:02:57 -0700 Subject: [PATCH] Pushing files --- Camera.ios.js | 61 +++++++ RCTCamera.h | 11 ++ RCTCamera.m | 85 ++++++++++ RCTCamera.xcodeproj/project.pbxproj | 248 ++++++++++++++++++++++++++++ RCTCameraManager.h | 4 + RCTCameraManager.m | 27 +++ README.md | 41 +++++ ViewfinderView.h | 9 + ViewfinderView.m | 21 +++ package.json | 10 ++ 10 files changed, 517 insertions(+) create mode 100644 Camera.ios.js create mode 100644 RCTCamera.h create mode 100644 RCTCamera.m create mode 100644 RCTCamera.xcodeproj/project.pbxproj create mode 100644 RCTCameraManager.h create mode 100644 RCTCameraManager.m create mode 100644 README.md create mode 100644 ViewfinderView.h create mode 100644 ViewfinderView.m create mode 100644 package.json diff --git a/Camera.ios.js b/Camera.ios.js new file mode 100644 index 0000000..57d2426 --- /dev/null +++ b/Camera.ios.js @@ -0,0 +1,61 @@ +var React = require('React'); +var NativeModules = require('NativeModules'); +var ReactIOSViewAttributes = require('ReactIOSViewAttributes'); +var StyleSheet = require('StyleSheet'); +var createReactIOSNativeComponentClass = require('createReactIOSNativeComponentClass'); +var PropTypes = require('ReactPropTypes'); +var StyleSheetPropType = require('StyleSheetPropType'); +var NativeMethodsMixin = require('NativeMethodsMixin'); +var flattenStyle = require('flattenStyle'); +var merge = require('merge'); + +var Camera = React.createClass({ + propTypes: { + orientation: PropTypes.integer, + }, + + mixins: [NativeMethodsMixin], + + viewConfig: { + uiViewClassName: 'UIView', + validAttributes: ReactIOSViewAttributes.UIView + }, + + getInitialState: function() { + return { + isAuthorized: false + }; + }, + + componentWillMount: function() { + NativeModules.CameraManager.checkDeviceAuthorizationStatus((function(err, isAuthorized) { + this.state.isAuthorized = isAuthorized; + this.setState(this.state); + }).bind(this)); + }, + + render: function() { + var style = flattenStyle([styles.base, this.props.style]); + var orientation = this.props.orientation; + + var nativeProps = merge(this.props, { + style, + orientation: orientation, + }); + + return + }, +}); + +var RCTCamera = createReactIOSNativeComponentClass({ + validAttributes: merge(ReactIOSViewAttributes.UIView, { orientation: true }), + uiViewClassName: 'RCTCamera', +}); + +var styles = StyleSheet.create({ + base: { + overflow: 'hidden' + }, +}); + +module.exports = Camera; diff --git a/RCTCamera.h b/RCTCamera.h new file mode 100644 index 0000000..d1eca9a --- /dev/null +++ b/RCTCamera.h @@ -0,0 +1,11 @@ +#import +#import "ViewfinderView.h" + +@class AVCaptureSession; + +@interface RCTCamera : UIView + +@property (nonatomic) AVCaptureSession *session; +@property (nonatomic) ViewfinderView *viewfinder; + +@end \ No newline at end of file diff --git a/RCTCamera.m b/RCTCamera.m new file mode 100644 index 0000000..dfad7f0 --- /dev/null +++ b/RCTCamera.m @@ -0,0 +1,85 @@ +#import "RCTCamera.h" +#import "RCTLog.h" +#import "ViewfinderView.h" +#import + +@implementation RCTCamera + +- (void)setSrc:(NSString *)source +{ + +} + +- (id)init +{ + if ((self = [super init])) { + + _viewfinder = [[ViewfinderView alloc] init]; + AVCaptureSession *session = [[AVCaptureSession alloc] init]; + + [[self viewfinder] setSession:session]; + [self addSubview:_viewfinder]; + + NSError *error = nil; + + NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; + AVCaptureDevice *captureDevice = [devices firstObject]; + + AVCaptureDevicePosition position = AVCaptureDevicePositionBack; + AVCaptureVideoOrientation interfaceOrientation = (AVCaptureVideoOrientation)UIInterfaceOrientationMaskLandscape; + + for (AVCaptureDevice *device in devices) + { + if ([device position] == position) + { + captureDevice = device; + break; + } + } + + AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; + + if (error) + { + NSLog(@"%@", error); + } + + if ([session canAddInput:captureDeviceInput]) + { + [session addInput:captureDeviceInput]; + +// [[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)interfaceOrientation]; + [(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:AVLayerVideoGravityResizeAspectFill]; + } + + [session startRunning]; + + } + return self; +} + +- (NSArray *)reactSubviews +{ + NSArray *subviews = @[_viewfinder]; + return subviews; +} + +- (void)layoutSubviews +{ + [super layoutSubviews]; + _viewfinder.frame = self.bounds; +} + +- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex +{ + RCTLogError(@"Camera does not support subviews"); + return; +} + +- (void)removeReactSubview:(UIView *)subview +{ + RCTLogError(@"Camera does not support subviews"); + return; +} + +@end diff --git a/RCTCamera.xcodeproj/project.pbxproj b/RCTCamera.xcodeproj/project.pbxproj new file mode 100644 index 0000000..aa4a36c --- /dev/null +++ b/RCTCamera.xcodeproj/project.pbxproj @@ -0,0 +1,248 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 4107014D1ACB732B00C6AA39 /* RCTCamera.m in Sources */ = {isa = PBXBuildFile; fileRef = 410701481ACB732B00C6AA39 /* RCTCamera.m */; }; + 4107014E1ACB732B00C6AA39 /* RCTCameraManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */; }; + 4107014F1ACB732B00C6AA39 /* ViewfinderView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4107014C1ACB732B00C6AA39 /* ViewfinderView.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 4107012D1ACB723B00C6AA39 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = "include/$(PRODUCT_NAME)"; + dstSubfolderSpec = 16; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 4107012F1ACB723B00C6AA39 /* libRCTCamera.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTCamera.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 410701471ACB732B00C6AA39 /* RCTCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTCamera.h; sourceTree = ""; }; + 410701481ACB732B00C6AA39 /* RCTCamera.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCamera.m; sourceTree = ""; }; + 410701491ACB732B00C6AA39 /* RCTCameraManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTCameraManager.h; sourceTree = ""; }; + 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCameraManager.m; sourceTree = ""; }; + 4107014B1ACB732B00C6AA39 /* ViewfinderView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewfinderView.h; sourceTree = ""; }; + 4107014C1ACB732B00C6AA39 /* ViewfinderView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewfinderView.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 4107012C1ACB723B00C6AA39 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 410701241ACB719800C6AA39 = { + isa = PBXGroup; + children = ( + 410701471ACB732B00C6AA39 /* RCTCamera.h */, + 410701481ACB732B00C6AA39 /* RCTCamera.m */, + 410701491ACB732B00C6AA39 /* RCTCameraManager.h */, + 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */, + 4107014B1ACB732B00C6AA39 /* ViewfinderView.h */, + 4107014C1ACB732B00C6AA39 /* ViewfinderView.m */, + 410701301ACB723B00C6AA39 /* Products */, + ); + sourceTree = ""; + }; + 410701301ACB723B00C6AA39 /* Products */ = { + isa = PBXGroup; + children = ( + 4107012F1ACB723B00C6AA39 /* libRCTCamera.a */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 4107012E1ACB723B00C6AA39 /* RCTCamera */ = { + isa = PBXNativeTarget; + buildConfigurationList = 410701411ACB723B00C6AA39 /* Build configuration list for PBXNativeTarget "RCTCamera" */; + buildPhases = ( + 4107012B1ACB723B00C6AA39 /* Sources */, + 4107012C1ACB723B00C6AA39 /* Frameworks */, + 4107012D1ACB723B00C6AA39 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = RCTCamera; + productName = RCTCamera; + productReference = 4107012F1ACB723B00C6AA39 /* libRCTCamera.a */; + productType = "com.apple.product-type.library.static"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 410701251ACB719800C6AA39 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0620; + TargetAttributes = { + 4107012E1ACB723B00C6AA39 = { + CreatedOnToolsVersion = 6.2; + }; + }; + }; + buildConfigurationList = 410701281ACB719800C6AA39 /* Build configuration list for PBXProject "RCTCamera" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 410701241ACB719800C6AA39; + productRefGroup = 410701301ACB723B00C6AA39 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 4107012E1ACB723B00C6AA39 /* RCTCamera */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 4107012B1ACB723B00C6AA39 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 4107014E1ACB732B00C6AA39 /* RCTCameraManager.m in Sources */, + 4107014D1ACB732B00C6AA39 /* RCTCamera.m in Sources */, + 4107014F1ACB732B00C6AA39 /* ViewfinderView.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 410701291ACB719800C6AA39 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + }; + name = Debug; + }; + 4107012A1ACB719800C6AA39 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + }; + name = Release; + }; + 410701421ACB723B00C6AA39 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 8.2; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + OTHER_LDFLAGS = "-ObjC"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SKIP_INSTALL = YES; + }; + name = Debug; + }; + 410701431ACB723B00C6AA39 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 8.2; + MTL_ENABLE_DEBUG_INFO = NO; + OTHER_LDFLAGS = "-ObjC"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SKIP_INSTALL = YES; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 410701281ACB719800C6AA39 /* Build configuration list for PBXProject "RCTCamera" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 410701291ACB719800C6AA39 /* Debug */, + 4107012A1ACB719800C6AA39 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 410701411ACB723B00C6AA39 /* Build configuration list for PBXNativeTarget "RCTCamera" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 410701421ACB723B00C6AA39 /* Debug */, + 410701431ACB723B00C6AA39 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = 410701251ACB719800C6AA39 /* Project object */; +} diff --git a/RCTCameraManager.h b/RCTCameraManager.h new file mode 100644 index 0000000..a40943f --- /dev/null +++ b/RCTCameraManager.h @@ -0,0 +1,4 @@ +#import "RCTViewManager.h" + +@interface RCTCameraManager : RCTViewManager +@end \ No newline at end of file diff --git a/RCTCameraManager.m b/RCTCameraManager.m new file mode 100644 index 0000000..f61741c --- /dev/null +++ b/RCTCameraManager.m @@ -0,0 +1,27 @@ +#import "RCTCameraManager.h" +#import "RCTCamera.h" +#import "RCTBridge.h" +#import + +@implementation RCTCameraManager + +@synthesize bridge = _bridge; + +- (UIView *)view +{ + return [[RCTCamera alloc] init]; +} + +RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger); + +- (void)checkDeviceAuthorizationStatus:(RCTResponseSenderBlock) callback +{ + RCT_EXPORT(); + NSString *mediaType = AVMediaTypeVideo; + + [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) { + callback(@[[NSNull null], @(granted)]); + }]; +} + +@end diff --git a/README.md b/README.md new file mode 100644 index 0000000..12c946c --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# react-native-camera +----------- +A camera viewport for React Native. This module is currently in the very early stages of development and does not support image capture at this time, though it is coming. + +## Getting started + +1. Fetch from NPM: `npm install react-native-camera --save` +2. In XCode, right click `Libraries` and `Add Files to ______` +3. Add `libRCTCamera.a` to `Build Phases -> Link Binary With Libraries` + +## Usage + +All you need is to `require` the `react-native-camera` module and then use the +`` tag. + +``` +var React = require('react-native'); +var { + AppRegistry, + StyleSheet, + Text, + View, +} = React; +var Camera = require('react-native-camera'); + +var cameraApp = React.createClass({ + render: function() { + return ( + + + + ); + } +}); + +AppRegistry.registerComponent('cameraApp', () => cameraApp); +``` + +------------ + +Thanks to Brent Vatne (@brentvatne) for the `react-native-video` module which provided me with a great example of how to set up this module. diff --git a/ViewfinderView.h b/ViewfinderView.h new file mode 100644 index 0000000..c7415e9 --- /dev/null +++ b/ViewfinderView.h @@ -0,0 +1,9 @@ +#import + +@class AVCaptureSession; + +@interface ViewfinderView : UIView + +@property (nonatomic) AVCaptureSession *session; + +@end diff --git a/ViewfinderView.m b/ViewfinderView.m new file mode 100644 index 0000000..fd6e14a --- /dev/null +++ b/ViewfinderView.m @@ -0,0 +1,21 @@ +#import "ViewfinderView.h" +#import + +@implementation ViewfinderView + ++ (Class)layerClass +{ + return [AVCaptureVideoPreviewLayer class]; +} + +- (AVCaptureSession *)session +{ + return [(AVCaptureVideoPreviewLayer *)[self layer] session]; +} + +- (void)setSession:(AVCaptureSession *)session +{ + [(AVCaptureVideoPreviewLayer *)[self layer] setSession:session]; +} + +@end \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..03fa20d --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "react-native-camera", + "version": "0.0.1", + "description": "A element for React Native", + "main": "Camera.ios.js", + "author": "Lochlan Wansbrough (http://lwansbrough.com)", + "dependencies": { + "react-native": "^0.3.1" + } +}