private static async Task SaveFacesForTileAsync(List <Rect> faces, IBuffer imageBuffer, int postId) { foreach (var face in faces) { var imageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(TILE_FOLDER_NAME, CreationCollisionOption.OpenIfExists); var imageFile = await imageFolder.CreateFileAsync($"{postId}-{faces.IndexOf(face)}.jpg", CreationCollisionOption.ReplaceExisting); await FileIO.WriteBufferAsync(imageFile, imageBuffer); await CropBitmap.GetCroppedBitmapAsync(imageFile, face, 1); } }
private static async Task CropImageFile(StorageFile imageFile, Size imageSize, CropMethod method, Size screenSize, IBuffer previewBuffer) { switch (method) { case CropMethod.None: break; case CropMethod.Center: { var cropRect = CropBitmap.GetCenterCropRect(imageSize, screenSize); await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0); } break; case CropMethod.TopMiddle: { var cropRect = CropBitmap.GetTopMiddleCropRect(imageSize, screenSize); await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0); } break; case CropMethod.MostFaces: case CropMethod.BiggestFace: var result = await DetechFromBufferAsync(previewBuffer, 3, 25); var faces = ImageCropper.ScaleFaces(result.Faces, imageSize, result.FrameSize); if (faces.Count() > 0) { if (method == CropMethod.BiggestFace) { faces = faces.OrderByDescending(o => o.Width).Take(1); } var cropRect = CropBitmap.GetGreedyCropRect(imageSize, screenSize, faces); await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0); } else { // No face detected, fall back crop var cropRect = CropBitmap.GetTopMiddleCropRect(imageSize, screenSize); await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0); } break; default: break; } }
public static async Task SetLockScreen(Post post, Rect cropRect) { Uri urlToDownload; if (false) // TODO: detect whether the device is a phone { // Only select file smaller than 2MB as the system required if (post.FileSize <= 2000000) { urlToDownload = new Uri(post.FileUrl); } else if (post.JpegFileSize <= 2000000) { urlToDownload = new Uri(post.JpegUrl); } else if (post.SampleFileSize <= 2000000) { urlToDownload = new Uri(post.SampleUrl); } else { urlToDownload = new Uri(post.PreviewUrl); } } else { urlToDownload = new Uri(post.JpegUrl); } // Download the file into local storage var imageSaved = await DownloadImageFromUri(urlToDownload, BackgroundTaskType.LockScreen); // Crop if needed //if (cropRect) //{ await CropBitmap.GetCroppedBitmapAsync(imageSaved, cropRect, 1); //} // Set the lockScreen await ChangeLockScreenBackground(imageSaved); // Delete the local file }