public void DidOutputMetadataObjects(AVCaptureMetadataOutput captureOutput, AVMetadataObject[] faces, AVCaptureConnection connection) { List<int> unseen = faceViews.Keys.ToList (); List<int> seen = new List<int> (); CATransaction.Begin (); CATransaction.SetValueForKey (NSObject.FromObject (true), (NSString) (CATransaction.DisableActions.ToString ())); foreach (var face in faces) { int faceId = (face as AVMetadataFaceObject).FaceID; unseen.Remove (faceId); seen.Add (faceId); FaceView view; if (faceViews.ContainsKey (faceId)) view = faceViews [faceId]; else { view = new FaceView (); view.Layer.CornerRadius = 10; view.Layer.BorderWidth = 3; view.Layer.BorderColor = UIColor.Green.CGColor; previewView.AddSubview (view); faceViews.Add (faceId, view); view.Id = faceId; view.Callback = TouchCallBack; if (lockedFaceID != null) view.Alpha = 0; } AVMetadataFaceObject adjusted = (AVMetadataFaceObject)(previewView.Layer as AVCaptureVideoPreviewLayer).GetTransformedMetadataObject (face); view.Frame = adjusted.Bounds; } foreach (int faceId in unseen) { FaceView view = faceViews [faceId]; view.RemoveFromSuperview (); faceViews.Remove (faceId); if (faceId == lockedFaceID) clearLockedFace (); } if (lockedFaceID != null) { FaceView view = faceViews [lockedFaceID.GetValueOrDefault ()]; float size = (float)Math.Max (view.Frame.Size.Width, view.Frame.Size.Height) / device.VideoZoomFactor; float zoomDelta = lockedFaceSize / size; float lockTime = (float)(CATransition.CurrentMediaTime () - this.lockTime); float zoomRate = (float)(Math.Log (zoomDelta) / lockTime); if (Math.Abs (zoomDelta) > 0.1) device.RampToVideoZoom (zoomRate > 0 ? MaxZoom : 1, zoomRate); } CATransaction.Commit (); }
partial void meme(NSObject sender) { memeEffect.Seek(CMTime.Zero); memeEffect.Play(); NSObject.CancelPreviousPerformRequest(this); PerformSelector(new ObjCRuntime.Selector("flash"), null, MEME_FLASH_DELAY); PerformSelector(new ObjCRuntime.Selector("startZoom:"), NSNumber.FromFloat(getZoomSliderValue()), MEME_ZOOM_DELAY); device.VideoZoomFactor = 1; foreach (var faceId in faceViews.Keys) { FaceView view = faceViews [faceId]; view.Alpha = 0; } }
void TouchCallBack(int faceId, FaceView view) { lockedFaceID = faceId; lockedFaceSize = Math.Max(view.Frame.Size.Width, view.Frame.Size.Height) / device.VideoZoomFactor; lockTime = CATransition.CurrentMediaTime(); UIView.BeginAnimations(null, IntPtr.Zero); UIView.SetAnimationDuration(0.3f); view.Layer.BorderColor = UIColor.Red.CGColor; foreach (var face in faceViews.Values) { if (face != view) { face.Alpha = 0; } } UIView.CommitAnimations(); beepEffect.Seek(CMTime.Zero); beepEffect.Play(); }
public void DidOutputMetadataObjects(AVCaptureMetadataOutput captureOutput, AVMetadataObject[] faces, AVCaptureConnection connection) { List <int> unseen = faceViews.Keys.ToList(); List <int> seen = new List <int> (); CATransaction.Begin(); CATransaction.SetValueForKey(NSObject.FromObject(true), (NSString)(CATransaction.DisableActions.ToString())); foreach (var face in faces) { // HACK: int faceId = (face as AVMetadataFaceObject).FaceID; int faceId = (int)(face as AVMetadataFaceObject).FaceID; unseen.Remove(faceId); seen.Add(faceId); FaceView view; if (faceViews.ContainsKey(faceId)) { view = faceViews [faceId]; } else { view = new FaceView(); view.Layer.CornerRadius = 10; view.Layer.BorderWidth = 3; view.Layer.BorderColor = UIColor.Green.CGColor; previewView.AddSubview(view); faceViews.Add(faceId, view); view.Id = faceId; view.Callback = TouchCallBack; if (lockedFaceID != null) { view.Alpha = 0; } } AVMetadataFaceObject adjusted = (AVMetadataFaceObject)(previewView.Layer as AVCaptureVideoPreviewLayer).GetTransformedMetadataObject(face); view.Frame = adjusted.Bounds; } foreach (int faceId in unseen) { FaceView view = faceViews [faceId]; view.RemoveFromSuperview(); faceViews.Remove(faceId); if (faceId == lockedFaceID) { clearLockedFace(); } } if (lockedFaceID != null) { FaceView view = faceViews [lockedFaceID.GetValueOrDefault()]; // HACK: Cast resulting nfloat to float // float size = (float)Math.Max (view.Frame.Size.Width, view.Frame.Size.Height) / device.VideoZoomFactor; float size = (float)(Math.Max(view.Frame.Size.Width, view.Frame.Size.Height) / device.VideoZoomFactor); float zoomDelta = lockedFaceSize / size; float lockTime = (float)(CATransition.CurrentMediaTime() - this.lockTime); float zoomRate = (float)(Math.Log(zoomDelta) / lockTime); if (Math.Abs(zoomDelta) > 0.1) { device.RampToVideoZoom(zoomRate > 0 ? MaxZoom : 1, zoomRate); } } CATransaction.Commit(); }
void TouchCallBack(int faceId, FaceView view) { lockedFaceID = faceId; lockedFaceSize = Math.Max (view.Frame.Size.Width, view.Frame.Size.Height) / device.VideoZoomFactor; lockTime = CATransition.CurrentMediaTime (); UIView.BeginAnimations (null, IntPtr.Zero); UIView.SetAnimationDuration (0.3f); view.Layer.BorderColor = UIColor.Red.CGColor; foreach (var face in faceViews.Values) { if (face != view) face.Alpha = 0; } UIView.CommitAnimations (); beepEffect.Seek (CMTime.Zero); beepEffect.Play (); }