/
uFMain.pas
1318 lines (1176 loc) · 49.8 KB
/
uFMain.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit uFMain;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
Macapi.ObjectiveC, iOSapi.Foundation, Macapi.Dispatch, iOSapi.AVFoundation,
iOSapi.CoreMedia, iOSapi.CocoaTypes, iOSapi.UIKit, iOSapi.CoreImage,
SyncObjs, iOSapi.CoreGraphics, Macapi.CoreServices, Posix.SysTypes,
Macapi.CoreFoundation, iOSapi.QuartzCore, FMX.Layouts;
{$DEFINE CALLBACK_ERRORS} // Because of an error in XD5 update 2 second parameter of
// callback method is improperly initialized
type
{$M+}
TFMain = class;
VideoCaptureDelegate = interface(IObjectiveC)
['{95C26C24-9DB3-441A-A60D-A20E96BEF584}']
procedure observeValueForKeyPath(keyPath: NSString; ofObject: Pointer; change: NSDictionary; context: Pointer); cdecl;
procedure flashAnimationDidStop(animationID: NSString; finished: NSNumber; context: Pointer); cdecl;
end;
TVideoCaptureDelegate = class(TOCLocal, VideoCaptureDelegate, AVCaptureVideoDataOutputSampleBufferDelegate)
private
[Weak] FMain: TFMain;
FFlashView: UIView;
public
constructor Create(Main: TFMain);
procedure observeValueForKeyPath(keyPath: NSString; ofObject: Pointer; change: NSDictionary; context: Pointer); cdecl;
procedure captureOutput(captureOutput: AVCaptureOutput; didOutputSampleBuffer: CMSampleBufferRef; fromConnection: AVCaptureConnection); cdecl;
procedure flashAnimationDidStop(animationID: NSString; finished: NSNumber; context: Pointer); cdecl;
procedure FlashAnimation(Flash: Boolean);
end;
{ AVCaptureStillImageOutput }
TAVCaptureCompletionHandler = procedure(const ImageDataSampleBuffer: CMSampleBufferRef;
const Error: NSError) of object;
// Redeclared the CATransactionClass interface to add the
// 'captureStillImageAsynchronouslyFromConnection:completionHandler:' selector.
AVCaptureStillImageOutput = interface(AVCaptureOutput)
['{E0B5F87B-AFA2-4AF2-AA36-AA4E5480A9AC}']
function availableImageDataCVPixelFormatTypes: NSArray; cdecl;
function availableImageDataCodecTypes: NSArray; cdecl;
function isCapturingStillImage: Boolean; cdecl;
function outputSettings: NSDictionary; cdecl;
procedure setOutputSettings(outputSettings: NSDictionary); cdecl;
procedure captureStillImageAsynchronouslyFromConnection(connection: AVCaptureConnection;
completionHandler: TAVCaptureCompletionHandler); cdecl;
end;
TAVCaptureStillImageOutput = class(TOCGenericImport<AVCaptureStillImageOutputClass, AVCaptureStillImageOutput>) end;
TFMain = class(TForm)
tbPhone: TToolBar;
btnPhoneFront: TSpeedButton;
btnPhoneBack: TSpeedButton;
btnPhoneSnap: TSpeedButton;
chkPhoneFaces: TSwitch;
lblPhoneFaces: TLabel;
tbPad: TToolBar;
btnPadFront: TSpeedButton;
btnPadBack: TSpeedButton;
btnPadSnap: TSpeedButton;
chkPadFaces: TSwitch;
lblPadFaces: TLabel;
layPhoneFaces: TLayout;
cpError: TCalloutPanel;
lblError: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnFrontClick(Sender: TObject);
procedure btnBackClick(Sender: TObject);
procedure btnSnapClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure chkFacesSwitch(Sender: TObject);
procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
var Handled: Boolean);
private
{ Private declarations }
FIsUsingFrontFacingCamera: Boolean;
FVideoCaptureDelegate: TVideoCaptureDelegate;
FStillImageOutput: AVCaptureStillImageOutput;
FAVCaptureStillImageIsCapturingStillImageContext: NSString;
FVideoDataOutput: AVCaptureVideoDataOutput;
FVideoDataOutputQueue: dispatch_queue_t;
FEffectiveScale: CGFloat;
FPreviewArea: CALayer;
FPreviewLayer: AVCaptureVideoPreviewLayer;
FFaceDetector: CIDetector;
FDetectFaces: Boolean;
FSquare: UIImage;
FDoingFaceDetection: Boolean;
FCurDeviceOrientation: UIDeviceOrientation;
FDestinationData: CFMutableDataRef;
FLastDistance: Single;
FSetupDone: Boolean;
procedure SetupAvCapture;
procedure TeardownAVCapture;
procedure ObserveValueForKeyPath(keyPath: NSString; ofObject: Pointer; change: NSDictionary; context: Pointer);
procedure captureOutput(captureOutput: AVCaptureOutput; didOutputSampleBuffer: CMSampleBufferRef; fromConnection: AVCaptureConnection);
procedure DrawFaceBoxesForFeatures(Features: NSArray; Clap: CGRect; Orientation: UIDeviceOrientation);
function VideoPreviewBoxForGravity(Gravity: NSString; FrameSize: CGSize; ApertureSize: CGSize): CGRect;
procedure SwitchCameras(FrontFacingCamera: Boolean);
procedure ToggleFaceDetection(DetectFaces: Boolean);
procedure TakePicture;
function OrientationForDeviceOrientation(DeviceOrientation: UIDeviceOrientation): AVCaptureVideoOrientation;
procedure StillImageCaptured(const ImageDataSampleBuffer: CMSampleBufferRef; const Error: NSError);
{$IFNDEF CALLBACK_ERRORS}
procedure DisplayErrorOnMainQueue(Error: NSError; const Title: String);
{$ENDIF}
function CreateCGImageFromCVPixelBuffer(PixelBuffer: CVPixelBufferRef; var ImageOut: CGImageRef): OSStatus;
function NewSquareOverlayedImageForFeatures(Features: NSArray; BackgroundImage: CGImageRef; Orientation: UIDeviceOrientation; IsFrontFacing: Boolean): CGImageRef;
function WriteCGImageToCameraRoll(Image: CGImageRef; Metadata: NSDictionary): Boolean;
procedure WriteToPhotosCompletion(assetURL: NSURL; error: NSError);
procedure WriteToCameraRollCompletion(assetURL: NSURL; error: NSError);
public
{ Public declarations }
end;
dispatch_work_t = reference to procedure;
dispatch_function_t = procedure(context: Pointer); cdecl;
procedure dispatch_async_f(queue: dispatch_queue_t; context: Pointer; work: dispatch_function_t); cdecl; external libdispatch name _PU + 'dispatch_async_f';
procedure dispatch_async(queue: dispatch_queue_t; work: dispatch_work_t);
procedure dispatch_sync_f(queue: dispatch_queue_t; context: Pointer; work: dispatch_function_t); cdecl; external libdispatch name _PU + 'dispatch_sync_f';
procedure dispatch_sync(queue: dispatch_queue_t; work: dispatch_work_t);
function dispatch_get_main_queue: dispatch_queue_t;
// Definimos esta función porque no está definida en iOSapi.UIKit
procedure UIGraphicsBeginImageContext(size: CGSize); cdecl; external libUIKit name _PU + 'UIGraphicsBeginImageContext';
const
libImageIO = '/System/Library/Frameworks/ImageIO.framework/ImageIO';
// Definimos estas funciones porque no está accesible Macapi.ImageIO para iOS en Delphi XE5
function CGImageDestinationCreateWithData(data: CFMutableDataRef; type_: CFStringRef; count: Longword; options: CFDictionaryRef): CGImageDestinationRef; cdecl; external libImageIO name _PU + 'CGImageDestinationCreateWithData';
procedure CGImageDestinationAddImage(idst: CGImageDestinationRef; image: CGImageRef; properties: CFDictionaryRef); cdecl; external libImageIO name _PU + 'CGImageDestinationAddImage';
function CGImageDestinationFinalize(idst: CGImageDestinationRef): Integer; cdecl; external libImageIO name _PU + 'CGImageDestinationFinalize';
var
FMain: TFMain;
implementation
uses Math, Macapi.ObjCRuntime, iOSapi.CoreVideo, FMX.Platform.iOS,
iOSapi.AssetsLibrary, FMX.Helpers.iOS
{$IF defined(IOS) and NOT defined(CPUARM)}
, Posix.Dlfcn
{$ENDIF}
;
{$R *.fmx}
{ ALAssetsLibrary }
// Because of a bug in the XE5 Update2 definition of
// Class definition for ALAssetsLibrary is missing
type
TALAssetsLibrary = class(TOCGenericImport<ALAssetsLibraryClass, ALAssetsLibrary>) end;
{ AVCaptureDeviceInput }
// Because of a bug in the XE5 Update2 definition of
// deviceInputWithDevice and initWithDevice methods,
// we redefine here AVCaptureDeviceInput interfaces.
type
AVCaptureDeviceInputClass = interface(AVCaptureInputClass)
['{71E3EB0E-73B3-48C3-AEF4-306F29F39E5B}']
{class} function deviceInputWithDevice(device: AVCaptureDevice; error: PPointer): Pointer; cdecl;
end;
AVCaptureDeviceInput = interface(AVCaptureInput)
['{9D2445AE-EFDD-4374-A76D-D5790EDB19F4}']
function device: AVCaptureDevice; cdecl;
function initWithDevice(device: AVCaptureDevice; error: PPointer): Pointer; cdecl;
end;
TAVCaptureDeviceInput = class(TOCGenericImport<AVCaptureDeviceInputClass, AVCaptureDeviceInput>) end;
{ GCD implementation }
function dispatch_get_main_queue: dispatch_queue_t;
var
FwkMod: HMODULE;
begin
Result := 0;
FwkMod := LoadLibrary(PWideChar(libdispatch));
if FwkMod <> 0 then
begin
Result := dispatch_queue_t(GetProcAddress(FwkMod, PWideChar('_dispatch_main_q')));
FreeLibrary(FwkMod);
end;
end;
procedure DispatchCallback(context: Pointer); cdecl;
var
CallbackProc: dispatch_work_t absolute context;
begin
try
CallbackProc;
finally
IInterface(context)._Release;
end;
end;
procedure dispatch_async(queue: dispatch_queue_t; work: dispatch_work_t);
var
callback: Pointer absolute work;
begin
IInterface(callback)._AddRef;
dispatch_async_f(queue, callback, DispatchCallback);
end;
procedure dispatch_sync(queue: dispatch_queue_t; work: dispatch_work_t);
var
callback: Pointer absolute work;
begin
IInterface(callback)._AddRef;
dispatch_sync_f(queue, callback, DispatchCallback);
end;
{ TVideoCaptureDelegate }
constructor TVideoCaptureDelegate.Create(Main: TFMain);
begin
inherited Create;
FMain := Main;
end;
procedure TVideoCaptureDelegate.observeValueForKeyPath(keyPath: NSString; ofObject: Pointer; change: NSDictionary; context: Pointer);
begin
FMain.ObserveValueForKeyPath(keyPath, ofObject, change, context);
end;
procedure TVideoCaptureDelegate.captureOutput(captureOutput: AVCaptureOutput; didOutputSampleBuffer: CMSampleBufferRef; fromConnection: AVCaptureConnection);
begin
FMain.CaptureOutput(captureOutput, didOutputSampleBuffer, fromConnection);
end;
procedure TVideoCaptureDelegate.flashAnimationDidStop(animationID: NSString; finished: NSNumber; context: Pointer);
begin
FFlashView.removeFromSuperview;
FFlashView.release;
FFlashView := nil;
end;
procedure TVideoCaptureDelegate.FlashAnimation(Flash: Boolean);
const
iOS7TitlebarOffset = 20;
var
Rect: CGRect;
begin
if Flash then
begin
// Do flash bulb like animation
FFlashView := TUIView.Alloc;
Rect := WindowHandleToPlatform(FMain.Handle).View.frame;
if not (TOSVersion.Check(7, 0) or (FMain.BorderStyle = TFmxFormBorderStyle.bsNone)) then
Rect.origin.y := iOS7TitlebarOffset;
FFlashView := TUIView.Wrap(FFlashView.initWithFrame(Rect));
FFlashView.setBackgroundColor(TUIColor.Wrap(TUIColor.OCClass.whiteColor));
FFlashView.setAlpha(0.0);
WindowHandleToPlatform(FMain.Handle).View.window.addSubview(FFlashView);
TUIView.OCClass.beginAnimations(nil, nil);
try
TUIView.OCClass.setAnimationDuration(0.2);
TUIView.OCClass.setAnimationDelegate(GetObjectID);
FFlashView.setAlpha(1.0);
finally
TUIView.OCClass.commitAnimations;
end;
end
else
begin
TUIView.OCClass.beginAnimations(nil, nil);
try
TUIView.OCClass.setAnimationDuration(0.2);
TUIView.OCClass.setAnimationDelegate(GetObjectID);
TUIView.OCClass.setAnimationDidStopSelector(sel_getUid('flashAnimationDidStop:finished:context:'));
FFlashView.setAlpha(0.0);
finally
TUIView.OCClass.commitAnimations;
end;
end;
end;
{ TFMain }
procedure TFMain.FormActivate(Sender: TObject);
begin
if not FSetupDone then
begin
try
SetupAVCapture;
except
on E: Exception do
begin
lblError.Text := E.Message;
cpError.Visible := True;
tbPhone.Enabled := False;
tbPad.Enabled := False;
end;
end;
FSetupDone := True;
end;
end;
procedure TFMain.FormCreate(Sender: TObject);
function CIDetectorAccuracyLow: Pointer;
begin
Result := Pointer(CocoaPointerConst(libCoreImage, 'CIDetectorAccuracyLow')^);
end;
function CIDetectorAccuracy: Pointer;
begin
Result := Pointer(CocoaPointerConst(libCoreImage, 'CIDetectorAccuracy')^);
end;
function CIDetectorTypeFace: NSString;
begin
Result := CocoaNSStringConst(libCoreImage, 'CIDetectorTypeFace');
end;
function IsTablet: Boolean;
begin
Result := TUIDevice.Wrap(TUIDevice.OCClass.currentDevice).userInterfaceIdiom = UIUserInterfaceIdiomPad;
end;
var
DetectorOptions: NSDictionary;
Objects, Keys: array[0..0] of Pointer;
begin
tbPad.Visible := IsTablet;
tbPhone.Visible := not tbPad.Visible;
FVideoCaptureDelegate := TVideoCaptureDelegate.Create(Self);
FAVCaptureStillImageIsCapturingStillImageContext := NSSTR('AVCaptureStillImageIsCapturingStillImageContext');
// ARC does not work with iOS native objects in Delphi, since NSStr is created
// with autorelease, we have to retain to prevent automatic release when we
// return control to the operating system.
FAVCaptureStillImageIsCapturingStillImageContext.retain;
FSquare := TUIImage.Wrap(TUIImage.OCClass.imageNamed(NSSTR('squarePNG')));
FSquare.retain;
Objects[0] := CIDetectorAccuracyLow;
Keys[0] := CIDetectorAccuracy;
DetectorOptions := TNSDictionary.Wrap(TNSDictionary.Alloc.initWithObjects(@Objects[0], @Keys[0], 1));
FFaceDetector := TCIDetector.Wrap(TCIDetector.OCClass.detectorOfType(CIDetectorTypeFace, nil, DetectorOptions));
FFaceDetector.retain;
DetectorOptions.release;
end;
procedure TFMain.FormDestroy(Sender: TObject);
begin
FAVCaptureStillImageIsCapturingStillImageContext.release;
TeardownAVCapture;
FfaceDetector.release;
FSquare.release;
end;
procedure TFMain.FormGesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
var
MaxScaleAndCropFactor: CGFloat;
begin
if TInteractiveGestureFlag.gfBegin in EventInfo.Flags then
FLastDistance := EventInfo.Distance
else if not (TInteractiveGestureFlag.gfEnd in EventInfo.Flags) then
begin
FEffectiveScale := ((FMain.Width * FEffectiveScale) + (EventInfo.Distance - FLastDistance) * 4.0) / FMain.Width;
if FEffectiveScale < 1.0 then
FEffectiveScale := 1.0
else if FStillImageOutput <> nil then
begin
MaxScaleAndCropFactor := FStillImageOutput.connectionWithMediaType(AVMediaTypeVideo).videoMaxScaleAndCropFactor;
if FEffectiveScale > MaxScaleAndCropFactor then
FEffectiveScale := MaxScaleAndCropFactor;
end;
objc_msgSend(objc_getClass('CATransaction'), sel_getUid('begin'));
TCATransaction.OCClass.setAnimationDuration(0.025);
if FPreviewLayer <> nil then
FPreviewLayer.setAffineTransform(CGAffineTransformMakeScale(FEffectiveScale, FEffectiveScale));
TCATransaction.OCClass.commit;
FLastDistance := EventInfo.Distance;
end;
end;
procedure TFMain.SetupAvCapture;
function AVCaptureSessionPreset640x480: NSString;
begin
Result := CocoaNSStringConst(libAVFoundation, 'AVCaptureSessionPreset640x480');
end;
function AVLayerVideoGravityResizeAspect: NSString;
begin
Result := CocoaNSStringConst(libAVFoundation, 'AVLayerVideoGravityResizeAspect');
end;
procedure CheckError(AnError: Pointer);
var Error: NSError;
begin
if AnError <> nil then
begin
Error := TNSError.Wrap(AnError);
raise Exception.Create(NSStrToStr(Error.localizedDescription));
end;
end;
var
Error: Pointer;
Session: AVCaptureSession;
Device: AVCaptureDevice;
DeviceInput: AVCaptureDeviceInput;
RGBOutputSettings: NSDictionary;
RootLayer: CALayer;
Bounds: CGRect;
begin
Error := nil;
Session := TAVCaptureSession.Create;
if TUIDevice.Wrap(TUIDevice.OCClass.currentDevice).userInterfaceIdiom = UIUserInterfaceIdiomPhone then
Session.setSessionPreset(AVCaptureSessionPreset640x480)
else
Session.setSessionPreset(AVCaptureSessionPresetPhoto);
// Select a video device, make an input
Device := TAVCaptureDevice.Wrap(TAVCaptureDevice.OCClass.defaultDeviceWithMediaType(AVMediaTypeVideo));
DeviceInput := TAVCaptureDeviceInput.Wrap(TAVCaptureDeviceInput.OCClass.deviceInputWithDevice(Device, @Error));
CheckError(Error);
// Select a video device, make an input
if Session.canAddInput(DeviceInput) then
Session.addInput(DeviceInput)
else
raise Exception.Create('Cannod add Video input to AVCaptureSession');
try
// Make a still image output
FStillImageOutput := TAVCaptureStillImageOutput.Create;
objc_msgSend((FStillImageOutput as ILocalObject).GetObjectID,
sel_getUid('addObserver:forKeyPath:options:context:'),
FVideoCaptureDelegate.GetObjectID,
(NSSTR('capturingStillImage') as ILocalObject).GetObjectID,
NSKeyValueObservingOptionNew,
(FAVCaptureStillImageIsCapturingStillImageContext as ILocalObject).GetObjectID);
if Session.canAddOutput(FStillImageOutput) then
Session.addOutput(FStillImageOutput)
else
raise Exception.Create('Cannod add StillImageOutput to AVCaptureSession');
// Make a video data output
FVideoDataOutput := TAVCaptureVideoDataOutput.Create;
// We want BGRA, both CoreGraphics and OpenGL work well with 'BGRA'
RGBOutputSettings := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject(TNSNumber.OCClass.numberWithInt(kCVPixelFormatType_32BGRA), Pointer(kCVPixelBufferPixelFormatTypeKey)));
FVideoDataOutput.setVideoSettings(RGBOutputSettings);
FVideoDataOutput.setAlwaysDiscardsLateVideoFrames(True); // discard if the data output queue is blocked (as we process the still image)
// Create a serial dispatch queue used for the sample buffer delegate as well as when a still image is captured
// a serial dispatch queue must be used to guarantee that video frames will be delivered in order
// see the header doc for setSampleBufferDelegate:queue: for more information
FVideoDataOutputQueue := dispatch_queue_create('VideoDataOutputQueue', dispatch_queue_t(DISPATCH_QUEUE_SERIAL));
FVideoDataOutput.setSampleBufferDelegate(FVideoCaptureDelegate.GetObjectID, FVideoDataOutputQueue);
if Session.canAddOutput(FVideoDataOutput) then
Session.addOutput(FVideoDataOutput)
else
raise Exception.Create('Cannod add VideoDataOutput to AVCaptureSession');
FVideoDataOutput.connectionWithMediaType(AVMediaTypeVideo).setEnabled(False);
FEffectiveScale := 1.0;
RootLayer := WindowHandleToPlatform(Handle).View.layer;
Bounds := RootLayer.bounds;
Bounds.size.height := Bounds.size.height - tbPad.Height;
FPreviewArea := TCALayer.Create;
FPreviewArea.setBackgroundColor(TUIColor.Wrap(TUIColor.OCClass.blackColor).CGColor);
FPreviewArea.setMasksToBounds(True);
FPreviewArea.setFrame(Bounds);
RootLayer.addSublayer(FPreviewArea);
FPreviewLayer := TAVCaptureVideoPreviewLayer.Wrap(TAVCaptureVideoPreviewLayer.Alloc.initWithSession(Session));
FPreviewLayer.setBackgroundColor(TUIColor.Wrap(TUIColor.OCClass.blackColor).CGColor);
FPreviewLayer.setVideoGravity(AVLayerVideoGravityResizeAspect);
FPreviewLayer.setFrame(Bounds);
FPreviewArea.addSublayer(FPreviewLayer);
Session.startRunning;
Session.release;
except
TeardownAVCapture;
raise;
end;
end;
procedure TFMain.TeardownAVCapture;
begin
FVideoDataOutput.release;
FVideoDataOutput := nil;
if FVideoDataOutputQueue <> dispatch_queue_t(nil) then
begin
dispatch_release(dispatch_object_t(FVideoDataOutputQueue));
FVideoDataOutputQueue := dispatch_queue_t(nil);
end;
if FStillImageOutput <> nil then
begin
objc_msgSend((FStillImageOutput as ILocalObject).GetObjectID,
sel_getUid('removeObserver:forKeyPath:'),
FVideoCaptureDelegate.GetObjectID,
(NSStr('capturingStillImage') as ILocalObject).GetObjectID);
FStillImageOutput.release;
FStillImageOutput := nil;
end;
if FPreviewLayer <> nil then
begin
FPreviewLayer.removeFromSuperlayer;
FPreviewLayer.release;
FPreviewLayer := nil;
end;
if FPreviewArea <> nil then
begin
FPreviewArea.removeFromSuperlayer;
FPreviewArea.release;
FPreviewArea := nil;
end;
end;
procedure TFMain.ObserveValueForKeyPath(keyPath: NSString; ofObject: Pointer; change: NSDictionary; context: Pointer);
function NSKeyValueChangeNewKey: NSString;
begin
Result := CocoaNSStringConst(libFoundation, 'NSKeyValueChangeNewKey');
end;
var
IsCapturingStillImage: Boolean;
begin
if Context = (FAVCaptureStillImageIsCapturingStillImageContext as ILocalObject).GetObjectID then
begin
IsCapturingStillImage := objc_msgSend(change.objectForKey((NSKeyValueChangeNewKey as ILocalObject).GetObjectID), sel_getUid('boolValue')) <> nil;
FVideoCaptureDelegate.FlashAnimation(IsCapturingStillImage);
end;
end;
procedure TFMain.btnBackClick(Sender: TObject);
begin
SwitchCameras(False);
end;
procedure TFMain.btnFrontClick(Sender: TObject);
begin
SwitchCameras(True);
end;
procedure TFMain.btnSnapClick(Sender: TObject);
begin
TakePicture;
end;
function CIDetectorImageOrientation: Pointer;
begin
Result := Pointer(CocoaPointerConst(libCoreImage, 'CIDetectorImageOrientation')^);
end;
procedure TFMain.CaptureOutput(captureOutput: AVCaptureOutput; didOutputSampleBuffer: CMSampleBufferRef; fromConnection: AVCaptureConnection);
const
PHOTOS_EXIF_0ROW_TOP_0COL_LEFT = 1; // 1 = 0th row is at the top, and 0th column is on the left (THE DEFAULT).
PHOTOS_EXIF_0ROW_TOP_0COL_RIGHT = 2; // 2 = 0th row is at the top, and 0th column is on the right.
PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT = 3; // 3 = 0th row is at the bottom, and 0th column is on the right.
PHOTOS_EXIF_0ROW_BOTTOM_0COL_LEFT = 4; // 4 = 0th row is at the bottom, and 0th column is on the left.
PHOTOS_EXIF_0ROW_LEFT_0COL_TOP = 5; // 5 = 0th row is on the left, and 0th column is the top.
PHOTOS_EXIF_0ROW_RIGHT_0COL_TOP = 6; // 6 = 0th row is on the right, and 0th column is the top.
PHOTOS_EXIF_0ROW_RIGHT_0COL_BOTTOM = 7; // 7 = 0th row is on the right, and 0th column is the bottom.
PHOTOS_EXIF_0ROW_LEFT_0COL_BOTTOM = 8; // 8 = 0th row is on the left, and 0th column is the bottom.
var
PixelBuffer: CVPixelBufferRef;
Attachments: CFDictionaryRef;
Image: CIImage;
ImageOptions: NSDictionary;
CurDeviceOrientation: UIDeviceOrientation;
ExifOrientation: Integer;
Features: NSArray;
FmtDesc: CMFormatDescriptionRef;
Clap: CGRect;
begin
// Got an image
PixelBuffer := CMSampleBufferGetImageBuffer(didOutputSampleBuffer);
Attachments := CMCopyDictionaryOfAttachments(kCFAllocatorDefault, didOutputSampleBuffer, kCMAttachmentMode_ShouldPropagate);
Image := TCIImage.Alloc;
Image := TCIImage.Wrap(Image.initWithCVPixelBuffer(PixelBuffer, TNSDictionary.Wrap(Attachments)));
if Attachments <> nil then
CFRelease(Attachments);
ImageOptions := nil;
CurDeviceOrientation := TUIDevice.Wrap(TUIDevice.OCClass.currentDevice).orientation;
// kCGImagePropertyOrientation values
// The intended display orientation of the image. If present, this key is a CFNumber value with the same value as defined
// by the TIFF and EXIF specifications -- see enumeration of integer constants.
// The value specified where the origin (0,0) of the image is located. If not present, a value of 1 is assumed.
//
// used when calling featuresInImage: options: The value for this key is an integer NSNumber from 1..8 as found in kCGImagePropertyOrientation.
// If present, the detection will be done based on that orientation but the coordinates in the returned features will still be based on those of the image.
case CurDeviceOrientation of
UIDeviceOrientationPortraitUpsideDown: // Device oriented vertically, home button on the top
ExifOrientation := PHOTOS_EXIF_0ROW_LEFT_0COL_BOTTOM;
UIDeviceOrientationLandscapeLeft: // Device oriented horizontally, home button on the right
if FIsUsingFrontFacingCamera then
ExifOrientation := PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT
else
ExifOrientation := PHOTOS_EXIF_0ROW_TOP_0COL_LEFT;
UIDeviceOrientationLandscapeRight: // Device oriented horizontally, home button on the left
if FIsUsingFrontFacingCamera then
ExifOrientation := PHOTOS_EXIF_0ROW_TOP_0COL_LEFT
else
ExifOrientation := PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT;
else
ExifOrientation := PHOTOS_EXIF_0ROW_RIGHT_0COL_TOP;
end;
ImageOptions := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject(TNSNumber.OCClass.numberWithInt(ExifOrientation), CIDetectorImageOrientation));
Features := FFaceDetector.featuresInImage(Image, ImageOptions);
Features.retain; // ARC does not operate in delphi with Objective C objects. Must be done manually
Image.release;
// Get the clean aperture
// the clean aperture is a rectangle that defines the portion of the encoded pixel dimensions
// that represents image data valid for display.
FmtDesc := CMSampleBufferGetFormatDescription(didOutputSampleBuffer);
Clap := CMVideoFormatDescriptionGetCleanAperture(FmtDesc, False {originIsTopLeft == false});
dispatch_async(dispatch_get_main_queue,
procedure begin
DrawFaceBoxesForFeatures(Features, Clap, CurDeviceOrientation);
end);
end;
procedure TFMain.chkFacesSwitch(Sender: TObject);
begin
ToggleFaceDetection((Sender as TSwitch).IsChecked);
end;
// Called asynchronously as the capture output is capturing sample buffers, this method asks the face detector (if on)
// to detect features and for each draw the red square in a layer and set appropriate orientation
procedure TFMain.DrawFaceBoxesForFeatures(Features: NSArray; Clap: CGRect; Orientation: UIDeviceOrientation);
function kCATransactionDisableActions: NSString;
begin
Result := CocoaNSStringConst(libQuartzCore, 'kCATransactionDisableActions');
end;
var
Sublayers: NSArray;
SublayersCount, CurrentSublayer: NSInteger;
FeaturesCount: NSInteger;
Idx: NSInteger;
Layer: CALayer;
ParentFrameSize: CGSize;
Gravity: NSString;
IsMirrored: Boolean;
PreviewBox: CGRect;
FF: CIFaceFeature;
FaceRect: CGRect;
Temp: CGFloat;
WidthScaleBy: CGFloat;
HeightScaleBy: CGFloat;
FeatureLayer: CALayer;
CurrentLayer: CALayer;
LName: NSString;
begin
try
Sublayers := TNSArray.Wrap(TNSArray.OCClass.arrayWithArray(FPreviewLayer.sublayers));
SublayersCount := Sublayers.count;
CurrentSublayer := 0;
FeaturesCount := Features.count;
objc_msgSend(objc_getClass('CATransaction'), sel_getUid('begin'));
TCATransaction.OCClass.setValue(kCFBooleanTrue, kCATransactionDisableActions);
// hide all the face layers
for Idx := 0 to SublayersCount - 1 do
begin
Layer := TCALayer.Wrap(Sublayers.objectAtIndex(Idx));
LName := Layer.name;
if LName <> nil then
if LName.isEqualToString(NSSTR('FaceLayer')) then
Layer.setHidden(True);
end;
if (FeaturesCount = 0) or not FDetectFaces then
begin
TCATransaction.OCClass.commit;
Exit; // Early exit
end;
ParentFrameSize := WindowHandleToPlatform(Handle).View.frame.size;
ParentFrameSize.height := ParentFrameSize.height - tbPad.Height;
Gravity := FPreviewLayer.videoGravity;
IsMirrored := FPreviewLayer.isMirrored;
PreviewBox := VideoPreviewBoxForGravity(Gravity, ParentFrameSize, Clap.size);
for Idx := 0 to FeaturesCount - 1 do
begin
// Find the correct position for the square layer within the previewLayer
// the feature box originates in the bottom left of the video frame.
// (Bottom right if mirroring is turned on)
FF := TCIFaceFeature.Wrap(Features.objectAtIndex(Idx));
FaceRect := FF.bounds;
// Flip preview width and height
Temp := FaceRect.size.width;
FaceRect.size.width := FaceRect.size.height;
FaceRect.size.height := temp;
Temp := FaceRect.origin.x;
FaceRect.origin.x := faceRect.origin.y;
FaceRect.origin.y := temp;
// Scale coordinates so they fit in the preview box, which may be scaled
WidthScaleBy := PreviewBox.size.width / Clap.size.height;
HeightScaleBy := PreviewBox.size.height / Clap.size.width;
FaceRect.size.width := FaceRect.size.width * WidthScaleBy;
FaceRect.size.height := FaceRect.size.height * HeightScaleBy;
FaceRect.origin.x := FaceRect.origin.x * WidthScaleBy;
FaceRect.origin.y := FaceRect.origin.y * HeightScaleBy;
if IsMirrored then
FaceRect := CGRectOffset(FaceRect, PreviewBox.origin.x + PreviewBox.size.width - FaceRect.size.width - (FaceRect.origin.x * 2), PreviewBox.origin.y)
else
FaceRect := CGRectOffset(FaceRect, PreviewBox.origin.x, PreviewBox.origin.y);
FeatureLayer := nil;
// Re-use an existing layer if possible
while (FeatureLayer = nil) and (CurrentSublayer < SublayersCount) do
begin
CurrentLayer := TCALayer.Wrap(Sublayers.objectAtIndex(CurrentSublayer));
Inc(CurrentSublayer);
LName := CurrentLayer.name;
if LName <> nil then
if LName.isEqualToString(NSSTR('FaceLayer')) then
begin
FeatureLayer := CurrentLayer;
CurrentLayer.setHidden(False);
end;
end;
// Create a new one if necessary
if FeatureLayer = nil then
begin
FeatureLayer := TCALayer.Create;
FeatureLayer.setContents(FSquare.CGImage);
FeatureLayer.setName(NSSTR('FaceLayer'));
FPreviewLayer.addSublayer(FeatureLayer);
FeatureLayer.release;
end;
FeatureLayer.setFrame(FaceRect);
case Orientation of
UIDeviceOrientationPortrait:
FeatureLayer.setAffineTransform(CGAffineTransformMakeRotation(DegToRad(0.0)));
UIDeviceOrientationPortraitUpsideDown:
FeatureLayer.setAffineTransform(CGAffineTransformMakeRotation(DegToRad(180.0)));
UIDeviceOrientationLandscapeLeft:
FeatureLayer.setAffineTransform(CGAffineTransformMakeRotation(DegToRad(90.0)));
UIDeviceOrientationLandscapeRight:
FeatureLayer.setAffineTransform(CGAffineTransformMakeRotation(DegToRad(-90.0)));
end;
end;
TCATransaction.OCClass.commit;
finally
Features.release; // ARC does not operate in delphi with Objective C objects. Must be done manually
end;
end;
// Find where the video box is positioned within the preview layer based on the video size and gravity
function TFMain.VideoPreviewBoxForGravity(Gravity: NSString; FrameSize: CGSize; ApertureSize: CGSize): CGRect;
function AVLayerVideoGravityResizeAspectFill: NSString;
begin
Result := CocoaNSStringConst(libAVFoundation, 'AVLayerVideoGravityResizeAspectFill');
end;
function AVLayerVideoGravityResizeAspect: NSString;
begin
Result := CocoaNSStringConst(libAVFoundation, 'AVLayerVideoGravityResizeAspect');
end;
function AVLayerVideoGravityResize: NSString;
begin
Result := CocoaNSStringConst(libAVFoundation, 'AVLayerVideoGravityResizeAspect');
end;
const
CGSizeZero: CGSize = (width: 0.0; height: 0.0);
var
ApertureRatio: CGFloat;
ViewRatio: CGFloat;
Size: CGSize;
VideoBox: CGRect;
begin
ApertureRatio := ApertureSize.height / ApertureSize.width;
ViewRatio := FrameSize.width / FrameSize.height;
Size := CGSizeZero;
if Gravity.isEqualToString(AVLayerVideoGravityResizeAspectFill) then
if ViewRatio > ApertureRatio then
begin
Size.width := FrameSize.width;
Size.height := ApertureSize.width * (FrameSize.width / ApertureSize.height);
end
else
begin
Size.width := ApertureSize.height * (FrameSize.height / ApertureSize.width);
Size.height := frameSize.height;
end
else if Gravity.isEqualToString(AVLayerVideoGravityResizeAspect) then
if ViewRatio > ApertureRatio then
begin
Size.width := ApertureSize.height * (FrameSize.height / ApertureSize.width);
Size.height := FrameSize.height;
end
else
begin
Size.width := FrameSize.width;
Size.height := ApertureSize.width * (FrameSize.width / ApertureSize.height);
end
else if Gravity.isEqualToString(AVLayerVideoGravityResize) then
begin
Size.width := FrameSize.width;
Size.height := FrameSize.height;
end;
VideoBox.size := size;
if Size.width < FrameSize.width then
VideoBox.origin.x := (FrameSize.width - Size.width) / 2.0
else
VideoBox.origin.x := (Size.width - FrameSize.width) / 2.0;
if Size.height < FrameSize.height then
VideoBox.origin.y := (FrameSize.height - Size.height) / 2.0
else
VideoBox.origin.y := (Size.height - FrameSize.height) / 2.0;
Result := VideoBox;
end;
// Use front/back camera
procedure TFMain.SwitchCameras(FrontFacingCamera: Boolean);
var
DesiredPosition: AVCaptureDevicePosition;
Devices: NSArray;
Device: AVCaptureDevice;
I, J: NSInteger;
OldInputs: NSArray;
Input, OldInput: AVCaptureDeviceInput;
begin
if FIsUsingFrontFacingCamera <> FrontFacingCamera then
begin
if FIsUsingFrontFacingCamera then
DesiredPosition := AVCaptureDevicePositionBack
else
DesiredPosition := AVCaptureDevicePositionFront;
Devices := TAVCaptureDevice.OCClass.devicesWithMediaType(AVMediaTypeVideo);
for I := 0 to NSInteger(Devices.count) - 1 do
begin
Device := TAVCaptureDevice.Wrap(Devices.objectAtIndex(I));
if Device.position = DesiredPosition then
begin
FPreviewLayer.session.beginConfiguration;
try
Input := TAVCaptureDeviceInput.Wrap(TAVCaptureDeviceInput.OCClass.deviceInputWithDevice(Device, nil));
OldInputs := FPreviewLayer.session.inputs;
for J := 0 to NSInteger(OldInputs.count) - 1 do
begin
OldInput := TAVCaptureDeviceInput.Wrap(OldInputs.objectAtIndex(J));
FPreviewLayer.session.removeInput(OldInput);
end;
FPreviewLayer.session.addInput(Input);
finally
FPreviewLayer.session.commitConfiguration;
end;
Break;
end;
end;
FIsUsingFrontFacingCamera := not FIsUsingFrontFacingCamera;
end;
end;
// Turn on/off face detection
procedure TFMain.ToggleFaceDetection(DetectFaces: Boolean);
const
CGRectZero: CGRect =
(
origin: (x: 0.0; y: 0.0);
size: (width: 0.0; height: 0.0)
);
begin
FDetectFaces := DetectFaces;
FVideoDataOutput.connectionWithMediaType(AVMediaTypeVideo).setEnabled(FDetectFaces);
if not FDetectFaces then
dispatch_async(dispatch_get_main_queue,
procedure begin
// Clear out any squares currently displaying.
DrawFaceBoxesForFeatures(TNSArray.Create, CGRectZero, UIDeviceOrientationPortrait);
end);
end;
// Main action method to take a still image -- if face detection has been turned on and a face has been detected
// the square overlay will be composited on top of the captured image and saved to the camera roll
procedure TFMain.TakePicture;
function AVVideoCodecJPEG: Pointer;
begin
Result := Pointer(CocoaPointerConst(libAVFoundation, 'AVVideoCodecJPEG')^);
end;
function AVVideoCodecKey: Pointer;
begin
Result := Pointer(CocoaPointerConst(libAVFoundation, 'AVVideoCodecKey')^);
end;
var
StillImageConnection: AVCaptureConnection;
CaptureOrientation: AVCaptureVideoOrientation;
OutputSettings: NSDictionary;
begin
// Find out the current orientation and tell the still image output.
StillImageConnection := FStillImageOutput.connectionWithMediaType(AVMediaTypeVideo);
FCurDeviceOrientation := TUIDevice.Wrap(TUIDevice.OCClass.currentDevice).orientation;
CaptureOrientation := OrientationForDeviceOrientation(FCurDeviceOrientation);
StillImageConnection.setVideoOrientation(CaptureOrientation);
StillImageConnection.setVideoScaleAndCropFactor(FEffectiveScale);
FDoingFaceDetection := FDetectFaces and (FEffectiveScale = 1.0);
// Set the appropriate pixel format / image type output setting depending on if we'll need an uncompressed image for
// the possiblity of drawing the red square over top or if we're just writing a jpeg to the camera roll which is the trival case
if FDoingFaceDetection then
OutputSettings := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject(TNSNumber.OCClass.numberWithInt(kCMPixelFormat_32BGRA),
kCVPixelBufferPixelFormatTypeKey))
else
OutputSettings := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject(AVVideoCodecJPEG, AVVideoCodecKey));
FStillImageOutput.setOutputSettings(OutputSettings);
FStillImageOutput.captureStillImageAsynchronouslyFromConnection(StillImageConnection, StillImageCaptured);
end;
procedure TFMain.StillImageCaptured(const ImageDataSampleBuffer: CMSampleBufferRef; const Error: NSError);
function kCGImagePropertyOrientation: CFStringRef;
begin
Result := CFStringRef(CocoaPointerConst(libImageIO, 'kCGImagePropertyOrientation')^);
end;
var
PixelBuffer: CVPixelBufferRef;
Attachments: CFDictionaryRef;
Image: CIImage;
ImageOptions: NSDictionary;
Orientation: Pointer;
JpegData: NSData;
Lib: ALAssetsLibrary;
begin
{$IFNDEF CALLBACK_ERRORS}
if Error <> nil then
DisplayErrorOnMainQueue(Error, 'Take picture failed')
else
{$ELSE}
if ImageDataSampleBuffer <> nil then
{$ENDIF}
if FDoingFaceDetection then
begin
// Got an image.
PixelBuffer := CMSampleBufferGetImageBuffer(ImageDataSampleBuffer);
Attachments := CMCopyDictionaryOfAttachments(kCFAllocatorDefault, ImageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
Image := TCIImage.Wrap(TCIImage.Alloc.initWithCVPixelBuffer(PixelBuffer, TNSDictionary.Wrap(Attachments)));
if Attachments <> nil then
CFRelease(Attachments);
Orientation := CMGetAttachment(ImageDataSampleBuffer, kCGImagePropertyOrientation, nil);
if Orientation <> nil then
begin
ImageOptions := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject(Orientation, CIDetectorImageOrientation));
ImageOptions.retain;
end
else
ImageOptions := nil;
// When processing an existing frame we want any new frames to be automatically dropped
// queueing this block to execute on the videoDataOutputQueue serial queue ensures this
// see the header doc for setSampleBufferDelegate:queue: for more information
dispatch_sync(FVideoDataOutputQueue,
procedure
var
Features: NSArray;
SrcImage: CGImageRef;
Err: OSStatus;
ImageResult: CGImageRef;
Attachments: CFDictionaryRef;
begin
// Get the array of CIFeature instances in the given image with a orientation passed in
// the detection will be done based on the orientation but the coordinates in the returned features will
// still be based on those of the image.
Features := FFaceDetector.featuresInImage(Image, ImageOptions);
if ImageOptions <> nil then
ImageOptions.release;
SrcImage := nil;
Err := CreateCGImageFromCVPixelBuffer(CMSampleBufferGetImageBuffer(ImageDataSampleBuffer), SrcImage);