Exemplo n.º 1
0
        private void OnDisCardClick()
        {
            SetResult(Result.Canceled);
            Finish();

            MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), null));
        }
Exemplo n.º 2
0
        private void SaveOutput(Bitmap croppedImage)
        {
            if (_saveUri != null)
            {
                try
                {
                    using (var outputStream = ContentResolver.OpenOutputStream(_saveUri))
                    {
                        if (outputStream != null)
                        {
                            croppedImage.Compress(_outputFormat, 75, outputStream);
                        }
                    }
                    //raise event
                    MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), croppedImage));
                }
                catch (Exception ex)
                {
                    Log.Error(GetType().Name, ex.Message);
                }

                var extras = new Bundle();
                SetResult(Result.Ok, new Intent(_saveUri.ToString())
                          .PutExtras(extras));
            }
            else
            {
                Log.Error(GetType().Name, "not defined image url");
            }
            croppedImage.Recycle();
            Finish();
        }
Exemplo n.º 3
0
        public override void OnBackPressed()
        {
            base.OnBackPressed();

            SetResult(Result.Canceled);
            Finish();

            //raise event
            MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), null));
        }
Exemplo n.º 4
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            RequestWindowFeature(WindowFeatures.NoTitle);
            SetContentView(Resource.Layout.CropImage);

            _imageView = FindViewById <CropImageView> (Resource.Id.image);

            ShowStorageToast(this);

            var extras = Intent.Extras;

            if (extras != null)
            {
                _imagePath = extras.GetString("image-path");

                _saveUri = GetImageUri(_imagePath);
                if (extras.GetString(MediaStore.ExtraOutput) != null)
                {
                    _saveUri = GetImageUri(extras.GetString(MediaStore.ExtraOutput));
                }

                _bitmap = GetBitmap(_imagePath);

                _aspectX = extras.GetInt("aspectX");
                _aspectY = extras.GetInt("aspectY");
                _outputX = extras.GetInt("outputX");
                _outputY = extras.GetInt("outputY");
                _scale   = extras.GetBoolean("scale", true);
                _scaleUp = extras.GetBoolean("scaleUpIfNeeded", true);

                if (extras.GetString("outputFormat") != null)
                {
                    _outputFormat = Bitmap.CompressFormat.ValueOf(extras.GetString("outputFormat"));
                }
            }

            if (_bitmap == null)
            {
                Finish();
                //raise event
                MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), null));
                return;
            }

            Window.AddFlags(WindowManagerFlags.Fullscreen);

            FindViewById <Button> (Resource.Id.discard).Click     += (sender, e) => OnDiscardClick();
            FindViewById <Button> (Resource.Id.save).Click        += (sender, e) => OnSaveClicked();
            FindViewById <Button> (Resource.Id.rotateLeft).Click  += (o, e) => OnRotateClicked(-90);
            FindViewById <Button> (Resource.Id.rotateRight).Click += (o, e) => OnRotateClicked(90);

            _imageView.SetImageBitmapResetBase(_bitmap, true);
            AddHighlightView();
        }
Exemplo n.º 5
0
        private void OnDisCardClick()
        {
            try
            {
                SetResult(Result.Canceled);
                Finish();

                //raise event
                MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), null));
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 6
0
        private void OnSaveClicked()
        {
            // TODO this code needs to change to use the decode/crop/encode single
            // step api so that we don't require that the whole (possibly large)
            // bitmap doesn't have to be read into memory
            if (Saving)
            {
                return;
            }

            Saving = true;

            var width  = Crop.CropRect.Width();
            var height = Crop.CropRect.Height();

            var croppedImage = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);

            {
                var canvas  = new Canvas(croppedImage);
                var dstRect = new Rect(0, 0, width, height);
                canvas.DrawBitmap(_bitmap, Crop.CropRect, dstRect, null);
            }

            // If the output is required to a specific size then scale or fill
            if (_outputX != 0 && _outputY != 0)
            {
                if (_scale)
                {
                    // Scale the image to the required dimensions
                    croppedImage.Transform(new Matrix(), _outputX, _outputY, _scaleUp);
                }
                else
                {
                    // Don't scale the image crop it to the size requested.
                    // Create an new image with the cropped image in the center and
                    // the extra space filled.
                    var b      = Bitmap.CreateBitmap(_outputX, _outputY, Bitmap.Config.Rgb565);
                    var canvas = new Canvas(b);

                    var srcRect = Crop.CropRect;
                    var dstRect = new Rect(0, 0, _outputX, _outputY);

                    var dx = (srcRect.Width() - dstRect.Width()) / 2;
                    var dy = (srcRect.Height() - dstRect.Height()) / 2;

                    // If the srcRect is too big, use the center part of it.
                    srcRect.Inset(Math.Max(0, dx), Math.Max(0, dy));

                    // If the dstRect is too big, use the center part of it.
                    dstRect.Inset(Math.Max(0, -dx), Math.Max(0, -dy));

                    // Draw the cropped bitmap in the center
                    canvas.DrawBitmap(_bitmap, srcRect, dstRect, null);

                    // Set the cropped bitmap as the new bitmap
                    croppedImage.Recycle();
                    croppedImage = b;
                }
            }

            // Return the cropped image directly or save it to the specified URI.
            var myExtras = Intent.Extras;

            if (myExtras != null && (myExtras.GetParcelable("data") != null || myExtras.GetBoolean("return-data")))
            {
                var extras = new Bundle();
                extras.PutParcelable("data", croppedImage);
                SetResult(Result.Ok, (new Intent()).SetAction("inline-data").PutExtras(extras));
                Finish();
            }
            else
            {
                BackgroundJob.StartBackgroundJob(this, null, Resources.GetString(Resource.String.savingImage), () => SaveOutput(croppedImage), _mHandler);
            }

            //raise event
            MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), croppedImage));
        }
Exemplo n.º 7
0
        private void OnSaveClicked()
        {
            if (Saving)
            {
                return;
            }

            Saving = true;

            var r = Crop.CropRect;

            var width  = r.Width();
            var height = r.Height();

            var croppedImage = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);

            {
                var canvas  = new Canvas(croppedImage);
                var dstRect = new Rect(0, 0, width, height);
                canvas.DrawBitmap(_bitmap, r, dstRect, null);
            }

            if (_outputX != 0 && _outputY != 0)
            {
                if (_scale)
                {
                    var old = croppedImage;
                    croppedImage = Util.Transform(new Matrix(),
                                                  croppedImage, _outputX, _outputY, _scaleUp);
                    if (old != croppedImage)
                    {
                        old.Recycle();
                    }
                }
                else
                {
                    var b = Bitmap.CreateBitmap(_outputX, _outputY,
                                                Bitmap.Config.Rgb565);
                    var canvas = new Canvas(b);

                    var srcRect = Crop.CropRect;
                    var dstRect = new Rect(0, 0, _outputX, _outputY);

                    var dx = (srcRect.Width() - dstRect.Width()) / 2;
                    var dy = (srcRect.Height() - dstRect.Height()) / 2;

                    srcRect.Inset(Math.Max(0, dx), Math.Max(0, dy));

                    dstRect.Inset(Math.Max(0, -dx), Math.Max(0, -dy));

                    canvas.DrawBitmap(_bitmap, srcRect, dstRect, null);

                    croppedImage.Recycle();
                    croppedImage = b;
                }
            }

            var myExtras = Intent.Extras;

            if (myExtras != null &&
                (myExtras.GetParcelable("data") != null || myExtras.GetBoolean("return-data")))
            {
                var extras = new Bundle();
                extras.PutParcelable("data", croppedImage);
                SetResult(Result.Ok,
                          (new Intent()).SetAction("inline-data").PutExtras(extras));
                Finish();
            }
            else
            {
                var b = croppedImage;
                BackgroundJob.StartBackgroundJob(this, null, "Saving image", () => SaveOutput(b), _mHandler);
            }

            MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), croppedImage));
        }
Exemplo n.º 8
0
 public override void OnBackPressed()
 {
     MediaCroped?.Invoke(this, new XViewEventArgs(nameof(MediaCroped), null));
     base.OnBackPressed();
 }