Exemplo n.º 1
0
        /**
         * Gets the crop window's position relative to the source Bitmap (not the image
         * displayed in the CropImageView).
         *
         * @return a RectF instance containing cropped area boundaries of the source Bitmap
         */

        public RectF GetActualCropRect()
        {
            try
            {
                Rect displayedImageRect = ImageViewUtil.getBitmapRectCenterInside(mBitmap, mImageView);

                // Get the scale factor between the actual Bitmap dimensions and the
                // displayed dimensions for width.
                float actualImageWidth    = mBitmap.Width;
                float displayedImageWidth = displayedImageRect.Width();
                float scaleFactorWidth    = actualImageWidth / displayedImageWidth;

                // Get the scale factor between the actual Bitmap dimensions and the
                // displayed dimensions for height.
                float actualImageHeight    = mBitmap.Height;
                float displayedImageHeight = displayedImageRect.Height();
                float scaleFactorHeight    = actualImageHeight / displayedImageHeight;

                // Get crop window position relative to the displayed image.
                float displayedCropLeft   = EdgeManager.LEFT.coordinate - displayedImageRect.Left;
                float displayedCropTop    = EdgeManager.TOP.coordinate - displayedImageRect.Top;
                float displayedCropWidth  = Edge.getWidth();
                float displayedCropHeight = Edge.getHeight();

                // Scale the crop window position to the actual size of the Bitmap.
                float actualCropLeft   = displayedCropLeft * scaleFactorWidth;
                float actualCropTop    = displayedCropTop * scaleFactorHeight;
                float actualCropRight  = actualCropLeft + displayedCropWidth * scaleFactorWidth;
                float actualCropBottom = actualCropTop + displayedCropHeight * scaleFactorHeight;

                // Correct for floating point errors. Crop rect boundaries should not
                // exceed the source Bitmap bounds.
                actualCropLeft   = Math.Max(0f, actualCropLeft);
                actualCropTop    = Math.Max(0f, actualCropTop);
                actualCropRight  = Math.Min(mBitmap.Width, actualCropRight);
                actualCropBottom = Math.Min(mBitmap.Height, actualCropBottom);

                var actualCropRect = new RectF(actualCropLeft,
                                               actualCropTop,
                                               actualCropRight,
                                               actualCropBottom);

                return(actualCropRect);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return(null);
        }
Exemplo n.º 2
0
        /**
         * Gets the cropped image based on the current crop window.
         *
         * @return a new Bitmap representing the cropped image
         */

        public Bitmap GetCroppedImage()
        {
            try
            {
                Rect displayedImageRect = ImageViewUtil.getBitmapRectCenterInside(mBitmap, mImageView);

                // Get the scale factor between the actual Bitmap dimensions and the
                // displayed dimensions for width.
                float actualImageWidth    = mBitmap.Width;
                float displayedImageWidth = displayedImageRect.Width();
                float scaleFactorWidth    = actualImageWidth / displayedImageWidth;

                // Get the scale factor between the actual Bitmap dimensions and the
                // displayed dimensions for height.
                float actualImageHeight    = mBitmap.Height;
                float displayedImageHeight = displayedImageRect.Height();
                float scaleFactorHeight    = actualImageHeight / displayedImageHeight;

                // Get crop window position relative to the displayed image.
                float cropWindowX      = EdgeManager.LEFT.coordinate - displayedImageRect.Left;
                float cropWindowY      = EdgeManager.TOP.coordinate - displayedImageRect.Top;
                float cropWindowWidth  = Edge.getWidth();
                float cropWindowHeight = Edge.getHeight();

                // Scale the crop window position to the actual size of the Bitmap.
                float actualCropX      = cropWindowX * scaleFactorWidth;
                float actualCropY      = cropWindowY * scaleFactorHeight;
                float actualCropWidth  = cropWindowWidth * scaleFactorWidth;
                float actualCropHeight = cropWindowHeight * scaleFactorHeight;

                // Crop the subset from the original Bitmap.
                Bitmap croppedBitmap = Bitmap.CreateBitmap(mBitmap,
                                                           (int)actualCropX,
                                                           (int)actualCropY,
                                                           (int)actualCropWidth,
                                                           (int)actualCropHeight);

                return(croppedBitmap);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return(null);
        }
Exemplo n.º 3
0
 protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
 {
     try
     {
         if (mBitmap != null)
         {
             Rect bitmapRect = ImageViewUtil.getBitmapRectCenterInside(mBitmap, this);
             mCropOverlayView.SetBitmapRect(bitmapRect);
         }
         else
         {
             mCropOverlayView.SetBitmapRect(EMPTY_RECT);
         }
     }
     catch (Exception ex)
     {
         ex.ToString();
     }
 }
Exemplo n.º 4
0
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            try
            {
                var widthMode  = (int)MeasureSpec.GetMode(widthMeasureSpec);
                int widthSize  = MeasureSpec.GetSize(widthMeasureSpec);
                var heightMode = (int)MeasureSpec.GetMode(heightMeasureSpec);
                int heightSize = MeasureSpec.GetSize(heightMeasureSpec);

                if (mBitmap != null)
                {
                    base.OnMeasure(widthMeasureSpec, heightMeasureSpec);

                    // Bypasses a baffling bug when used within a ScrollView, where
                    // heightSize is set to 0.
                    if (heightSize == 0)
                    {
                        heightSize = mBitmap.Height;
                    }

                    int desiredWidth;
                    int desiredHeight;

                    double viewToBitmapWidthRatio  = Double.PositiveInfinity;
                    double viewToBitmapHeightRatio = Double.PositiveInfinity;

                    // Checks if either width or height needs to be fixed
                    if (widthSize < mBitmap.Width)
                    {
                        viewToBitmapWidthRatio = widthSize / (double)mBitmap.Width;
                    }
                    if (heightSize < mBitmap.Height)
                    {
                        viewToBitmapHeightRatio = heightSize / (double)mBitmap.Height;
                    }

                    // If either needs to be fixed, choose smallest ratio and calculate
                    // from there
                    if (viewToBitmapWidthRatio != Double.PositiveInfinity ||
                        viewToBitmapHeightRatio != Double.PositiveInfinity)
                    {
                        if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio)
                        {
                            desiredWidth  = widthSize;
                            desiredHeight = (int)(mBitmap.Height * viewToBitmapWidthRatio);
                        }
                        else
                        {
                            desiredHeight = heightSize;
                            desiredWidth  = (int)(mBitmap.Width * viewToBitmapHeightRatio);
                        }
                    }

                    // Otherwise, the picture is within frame layout bounds. Desired
                    // width is
                    // simply picture size
                    else
                    {
                        desiredWidth  = mBitmap.Width;
                        desiredHeight = mBitmap.Height;
                    }

                    int width  = GetOnMeasureSpec(widthMode, widthSize, desiredWidth);
                    int height = GetOnMeasureSpec(heightMode, heightSize, desiredHeight);

                    mLayoutWidth  = width;
                    mLayoutHeight = height;

                    Rect bitmapRect = ImageViewUtil.getBitmapRectCenterInside(mBitmap.Width,
                                                                              mBitmap.Height,
                                                                              mLayoutWidth,
                                                                              mLayoutHeight);
                    mCropOverlayView.SetBitmapRect(bitmapRect);

                    // MUST CALL THIS
                    SetMeasuredDimension(mLayoutWidth, mLayoutHeight);
                }
                else
                {
                    mCropOverlayView.SetBitmapRect(EMPTY_RECT);
                    SetMeasuredDimension(widthSize, heightSize);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }