Crop Photo Recipe?

Is there a recipe that that has an Android/IOS crop photo implementation?

Having trouble drawing a transparent, moveable rectangle over my image view, tried a few methods (android xml, implementing imageview...) but could use a working example to understand it better.

I can crop it fine using bitmapfactory after.

Thanks in advance.

Posts

  • DylanDempseyDylanDempsey CAMember

    Am I the first person that needed to implement simple photo cropping for the user??

  • This is some code i used while back for resizing and croping image. It's for .net but you can convert it without problems...
    Hope it helps.

    public static Bitmap ResizeAndCropImage(Bitmap image, int reqWidth, int reqHeight)
            {
                //get original width and height of the incoming image
                var originalWidth = image.Width; // 1000
                var originalHeight = image.Height; // 800
    
                //get the percentage difference in size of the dimension that will change the least
                var percWidth = ((float)reqWidth/ (float)originalWidth); // 0.2
                var percHeight = ((float)reqHeight/ (float)originalHeight); // 0.25
                var percentage = Math.Max(percHeight, percWidth); // 0.25
    
                //get the ideal width and height for the resize (to the next whole number)
                var width = (int)Math.Max(originalWidth * percentage, reqWidth); // 250
                var height = (int)Math.Max(originalHeight * percentage, reqHeight); // 200
    
                //actually resize it
                using (var resizedImage = new Bitmap(width, height))
                {
                    using (var graphics = Graphics.FromImage((Image)resizedImage))
                    {
                        //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.DrawImage(image, 0, 0, width, height);
                    }
    
                    //work out the coordinates of the top left pixel for cropping
                    var x = (width - reqWidth) / 2; // 25
                    var y = (height - reqHeight) / 2; // 0
    
                    //create the cropping rectangle
                    var rectangle = new Rectangle(x, y, reqWidth, reqHeight); // 25, 0, 200, 200
    
                    // Cropp the resized image and return it.
                    return resizedImage.Clone(rectangle, resizedImage.PixelFormat);
                }
            }
    
  • DylanDempseyDylanDempsey CAMember
    edited October 2013

    Cool, thanks for the reply. I have the backend code using bitmapfactory, was wondering if there was something cross platform to handle the UI (a clickable square for the user to specificy the crop boundaries). I still have to do IOS but I ended up creating it from scratch on Android by creating a drawable transparent rectangle shape overlayed inside the image, then implementing a tap and hold listener so the user can drag it. The XY boundaries are then sent to a similar function like the one you posted (except using bitmapfactory). Not that hard just figured there might be a nice component or library as it seems like a pretty common thing to implement. Cheers.

  • RaphaelChiorlinRanieriRaphaelChiorlinRanieri BRMember ✭✭✭

    @DylanDempsey Did you achieve your goal? Could you share your solution? Im stucked in the same problem. Thanks

  • JavierHolcmanJavierHolcman ARMember ✭✭

    Just for iOS to crop, zoom and rotate photos: TOCropViewController

  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭

    Hi, I done this in Xamarin.Forms, here is the sample.......

Sign In or Register to comment.