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

编程入门 行业动态 更新时间:2024-10-21 22:55:32
本文介绍了将 View Rect 坐标转换为位图区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-23 03:39:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   转换为   坐标   区域   View

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!