Exemple #1
0
        public static UIImage ResizeImageWithAspectRatio(this UIImage imageSource, float scale)
        {
            if (scale > 1.0f)
            {
                return(imageSource);
            }

            using (CIContext c = CIContext.Create())
            {
                var sourceImage = CIImage.FromCGImage(imageSource.CGImage);

                var f = new CILanczosScaleTransform
                {
                    Scale       = scale,
                    Image       = sourceImage,
                    AspectRatio = 1.0f
                };


                var output = f.OutputImage;

                var cgi = c.CreateCGImage(output, output.Extent);
                return(UIImage.FromImage(cgi, 1.0f, imageSource.Orientation));
            }
        }
        public void CreateRefCount()
        {
            // Bug #7117

            CIImage  img         = new CIImage(CIColor.FromRgb(0.5f, 0.5f, 0.5f));
            Selector retainCount = new Selector("retainCount");

#if MONOMAC
            using (var ctx = new CIContext(null)) {
#else
            using (var ctx = CIContext.Create()) {
#endif
                using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 5, 5))) {
                    int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle);
                    Assert.AreEqual(1, rc, "CreateCGImage #a1");
                }

                using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 32, 32), CIImage.FormatARGB8, null)) {
                    int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle);
                    Assert.AreEqual(1, rc, "CreateCGImage #b1");
                }

#if !MONOMAC // CreateCGImage returning null on mac
                using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 5, 5), CIFormat.ARGB8, null)) {
                    int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle);
                    Assert.AreEqual(1, rc, "CreateCGImage #c1");
                }
#endif
            }
        }
Exemple #3
0
        public static UIImage FilteredImage(UIImage image)
        {
            var progress = NSProgress.FromTotalUnitCount(-1);

            progress.Cancellable = false;
            progress.Pausable    = false;

            UIImage outputImage;

            var filter = new CIPhotoEffectTransfer();

            var cgImage = image.CGImage;
            var ciImage = CIImage.FromCGImage(cgImage);

            filter.SetValueForKey(ciImage, new NSString("inputImage"));
            var outputCIImage = filter.OutputImage;
            var ciContext     = CIContext.Create();
            var outputCGImage = ciContext.CreateCGImage(outputCIImage, outputCIImage.Extent);

            outputImage = UIImage.FromImage(outputCGImage);

            outputCGImage.Dispose();
            ciContext.Dispose();
            outputCIImage.Dispose();
            ciImage.Dispose();
            cgImage.Dispose();
            filter.Dispose();

            progress.CompletedUnitCount = 1;
            progress.TotalUnitCount     = 1;

            return(outputImage);
        }
        private void setupImage(ImageSource source, UIImageView target)
        {
            IImageSourceHandler handler = null;

            if (source is UriImageSource)
            {
                handler = new ImageLoaderSourceHandler();
            }
            else if (source is FileImageSource)
            {
                handler = new FileImageSourceHandler();
            }
            else if (source is StreamImageSource)
            {
                handler = new StreamImagesourceHandler();
            }

            if (handler != null)
            {
                UIImage image = target.Image;
                if (image != null)
                {
                    using (var context = CIContext.Create())
                        using (var inputImage = CIImage.FromCGImage(image.CGImage))
                            using (var filter = new CIGaussianBlur()
                            {
                                Image = inputImage, Radius = 15
                            })
                                using (var resultImage = context.CreateCGImage(filter.OutputImage, inputImage.Extent))
                                    target.Image = new UIImage(resultImage);
                }
            }
        }
Exemple #5
0
        void BlurImage(UIImageView imageView, UIImage image, float radius, float transitionDuration)
        {
            Task.Run(() =>
            {
                using (var context = CIContext.Create())
                    using (var inputImage = CIImage.FromCGImage(image.CGImage))
                        using (var filter = new CIGaussianBlur()
                        {
                            Image = inputImage, Radius = radius
                        })
                            using (var resultImage = context.CreateCGImage(filter.OutputImage, inputImage.Extent))
                            {
                                var blurimage = new UIImage(resultImage);

                                Device.BeginInvokeOnMainThread(() =>
                                {
                                    imageView.Hidden = false;

                                    if (transitionDuration > 0.0f)
                                    {
                                        UIView.Transition(imageView, transitionDuration, UIViewAnimationOptions.TransitionCrossDissolve, animation: () =>
                                        {
                                            imageView.Image = blurimage;
                                        }, completion: null);
                                    }
                                    else
                                    {
                                        imageView.Image = blurimage;
                                    }
                                });
                            }
            });
        }
        /// <summary>
        /// Resize image maintain aspect ratio
        /// </summary>
        /// <param name="imageSource"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static UIImage ResizeImageWithAspectRatio(this UIImage imageSource, float scale)
        {
            if (scale > 1.0f)
            {
                return(imageSource);
            }


            using (var c = CIContext.Create())
            {
                var sourceImage = CIImage.FromCGImage(imageSource.CGImage);
                var orientation = imageSource.Orientation;
                imageSource?.Dispose();

                var transform = new CILanczosScaleTransform
                {
                    Scale       = scale,
                    Image       = sourceImage,
                    AspectRatio = 1.0f
                };

                var output = transform.OutputImage;
                using (var cgi = c.CreateCGImage(output, output.Extent))
                {
                    transform?.Dispose();
                    output?.Dispose();
                    sourceImage?.Dispose();

                    return(UIImage.FromImage(cgi, 1.0f, orientation));
                }
            }
        }
Exemple #7
0
        public static UIImage MakeUIImageFromCIImage(this CIImage ciImage)
        {
            CIContext context = CIContext.Create();
            CGImage   cgImage = context.CreateCGImage(ciImage, ciImage.Extent);//[context createCGImage: ciImage fromRect:[ciImage extent]];

            UIImage uiImage = new UIImage(cgImage);

            cgImage.Dispose();

            return(uiImage);
        }
Exemple #8
0
        /// <summary>
        /// Resize image maintain aspect ratio
        /// </summary>
        /// <param name="imageSource"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static UIImage ResizeImageWithAspectRatio(this UIImage imageSource, float scale)
        {
            if (scale > 1.0f)
            {
                return(imageSource);
            }


            using (var c = CIContext.Create())
            {
                var sourceImage = CIImage.FromCGImage(imageSource.CGImage);
                var orientation = imageSource.Orientation;
                imageSource?.Dispose();

                CILanczosScaleTransform transform = null;

                /*if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
                 * {
                 *      transform = new CILanczosScaleTransform
                 *      {
                 *              Scale = scale,
                 *              InputImage = sourceImage,
                 *              AspectRatio = 1.0f
                 *      };
                 * }
                 * else*/
                //{
                transform = new CILanczosScaleTransform
                {
                    Scale       = scale,
                    Image       = sourceImage,
                    AspectRatio = 1.0f
                };
                //}

                var output = transform.OutputImage;
                using (var cgi = c.CreateCGImage(output, output.Extent))
                {
                    transform?.Dispose();
                    output?.Dispose();
                    sourceImage?.Dispose();

                    return(UIImage.FromImage(cgi, 1.0f, orientation));
                }
            }
        }
        /// <summary>
        /// Resize image maintain aspect ratio
        /// </summary>
        /// <param name="imageSource"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static UIImage ResizeImageWithAspectRatio(this UIImage imageSource, float scale)
        {
            if (scale > 1.0f)
            {
                return(imageSource);
            }


            using var c = CIContext.Create();
            var sourceImage = CIImage.FromCGImage(imageSource.CGImage);
            var orientation = imageSource.Orientation;

            imageSource?.Dispose();

            CILanczosScaleTransform transform = null;

            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                transform = new CILanczosScaleTransform
                {
                    Scale       = scale,
                    InputImage  = sourceImage,
                    AspectRatio = 1.0f
                };
            }
            else
            {
                transform = new CILanczosScaleTransform
                {
                    Scale = scale,
#pragma warning disable CS0618 // Type or member is obsolete
                    Image = sourceImage,
#pragma warning restore CS0618 // Type or member is obsolete
                    AspectRatio = 1.0f
                };
            }

            var output = transform.OutputImage;
            using var cgi = c.CreateCGImage(output, output.Extent);
            transform?.Dispose();
            output?.Dispose();
            sourceImage?.Dispose();

            return(UIImage.FromImage(cgi, 1.0f, orientation));
        }
        public async void Sample3Png()
        {
            await _api.Init("eng");

            using (var stream = LoadSample("sample3.png"))
                using (var image = new CIImage(NSData.FromStream(stream)))
                    using (var blur = new CIGaussianBlur())
                        using (var context = CIContext.Create()) {
                            blur.SetDefaults();
                            blur.Image  = image;
                            blur.Radius = 0;
                            using (var outputCiImage = context.CreateCGImage(blur.OutputImage, image.Extent))
                                using (var newImage = new UIImage(outputCiImage)) {
                                    var result = await((TesseractApi)_api).Recognise(newImage);
                                    Assert.IsTrue(result);
                                    Assert.AreEqual(
                                        "the quick brown fox\njumps over the lazy dog-\n\nTHE QUICK BROlLIN FOX\nJUMPS OVER THE LAZY DOG.\n\n",
                                        _api.Text);
                                }
                        }
        }