Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setter for OverlayView's cropViewRect in order to make possible to show custom cropping rect when uCrop starts #582

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

andreiolar
Copy link

@andreiolar andreiolar commented Oct 21, 2019

Made possible to set the mCropViewRect from OverlayView to a custom value in order to present a custom cropping rect when uCrop starts.

Background: We wanted to integrate document scanning and cropping together. So instead of doing it in multiple steps, we wanted to be able to do it in one step. Basically we wanted to suggest our users what to crop when taking pictures of a document, so we needed a way to highlight the document inside the image.

Solution: In simple terms, the solution was to be able to set mCropViewRect from OverlayView, so I exposed a setter.
Problem was that scanning things from the image outside uCrop gives you coordinates/points in the image space. But the View does not necessarily have the same dimensions as the image you have passed. So RectUtils#convertImageSpaceRectToCropViewRect does exactly what it says, converts those points you have passed into points that correspond to the current view, so that the OverlayView's rect is rendered as it would have been on the image itself.
Only condition for this to work is that setCropViewRectInImageSpace must be used in combination with setMaxBitmapSize (large number), so that the image does not get downscaled. Because if the image gets downscaled, the points/coordinates won't work anymore.

Related issues (maybe more related issues that were closed):
#571
#569
#468

Screenshots:
Before, when you open uCrop, your view will look like this

Screenshot_20191023_130249_no tripletex debug

Open uCrop after doing some edge detection on the above image and passing options.setCropViewRectInImageSpace(new RectF(...)); will result in the following

Screenshot_20191023_135233_no tripletex debug

It would be a really wanted feature for us. I am waiting for questions or improvements. I expect some, since this is my first PR ever, but we kind of really need this functionality :)

@andreiolar andreiolar changed the title Added setter for mCropViewRect Added setter for OverlayView's cropViewRect in order to make possible to show custom cropping rect when uCrop starts Oct 23, 2019
@inepineee
Copy link

Any chance this will be merged soon? 🙏

@xiaoyvyv
Copy link

Calculate the transformed RectF using the following method for greater accuracy.

    /**
     * 原始相对图片的坐标框根据图片的变换矩阵映射变化后对应的坐标框
     *
     * @param currentImageMatrix 当前图片的矩阵
     * @param cropViewRect       原生的坐标框
     * @return 变化映射后对应的坐标框
     */
    public static RectF calculateTransformedRect(Matrix currentImageMatrix, RectF cropViewRect) {
        float[] lt = {cropViewRect.left, cropViewRect.top};
        float[] rb = {cropViewRect.right, cropViewRect.bottom};

        currentImageMatrix.mapPoints(lt);
        currentImageMatrix.mapPoints(rb);

        return new RectF(lt[0], lt[1], rb[0], rb[1]);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants