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

当相机最大预览尺寸远小于屏幕尺寸时会报错Crop rectangle does not fit within image data. #395

Closed
StaticCoder opened this issue Jan 21, 2019 · 1 comment

Comments

@StaticCoder
Copy link

StaticCoder commented Jan 21, 2019

我的平板是1920x1200的分辨率,竖屏,摄像头为500w像素,通过Camera.Parameters.getSupportedPreviewSizes()获取支持的预览尺寸,发现最大为1280x768,远小于1920x1200,这样的话就会导致"Crop rectangle does not fit within image data.",查看库里的代码发现:

scanBoxAreaRect = mScanBoxView.getScanBoxAreaRect(height);

source = new PlanarYUVLuminanceSource(data, width, height, scanBoxAreaRect.left,

scanBoxAreaRect.top, scanBoxAreaRect.width(), scanBoxAreaRect.height(), false);

public Rect getScanBoxAreaRect(int previewHeight) {
if (mIsOnlyDecodeScanBoxArea && getVisibility() == View.VISIBLE) {
Rect rect = new Rect(mFramingRect);
float ratio = 1.0f * previewHeight / getMeasuredHeight();

        float centerX = rect.exactCenterX();
        float centerY = rect.exactCenterY();

        float halfWidth = rect.width() / 2f;
        float halfHeight = rect.height() / 2f;
        float newHalfWidth = halfWidth * ratio;
        float newHalfHeight = halfHeight * ratio;

        rect.left = (int) (centerX - newHalfWidth);
        rect.right = (int) (centerX + newHalfWidth);
        rect.top = (int) (centerY - newHalfHeight);
        rect.bottom = (int) (centerY + newHalfHeight);
        return rect;
    } else {
        return null;
    }
}

这样子会导致:

if (left + width > dataWidth || top + height > dataHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}

虽然能通过app:qrcv_isOnlyDecodeScanBoxArea="false"来解决,但是这样就会导致功能缺失,希望能解决一下,谢谢。

@StaticCoder
Copy link
Author

经研究将库里的几行代码改一下就行了:
rect.left = (int) (centerX - newHalfWidth);
rect.right = (int) (centerX + newHalfWidth);
rect.top = (int) (centerY - newHalfHeight);
rect.bottom = (int) (centerY + newHalfHeight);
改成
rect.left = (int)(centerX * ratio - newHalfWidth);
rect.right = (int)(centerX * ratio + newHalfWidth);
rect.top = (int)(centerY * ratio - newHalfHeight);
rect.bottom = (int)(centerY * ratio + newHalfHeight);

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

No branches or pull requests

1 participant