Beispiel #1
0
        public string CropAndSave(GalleryMediaModel model)
        {
            FileStream stream = null;
            Bitmap     sized  = null;
            Bitmap     croped = null;

            try
            {
                var parameters    = model.Parameters;
                var rotation      = parameters.Rotation;
                var previewBounds = parameters.PreviewBounds;
                var cropBounds    = parameters.CropBounds;

                var options = new BitmapFactory.Options {
                    InJustDecodeBounds = true
                };
                BitmapFactory.DecodeFile(model.Path, options);

                var pWidth = (int)Math.Round((previewBounds.Right - previewBounds.Left) / parameters.Scale);
                var dZ     = (rotation % 180 > 0 ? options.OutHeight : options.OutWidth) / (float)pWidth;

                var width  = (int)Math.Round((cropBounds.Right - cropBounds.Left) * dZ / parameters.Scale);
                var height = (int)Math.Round((cropBounds.Bottom - cropBounds.Top) * dZ / parameters.Scale);
                var x      = (int)Math.Max(Math.Round(-previewBounds.Left * dZ / parameters.Scale), 0);
                var y      = (int)Math.Max(Math.Round(-previewBounds.Top * dZ / parameters.Scale), 0);

                var sampleSize = BitmapUtils.CalculateInSampleSize(width, height, BitmapUtils.MaxImageSize, BitmapUtils.MaxImageSize);

                width  = width / sampleSize;
                height = height / sampleSize;
                x      = x / sampleSize;
                y      = y / sampleSize;

                options.InSampleSize             = sampleSize;
                options.InJustDecodeBounds       = false;
                options.InPreferQualityOverSpeed = true;
                sized = BitmapFactory.DecodeFile(model.Path, options);


                switch (rotation)
                {
                case 90:
                {
                    var b = width;
                    width  = height;
                    height = b;

                    b = x;
                    x = y;
                    y = sized.Height - b - height;
                    break;
                }

                case 180:
                {
                    x = sized.Width - width - x;
                    y = sized.Height - height - y;
                    break;
                }

                case 270:
                {
                    var b = width;
                    width  = height;
                    height = b;

                    b = y;
                    y = x;
                    x = sized.Width - b - width;
                    break;
                }
                }

                x = Math.Max(x, 0);
                y = Math.Max(y, 0);

                if (x + width > sized.Width)
                {
                    width = sized.Width - x;
                }
                if (y + height > sized.Height)
                {
                    height = sized.Height - y;
                }

                var matrix = new Matrix();
                matrix.SetRotate(rotation);

                croped = Bitmap.CreateBitmap(sized, x, y, width, height, matrix, true);

                var directory = new Java.IO.File(Context.CacheDir, Constants.Steepshot);
                if (!directory.Exists())
                {
                    directory.Mkdirs();
                }

                var outPath = $"{directory}/{Guid.NewGuid()}.jpeg";
                stream = new FileStream(outPath, FileMode.Create);
                croped.Compress(Bitmap.CompressFormat.Jpeg, 99, stream);

                var args = new Dictionary <string, string>
                {
                    { ExifInterface.TagImageLength, croped.Height.ToString() },
                    { ExifInterface.TagImageWidth, croped.Width.ToString() },
                    { ExifInterface.TagOrientation, "1" },
                };

                BitmapUtils.CopyExif(model.Path, outPath, args);

                return(outPath);
            }
            catch (Exception ex)
            {
                AppSettings.Logger.Error(ex);
                return(string.Empty);
            }
            finally
            {
                stream?.Dispose();
                BitmapUtils.ReleaseBitmap(sized);
                BitmapUtils.ReleaseBitmap(croped);
            }
        }