/** * 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); }
/** * 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(); } }