问题描述
限时送ChatGPT账号..我有一个带有矩形(简单视图)的相机屏幕,要使用户需要将对象放置在矩形内,拍摄照片后,应用程序需要剪切图像并仅显示矩形内的区域.
I have camera screen with rectangle(simple view) inside, to make photo the user need to place object inside the rectangle, after taking picture the app need to cut image and show only the area inside rectangle.
从camera2
api 我接收图像并将其转换为 bitmap
.我在屏幕上有矩形坐标.
From camera2
api i receives the image and converting him to bitmap
.
I have the Rectangle coordinates on screen.
问题是 bitmap
width
/height
例如 5472/7296,以及 Rect
坐标与设备屏幕 left-114 top-764 width-852 height-609 相关,我如何将其转换为位图区域以在 Rect's
区域剪切位图.
The problem is that the bitmap
width
/height
for example 5472/7296, and the Rect
coordinates related to device screen left-114 top-764 width-852 height-609, how i can convert it to area on bitmap to cut the bitmap in Rect's
area.
我正在使用
Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)
我试过用百分比来计算,但它并不理想(大约有 5% 的误差).
I have tried to calculate with percentage but it not ideal(about 5% inaccuracy).
推荐答案
所以解决方案是将位图缩放到屏幕大小.
So the solution was to scale the bitmap to screens size.
Bitmap bitmap = BitmapUtil.scaleBitmap(bitmap, screenWidth, screenHeight);
然后通过 Rect
坐标裁剪
Bitmap cropBitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());
这篇关于将 View Rect 坐标转换为位图区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论