public static void SavePhotoFromSource(this Face face, UIImage sourceImage) { using (var croppedFaceImg = sourceImage.Crop(face.FaceRectangle)) { face.SavePhotoFromCropped(croppedFaceImg); } }
public Task <List <FaceImageModel> > Detect(UIImage photo) { try { List <FaceImageModel> faces = new List <FaceImageModel>(); var tcs = new TaskCompletionSource <List <FaceImageModel> >(); using (var jpgData = photo.AsJPEG(.8f)) { Client.DetectWithData(jpgData, true, true, new NSObject[0], (detectedFaces, error) => { tcs.FailTaskIfErrored(error.ToException()); foreach (var detectedFace in detectedFaces) { var face = detectedFace.ToFace(); faces.Add(face); using (var croppedImage = photo.Crop(face.FaceRectangle)) { //save to disk using (var data = croppedImage.AsJPEG()) { data.Save(face.PhotoPath, true); } } } tcs.SetResult(faces); }).Resume(); } return(tcs.Task); } catch (Exception ex) { Log.Error(ex.Message); throw; } }
/// <summary> /// Using the ninepatch core image, create the newly resized image. /// </summary> /// <returns>The resizable image from nine patch image.</returns> /// <param name="image">Image.</param> private static UIImage CreateResizableImageFromNinePatchImage(UIImage ninePatch) { int width = (int)ninePatch.Size.Width; int height = (int)ninePatch.Size.Height; float[,] alphaImage = GetAlphasFromImage(ninePatch, 0, 0, width * height); // The width scale section of a ninepatch is only valid from 0 - image.width EXCLUDING the first and last pixel (ctnd.) float[] npWidthBar = new float[width]; float[] npHeightBar = new float[height]; for (int w = 0; w < width; w++) { npWidthBar[w] = alphaImage[w, 0]; } for (int h = 0; h < height; h++) { npHeightBar[h] = alphaImage[0, h]; } float left = -1, top = -1, right = -1, bottom = -1; // Start looking for the scale bounds of the image. // Note: all of the following loops are inset by a single pixel to account // for the fact that a valid ninepatch CANNOT have a scale pixel at the // width and height extremes. (ie. a width pixel cannot be at 0,0) // Find the left-most ninepatch scale indicator for (int i = 1; i < width - 1; i++) { if (npWidthBar[i] == 1) { left = i; break; } } // Find the right-most ninepatch scale indicator for (int i = width - 2; i > 0; i--) { if (npWidthBar[i] == 1) { right = i; break; } } // Find the top-most ninepatch scale indicator for (int i = 1; i < height - 1; i++) { if (npHeightBar[i] == 1) { top = i; break; } } // Find the bottom-most ninepatch scale indicator for (int i = height - 2; i > 0; i--) { if (npHeightBar[i] == 1) { bottom = i; break; } } // Ensure that the image is valid if (left == -1 || right == -1 || top == -1 || bottom == -1) { Log.E("NinePathImageFactory", "Failed to parse ninepatch. Returning image instead."); return(ninePatch); // throw new ArgumentException("Cannot create ninepatch image: ninepatch scale bars invalid {l:" + left + ", t: " + top + ", r:" + right + " b:" + bottom + "}"); } var cropImage = ninePatch.Crop(new CGRect(1, 1, width - 3, height - 3)); return(cropImage.CreateResizableImage(new UIEdgeInsets(top, left, bottom, right))); }
/// <summary> /// Creates a thumbnail image for a given Face. /// </summary> /// <returns>The thumbnail image.</returns> /// <param name="face">The Face to generate a thumbnail for.</param> /// <param name="sourceImage">The source image or photo to crop the thumbnail from.</param> public static UIImage CreateThumbnail(this Model.Face face, UIImage sourceImage) { return(sourceImage.Crop(face.FaceRectangle.ToRectangle())); }