Exemple #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);
        }
Exemple #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);
        }
        private void DrawRuleOfThirdsGuidelines(Canvas canvas)
        {
            try
            {
                float left   = EdgeManager.LEFT.coordinate;
                float top    = EdgeManager.TOP.coordinate;
                float right  = EdgeManager.RIGHT.coordinate;
                float bottom = EdgeManager.BOTTOM.coordinate;


                float cx     = (left + right) / 2;
                float cy     = (top + bottom) / 2;
                float radius = (right - left) / 2;

                var circleSelectionPath = new Path();
                circleSelectionPath.AddCircle(cx, cy, radius, Path.Direction.Cw);
                canvas.ClipPath(circleSelectionPath, Region.Op.Replace);


                // Draw vertical guidelines.
                float oneThirdCropWidth = Edge.getWidth() / 3;

                float x1 = left + oneThirdCropWidth;
                canvas.DrawLine(x1, top, x1, bottom, mGuidelinePaint);
                float x2 = right - oneThirdCropWidth;
                canvas.DrawLine(x2, top, x2, bottom, mGuidelinePaint);

                // Draw horizontal guidelines.
                float oneThirdCropHeight = Edge.getHeight() / 3;

                float y1 = top + oneThirdCropHeight;
                canvas.DrawLine(left, y1, right, y1, mGuidelinePaint);
                float y2 = bottom - oneThirdCropHeight;
                canvas.DrawLine(left, y2, right, y2, mGuidelinePaint);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
Exemple #4
0
        /**
         * Returns whether or not you can re-scale the image based on whether any edge would be out of bounds.
         * Checks all the edges for a possibility of jumping out of bounds.
         *
         * @param Edge the Edge that is about to be expanded
         * @param imageRect the rectangle of the picture
         * @param aspectratio the desired aspectRatio of the picture.
         *
         * @return whether or not the new image would be out of bounds.
         */
        public bool isNewRectangleOutOfBounds(Edge edge, Rect imageRect, float aspectRatio)
        {
            float offset = edge.snapOffset(imageRect);

            switch (edgeType)
            {
            case EdgeType.LEFT:
                if (edge.Equals(EdgeManager.TOP))
                {
                    float top    = imageRect.Top;
                    float bottom = EdgeManager.BOTTOM.coordinate - offset;
                    float right  = EdgeManager.RIGHT.coordinate;
                    float left   = AspectRatioUtil.calculateLeft(top, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.BOTTOM))
                {
                    float bottom = imageRect.Bottom;
                    float top    = EdgeManager.TOP.coordinate - offset;
                    float right  = EdgeManager.RIGHT.coordinate;
                    float left   = AspectRatioUtil.calculateLeft(top, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;

            case EdgeType.TOP:
                if (edge.Equals(EdgeManager.LEFT))
                {
                    float left   = imageRect.Left;
                    float right  = EdgeManager.RIGHT.coordinate - offset;
                    float bottom = EdgeManager.BOTTOM.coordinate;
                    float top    = AspectRatioUtil.calculateTop(left, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.RIGHT))
                {
                    float right  = imageRect.Right;
                    float left   = EdgeManager.LEFT.coordinate - offset;
                    float bottom = EdgeManager.BOTTOM.coordinate;
                    float top    = AspectRatioUtil.calculateTop(left, right, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;

            case EdgeType.RIGHT:
                if (edge.Equals(EdgeManager.TOP))
                {
                    float top    = imageRect.Top;
                    float bottom = EdgeManager.BOTTOM.coordinate - offset;
                    float left   = EdgeManager.LEFT.coordinate;
                    float right  = AspectRatioUtil.calculateRight(left, top, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.BOTTOM))
                {
                    float bottom = imageRect.Bottom;
                    float top    = EdgeManager.TOP.coordinate - offset;
                    float left   = EdgeManager.LEFT.coordinate;
                    float right  = AspectRatioUtil.calculateRight(left, top, bottom, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;


            case EdgeType.BOTTOM:
                if (edge.Equals(EdgeManager.LEFT))
                {
                    float left   = imageRect.Left;
                    float right  = EdgeManager.RIGHT.coordinate - offset;
                    float top    = EdgeManager.TOP.coordinate;
                    float bottom = AspectRatioUtil.calculateBottom(left, top, right, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                else if (edge.Equals(EdgeManager.RIGHT))
                {
                    float right  = imageRect.Right;
                    float left   = EdgeManager.LEFT.coordinate - offset;
                    float top    = EdgeManager.TOP.coordinate;
                    float bottom = AspectRatioUtil.calculateBottom(left, top, right, aspectRatio);

                    return(isOutOfBounds(top, left, bottom, right, imageRect));
                }
                break;
            }
            return(true);
        }