将 View Rect 坐标转换为位图区域

Convert View Rect coordinates to bitmap area

我的相机屏幕里面有矩形(简单视图),要拍照片用户需要在矩形内放置物体,拍照后应用程序需要剪切图像并只显示矩形内的区域。

问题是 bitmap width/height 例如 5472/7296,以及与设备屏幕相关的 Rect 坐标 left-114 top-764 width-852 height-609,如何将其转换为位图上的区域以在 Rect's 区域切割位图。

我正在切割位图使用

Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)

我试过用百分比计算,但不太理想(大约有 5% 的误差)。

所以解决方案是将位图缩放到屏幕大小。

Bitmap bitmap = BitmapUtil.scaleBitmap(bitmap, screenWidth, screenHeight);

然后按 Rect 坐标裁剪

Bitmap cropBitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());