@@ -5,12 +5,8 @@ import { CameraViewBase, autoFocusProperty, flashModeProperty } from './index.co
55class ProcessRawVideoSampleBufferDelegateImpl extends NSObject implements ProcessRawVideoSampleBufferDelegate {
66 cameraViewRenderToCustomContextWithImageBufferOnQueue ( cameraView : NSCameraView , imageBuffer : any , queue : NSObject ) : void { }
77 cameraViewWillProcessRawVideoSampleBufferOnQueue ( cameraView : NSCameraView , sampleBuffer : any , queue : NSObject ) : void {
8- try {
9- const owner = this . _owner ?. get ( ) ;
10- owner ?. notify ( { eventName : 'frame' , object : owner , cameraView, sampleBuffer, queue } ) ;
11- } catch ( err ) {
12- console . log ( 'process error' , err , err . stack ) ;
13- }
8+ const owner = this . _owner ?. get ( ) ;
9+ this . _owner ?. get ( ) ?. notify ( { eventName : 'frame' , object : owner , cameraView, sampleBuffer, queue } ) ;
1410 }
1511 _owner : WeakRef < CameraView > ;
1612 public static ObjCProtocols = [ ProcessRawVideoSampleBufferDelegate ] ;
@@ -21,15 +17,79 @@ class ProcessRawVideoSampleBufferDelegateImpl extends NSObject implements Proces
2117 return delegate ;
2218 }
2319}
20+ @NativeClass
21+ class NSCameraViewVideoDelegateImpl extends NSObject implements NSCameraViewVideoDelegate {
22+ cameraViewDidCompletePhotoCaptureFromVideoFrame ( cameraView : NSCameraView , photoDict : any ) : void {
23+ this . _owner ?. get ( ) ?. cameraViewDidCompletePhotoCaptureFromVideoFrame ( photoDict ) ;
24+ }
25+
26+ _owner : WeakRef < CameraView > ;
27+ public static ObjCProtocols = [ NSCameraViewVideoDelegate ] ;
28+
29+ static initWithOwner ( owner : CameraView ) {
30+ const delegate = NSCameraViewVideoDelegateImpl . new ( ) as NSCameraViewVideoDelegateImpl ;
31+ delegate . _owner = new WeakRef ( owner ) ;
32+ return delegate ;
33+ }
34+ }
35+ @NativeClass
36+ class NSCameraViewPhotoDelegateImpl extends NSObject implements NSCameraViewPhotoDelegate {
37+ cameraViewDidCapturePhotoWithConfiguration ( cameraView : NSCameraView , photoConfiguration : any ) : void { }
38+ cameraViewDidFinishProcessingPhotoPhotoDictPhotoConfiguration (
39+ cameraView : NSCameraView ,
40+ photo : AVCapturePhoto ,
41+ photoDict : NSDictionary < string , any > ,
42+ photoConfiguration : NSCameraViewPhotoConfiguration
43+ ) {
44+ this . _owner ?. get ( ) ?. cameraViewDidFinishProcessingPhotoPhotoDictPhotoConfiguration ( photo , photoDict ) ;
45+ }
46+
47+ _owner : WeakRef < CameraView > ;
48+ public static ObjCProtocols = [ NSCameraViewPhotoDelegate ] ;
49+
50+ static initWithOwner ( owner : CameraView ) {
51+ const delegate = NSCameraViewPhotoDelegateImpl . new ( ) as NSCameraViewPhotoDelegateImpl ;
52+ delegate . _owner = new WeakRef ( owner ) ;
53+ return delegate ;
54+ }
55+ }
2456
2557export class CameraView extends CameraViewBase {
58+ cameraViewDidFinishProcessingPhotoPhotoDictPhotoConfiguration ( photo : AVCapturePhoto , photoDict : any ) {
59+ const cgImage = photo . CGImageRepresentation ( ) ;
60+ const orientation = photo . metadata . objectForKey ( kCGImagePropertyOrientation ) ;
61+ const image = UIImage . imageWithCGImageScaleOrientation ( cgImage , 1 , orientation ) ;
62+ this . photoCaptureListener . forEach ( ( c ) => c ( image , photoDict ) ) ;
63+ }
64+ cameraViewDidProcessPhotoCaptureWithPhotoConfiguration ( photoDict : any ) { }
65+ videoCaptureListener = new Set < Function > ( ) ;
66+ photoCaptureListener = new Set < Function > ( ) ;
67+ cameraViewDidCompletePhotoCaptureFromVideoFrame ( photoDict : any ) {
68+ this . videoCaptureListener . forEach ( ( c ) => c ( photoDict ) ) ;
69+ }
2670 nativeViewProtected : NSCameraView ;
2771 _processor : ProcessRawVideoSampleBufferDelegate ;
72+ videoDelegate : NSCameraViewVideoDelegate ;
73+ photoDelegate : NSCameraViewPhotoDelegate ;
2874 createNativeView ( ) {
2975 return NSCameraView . alloc ( ) . initWithFrame ( CGRectZero ) ;
3076 }
3177 private _frameChangeCount = 0 ;
3278
79+ initNativeView ( ) : void {
80+ super . initNativeView ( ) ;
81+ const nativeView = this . nativeViewProtected ;
82+ nativeView . photoDelegate = this . photoDelegate = NSCameraViewPhotoDelegateImpl . initWithOwner ( this ) ;
83+ nativeView . videoDelegate = this . videoDelegate = NSCameraViewVideoDelegateImpl . initWithOwner ( this ) ;
84+ }
85+ disposeNativeView ( ) {
86+ this . stopPreview ( ) ;
87+ this . detachProcessor ( ) ;
88+ const nativeView = this . nativeViewProtected ;
89+ nativeView . videoDelegate = this . videoDelegate = null ;
90+ nativeView . photoDelegate = this . photoDelegate = null ;
91+ super . disposeNativeView ( ) ;
92+ }
3393 get processor ( ) {
3494 return this . _processor ;
3595 }
@@ -88,12 +148,6 @@ export class CameraView extends CameraViewBase {
88148 this . nativeViewProtected . processingDelegate = null ;
89149 super . onUnloaded ( ) ;
90150 }
91- disposeNativeView ( ) {
92- this . stopPreview ( ) ;
93- this . detachProcessor ( ) ;
94-
95- super . disposeNativeView ( ) ;
96- }
97151 previewStarted = false ;
98152 startPreview ( ) {
99153 if ( this . previewStarted ) {
@@ -110,15 +164,39 @@ export class CameraView extends CameraViewBase {
110164 this . nativeViewProtected ?. stopPreview ( ) ;
111165 }
112166 focusAtPoint ( x , y ) {
113- this . nativeViewProtected ?. nextLevel ?. focusAtAdjustedPointOfInterest ( CGPointMake ( x , y ) ) ;
167+ this . nativeViewProtected ?. focusAtAdjustedPointOfInterest ( CGPointMake ( x , y ) ) ;
114168 }
115169 async takePicture ( options : TakePictureOptions = { } ) {
116- throw new Error ( 'Method not implemented.' ) ;
117- // this.nativeViewProtected?.nextLevel?.capturePhoto();
170+ return new Promise ( ( resolve , reject ) => {
171+ try {
172+ // if (!this.nativeViewProtected.canCaptureVideo) {
173+ // return reject(new Error('this device cant capture photo: ' + this.nativeViewProtected.canCaptureVideo));
174+ // }
175+ // const onPhoto = (photoDict: NSDictionary<any, any>) => {
176+ // this.videoCaptureListener.delete(onPhoto);
177+ // const photoData = photoDict.objectForKey('NextLevelPhotoJPEGKey');
178+ // console.log('photoData', photoData, photoDict);
179+ // resolve({ image: new UIImage({ data: photoData }) });
180+ // };
181+ // this.videoCaptureListener.add(onPhoto);
182+ // console.log('capturePhotoFromVideo');
183+ // this.nativeViewProtected?.capturePhotoFromVideo();
184+ if ( ! this . nativeViewProtected . canCapturePhoto ) {
185+ return reject ( new Error ( 'this device cant capture photo: ' + this . nativeViewProtected . canCapturePhoto ) ) ;
186+ }
187+ const onPhoto = ( image , photoDict : NSDictionary < any , any > ) => {
188+ resolve ( { image } ) ;
189+ } ;
190+ this . photoCaptureListener . add ( onPhoto ) ;
191+ this . nativeViewProtected ?. capturePhoto ( ) ;
192+ } catch ( error ) {
193+ reject ( error ) ;
194+ }
195+ } ) ;
118196 }
119197
120198 toggleCamera ( ) {
121- throw new Error ( 'Method not implemented.' ) ;
199+ this . nativeViewProtected . toggleCamera ( ) ;
122200 }
123201
124202 [ flashModeProperty . setNative ] ( value : string | number ) {
0 commit comments