public Task <SupportImageXF> IF_WriteStreamToFile(SupportImageXF imageSet, SyncPhotoOptions options) { return(Task.Factory.StartNew(() => { try { var newWidth = options.Width; var newHeight = options.Height; // Create temp file path var fileNameOnly = "new_"; var fileExtension = ".jpg"; var newFileName = fileNameOnly + "_" + DateTime.Now.ToFormatString("yyyyMMddHHmmss") + "_" + newWidth + "x" + newHeight + fileExtension; var tempPath = Path.Combine(Path.GetTempPath(), newFileName); FileHelper.CreateFile(tempPath); // Write data to temp file using (var newStream = FileHelper.GetWriteFileStream(tempPath)) { var buffer = imageSet.RawData; newStream.Write(buffer, 0, buffer.Length); imageSet.ProcessFilePath = tempPath; } Console.WriteLine(imageSet.ProcessFilePath); return imageSet; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return imageSet; } })); }
public static Bitmap SyncBitmapWithQuality(this Bitmap bitmap, SyncPhotoOptions syncPhotoOptions) { int width = bitmap.Width; int height = bitmap.Height; float scaleSize = 1f; if (height > syncPhotoOptions.Height || width > syncPhotoOptions.Width) { scaleSize = width < height ? ((float)syncPhotoOptions.Height) / height : ((float)syncPhotoOptions.Width) / width; } Matrix matrix = new Matrix(); matrix.PostScale(scaleSize, scaleSize); Bitmap resizedBitmap = Bitmap.CreateBitmap(bitmap, 0, 0, width, height, matrix, false); using (var stream = new MemoryStream()) { bitmap.Compress(Bitmap.CompressFormat.Jpeg, (int)(syncPhotoOptions.Quality * 100), stream); } bitmap.Recycle(); return(resizedBitmap); }
public static Bitmap getResizedBitmap(Bitmap bm, SyncPhotoOptions SyncPhotoOptions) { int width = bm.Width; int height = bm.Height; float scaleSize = 1f; if (height > SyncPhotoOptions.Height || width > SyncPhotoOptions.Width) { scaleSize = width < height ? ((float)SyncPhotoOptions.Height) / height : ((float)SyncPhotoOptions.Width) / width; Matrix matrix = new Matrix(); matrix.PostScale(scaleSize, scaleSize); Bitmap resizedBitmap = Bitmap.CreateBitmap(bm, 0, 0, width, height, matrix, false); using (var stream = new MemoryStream()) { resizedBitmap.Compress(Bitmap.CompressFormat.Jpeg, 80, stream); } bm.Recycle(); return(resizedBitmap); } else { return(bm); } }
//1.2 megapixel 1280 x 960 public static UIImage ResizeImage(this UIImage sourceImage, SyncPhotoOptions options) { nfloat scale = 1.0f; CoreGraphics.CGSize cGSize; if (sourceImage.Size.Width > sourceImage.Size.Height) { //scale by width scale = options.Width / sourceImage.Size.Width; cGSize = new CoreGraphics.CGSize(options.Width, sourceImage.Size.Height * scale); } else { //scale by height scale = options.Width / sourceImage.Size.Height; cGSize = new CoreGraphics.CGSize(sourceImage.Size.Width * scale, options.Width); } UIGraphics.BeginImageContext(cGSize); sourceImage.Draw(new CoreGraphics.CGRect(0, 0, cGSize.Width, cGSize.Height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resultImage); }
public static Bitmap GetOriginalBitmapFromPath(this string fileName, SyncPhotoOptions syncPhotoOptions) { // First we get the the dimensions of the file on disk BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true }; BitmapFactory.DecodeFile(fileName, options); // Next we calculate the ratio that we need to resize the image by // in order to fit the requested dimensions. int outHeight = options.OutHeight; int outWidth = options.OutWidth; int inSampleSize = 1; if (outHeight > syncPhotoOptions.Height || outWidth > syncPhotoOptions.Width) { inSampleSize = outWidth < outHeight ? outHeight / syncPhotoOptions.Height : outWidth / syncPhotoOptions.Width; } // Now we will load the image and have BitmapFactory resize it for us. options.InSampleSize = inSampleSize; options.InJustDecodeBounds = false; Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options); return(resizedBitmap); }
public void IF_OpenCamera(IGalleryPickerResultListener pickerResultListener, SyncPhotoOptions options, int _CodeRequest) { CodeRequest = _CodeRequest; galleryPickerResultListener = pickerResultListener; UIStoryboard storyboard = UIStoryboard.FromName("UtilStoryboard", null); XFCameraController controller = (XFCameraController)storyboard.InstantiateViewController("XFCameraController"); NaviExtensions.OpenController(controller); }
public void IF_OpenCamera(IGalleryPickerResultListener pickerResultListener, SyncPhotoOptions options, int _CodeRequest) { CodeRequest = _CodeRequest; galleryPickerResultListener = pickerResultListener; var pickerIntent = new Intent(SupportWidgetXFSetup.Activity, typeof(GalleryPickerActivity)); pickerIntent.PutExtra(Utils.SubscribeImageFromCamera, Utils.SubscribeImageFromCamera); SupportWidgetXFSetup.Activity.StartActivity(pickerIntent); }
private async void OnOpenCameraCommand() { var xx = DependencyService.Get <ISupportMedia>(); if (xx != null) { var option = new SyncPhotoOptions(); var result = await xx.IF_OpenCamera(option); ImageItems.Add(result); var result2 = await xx.IF_WriteStreamToFile(result, option); var xxyy = new SupportImageXF() { ImageSourceXF = ImageSource.FromFile(result2.ProcessFilePath) }; ImageItems.Add(xxyy); } }
public Task <SupportImageXF> IF_SyncPhotoFromCloud(SupportImageXF imageSet, SyncPhotoOptions options) { ManualResetEvent manualResetEvent = new ManualResetEvent(false); return(Task.Factory.StartNew(() => { try { manualResetEvent.Reset(); var sortOptions = new PHFetchOptions(); sortOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; var FeechPhotoByIdentifiers = PHAsset.FetchAssetsUsingLocalIdentifiers( new string[] { imageSet.OriginalPath }, sortOptions).Cast <PHAsset>().FirstOrDefault(); Console.WriteLine(imageSet.OriginalPath + " ==> Run at step 1"); if (FeechPhotoByIdentifiers != null) { var requestOptions = new PHImageRequestOptions() { NetworkAccessAllowed = true, DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat, ResizeMode = PHImageRequestOptionsResizeMode.None, }; var requestSize = PHImageManager.MaximumSize; Console.WriteLine(imageSet.OriginalPath + " ==> Run at step 2"); PHImageManager.DefaultManager.RequestImageForAsset(FeechPhotoByIdentifiers, requestSize, PHImageContentMode.AspectFit, requestOptions, (image, info) => { Console.WriteLine(imageSet.OriginalPath + " ==> Run at step 3"); if (image != null) { //var newImage = result.ResizeImage(options); //imageSet.ImageRawData = newImage.AsJPEG(options.Quality).ToArray(); try { var url = info.ObjectForKey(new NSString("PHImageFileURLKey")) as NSUrl; if (url == null) { // Get image and save to file var filePath = Path.Combine(Path.GetTempPath(), $"Photo_{Path.GetTempFileName()}"); image.AsJPEG().Save(filePath, false, out _); url = new NSUrl(filePath); } var fileName = url.Path; var newWidth = image.Size.Width; var newHeight = image.Size.Height; // Create temp file path var fileNameOnly = Path.GetFileNameWithoutExtension(fileName); var fileExtension = Path.GetExtension(fileName); var newFileName = fileNameOnly + "_" + DateTime.Now.ToFormatString("yyyyMMddHHmmss") + "_" + newWidth + "x" + newHeight + fileExtension; var tempPath = Path.Combine(Path.GetTempPath(), newFileName); FileHelper.CreateFile(tempPath); // Write data to temp file using (var newStream = FileHelper.GetWriteFileStream(tempPath)) { using (var imageData = image.AsJPEG()) { var buffer = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, buffer, 0, Convert.ToInt32(imageData.Length)); newStream.Write(buffer, 0, buffer.Length); imageSet.ProcessFilePath = tempPath; //Console.WriteLine("Finish item at ==> " + tempPath); } } Console.WriteLine(imageSet.OriginalPath + " ==> Run at step 4"); manualResetEvent.Set(); } catch (Exception ex) { // Ignore Console.WriteLine("Error for " + imageSet.OriginalPath); Console.WriteLine(ex.StackTrace); manualResetEvent.Set(); } } else { Console.WriteLine("Can not sync image"); manualResetEvent.Set(); } }); } else { Console.WriteLine(imageSet.OriginalPath + " ==> Run at step 5"); manualResetEvent.Set(); } manualResetEvent.WaitOne(); return imageSet; } catch (Exception ex) { Console.WriteLine(ex); return imageSet; } })); }
public XFCameraController(IntPtr handle) : base(handle) { syncPhotoOptions = new SyncPhotoOptions(); }
public void IF_OpenGallery(IGalleryPickerResultListener pickerResultListener, SyncPhotoOptions options, int _CodeRequest) { CodeRequest = _CodeRequest; galleryPickerResultListener = pickerResultListener; NaviExtensions.OpenController(new GalleryPickerController()); }
public Task <SupportImageXF> IF_SyncPhotoFromCloud(ISupportMediaResultListener supportMediaResultListener, SupportImageXF imageSet, SyncPhotoOptions options) { var bitmap = imageSet.OriginalPath.GetOriginalBitmapFromPath(options); using (var streamBitmap = new MemoryStream()) { bitmap.Compress(Bitmap.CompressFormat.Jpeg, (int)(options.Quality * 100), streamBitmap); imageSet.ImageRawData = streamBitmap.ToArray().ToArray(); bitmap.Recycle(); return(Task.FromResult <SupportImageXF>(imageSet)); } }
public void IF_OpenGallery(ISupportMediaResultListener _supportMediaResultListener, SyncPhotoOptions options, int _CodeRequest) { CodeRequest = _CodeRequest; supportMediaResultListener = _supportMediaResultListener; var pickerIntent = new Intent(SupportMediaXFSetup.Activity, typeof(GalleryPickerActivity)); SupportMediaXFSetup.Activity.StartActivity(pickerIntent); }
public void IF_OpenCamera(ISupportMediaResultListener _supportMediaResultListener, SyncPhotoOptions options, int _CodeRequest) { CodeRequest = _CodeRequest; supportMediaResultListener = _supportMediaResultListener; var pickerIntent = new Intent(SupportMediaXFSetup.Activity, typeof(CamActivity)); pickerIntent.PutExtra(Utils.SubscribeImageFromCamera, Utils.SubscribeImageFromCamera); SupportMediaXFSetup.Activity.StartActivity(pickerIntent); }
public Task <List <SupportImageXF> > IF_OpenGallery(SyncPhotoOptions options) { ManualResetEvent manualResetEvent = new ManualResetEvent(false); List <SupportImageXF> result = new List <SupportImageXF>(); var currentPresent = NaviExtensions.GetTopController(); if (currentPresent == null) { return(Task.FromResult(new List <SupportImageXF>())); } return(Task.Factory.StartNew(() => { try { manualResetEvent.Reset(); SupportGalleryPickerController supportGalleryPickerController = null; currentPresent.InvokeOnMainThread(() => { var storyboard = UIStoryboard.FromName("SupportMediaStoryboard", null); supportGalleryPickerController = (SupportGalleryPickerController)storyboard.InstantiateViewController("SupportGalleryPickerController"); supportGalleryPickerController.syncPhotoOptions = options; supportGalleryPickerController.OnPicked += (obj) => { supportGalleryPickerController.InvokeOnMainThread(() => { try { supportGalleryPickerController.DismissViewController(true, () => { if (obj != null) { foreach (var item in obj) { result.Add(item.galleryImageXF); } } manualResetEvent.Set(); }); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); manualResetEvent.Set(); } }); }; currentPresent.PresentViewController(supportGalleryPickerController, true, null); }); manualResetEvent.WaitOne(); if (supportGalleryPickerController != null) { supportGalleryPickerController.Dispose(); } return result; } catch (Exception ex) { Console.WriteLine(ex); return result; } })); }
public async Task <GalleryImageXF> IF_SyncPhotoFromCloud(IGalleryPickerResultListener galleryPickerResultListener, GalleryImageXF imageSet, SyncPhotoOptions options) { try { bool FinishSync = false; Debug.WriteLine(imageSet.OriginalPath); var sortOptions = new PHFetchOptions(); sortOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; var FeechPhotoByIdentifiers = PHAsset.FetchAssetsUsingLocalIdentifiers( new string[] { imageSet.OriginalPath }, sortOptions).Cast <PHAsset>().FirstOrDefault(); if (FeechPhotoByIdentifiers != null) { var requestOptions = new PHImageRequestOptions() { NetworkAccessAllowed = true, DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat, ResizeMode = PHImageRequestOptionsResizeMode.None, }; var requestSize = new CoreGraphics.CGSize(options.Width, options.Height); requestSize = PHImageManager.MaximumSize; PHImageManager.DefaultManager.RequestImageForAsset(FeechPhotoByIdentifiers, requestSize, PHImageContentMode.AspectFit, requestOptions, (result, info) => { if (result != null) { var newImage = result.ResizeImage(options); imageSet.ImageRawData = newImage.AsJPEG(options.Quality).ToArray(); } FinishSync = true; }); do { if (FinishSync) { return(imageSet); } await Task.Delay(1000); } while (!FinishSync); } return(imageSet); } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); return(imageSet); } }
public void IF_OpenGallery(ISupportMediaResultListener _supportMediaResultListener, SyncPhotoOptions options, int _CodeRequest) { CodeRequest = _CodeRequest; supportMediaResultListener = _supportMediaResultListener; UIStoryboard storyboard = UIStoryboard.FromName("SupportMediaStoryboard", null); SupportGalleryPickerController controller = (SupportGalleryPickerController)storyboard.InstantiateViewController("SupportGalleryPickerController"); controller.syncPhotoOptions = options; NaviExtensions.OpenController(controller); }