java – ImageView坐标与位图像素之间的对应关系 – Android

在我的应用程序中,我希望用户能够选择 ImageView中包含的图像的某些内容.

要选择内容,我将ImageView类子化,使其实现OnTouchListener,以便绘制一个带有用户决定的边框的矩形.

这是一个绘图结果的例子(有一个想法,如何工作,你可以想到它,当你点击鼠标在桌面上拖动鼠标):

现在我需要确定位图图像的哪些像素对应于所选的部分.这是很容易确定哪些是ImageView属于矩形的点,但我不知道如何获取通信像素,因为ImageView具有不同于原始图像的长宽比.

我遵循了描述especially here,but also here的方法,但我不完全满意,因为在我看来,ImageView上的对应关系是像素和点之间的1比1,并且不会将原始图像上的所有相应像素给出到所选区域.

调用hovedRectViewViewView上的矩形是:

class Point {
    float x,y;
    @Override
    public String toString() {
        return x + "," + y;
    }
}

Vector<Point> pointsInRect = new Vector<Point>();

for( int x = hoveredRect.left; x <= hoveredRect.right; x++ ){
    for( int y = hoveredRect.top; y <= hoveredRect.bottom; y++ ){

        Point pointInRect = new Point();
        pointInRect.x = x;
        pointInRect.y = y;
        pointsInRect.add(pointInRect);
    }   
}

如何获取矢量<像素> pixelInImage包含Bitmap图像的相应像素?

补充说明

我会解释一下我的问题的上下文:

I need to do some image processing on the selected part,and want to
be sure that all the pixels in the rectangle get processed.

The image processing will be done on a server but it needs to know exactly which
pixels to process. Server works with image with real dimensions,
android app just tells which pixels to process to the server by passing a vector
containing the pixel coordinates

为什么我不喜欢上述链接中提出的解决方案:

The answers given transform coordinates with a 1 to 1 fashion. This approach clearly is
not valid for my task,since an area of say 50 points in the ImageView of a certain
size on the screen cannot correspond to an area of the same number of pixels in the real
image,but should consider the different aspect ratio.

例如,如果图像小于应用程序上显示的ImageView,则应选择该区域:

解决方法

利玛窦,

这似乎是一个问题,您可以(主观地)容忍您发送到服务器的像素有多少错误.事实上,对于任何不会出现一个漂亮整洁整数的宽高比,您必须决定“推”选择框的方向.

您链接的解决方案是完美的解决方案.你必须问自己:用户是否注意到,如果我处理的图像是从屏幕上显示的选择框中的一个像素?我的猜测可能不是当用触摸屏上的大胖手指选择一个矩形时,我无法想像用户将具有像素精度:D

由于是这种情况,所以我可以让抛出一个整数时发生的floor() – 来处理最终传递给服务器的像素.

我们来看一个例子.

让我们将ImageView和Bitmap的宽度和高度定义为:

ImageViewWidth = 400,ImageViewHeight = 150
BitmapWidth = 176,BitmapHeight = 65

然后,您将使用它们在其间转换选择框的宽高比为:

WidthRatio = BitmapWidth / ImageViewWidth = 175 / 400 = 0.44
HeightRatio = BitmapHeight / ImageViewHeight = 65 / 150 = 0.44

一些不错的丑陋的数字. ImageView中的任何像素都将对应于Bitmap中的像素,如下所示:

BitmapPixelX = ImageViewPixelX * WidthRatio
BitmapPixelY = ImageViewPixelY * HeightRatio

现在我把这个Bitmap放在我的ImageView的屏幕上,供用户选择一个矩形,用户在ImageView中选择一个有左上角和右下角坐标的矩形:

RectTopLeftX = 271,RectTopLeftY = 19
RectBottomRightX = 313,RectBottomRightY = 42

如何确定位图中哪些像素对应?简单.我们之前确定的比率.现在我们来看看左上角的坐标.

RectTopLeftX * WidthRatio = 271 * .44 = 119.24
RectTopLeftY * HeightRatio = 19 * .44 = 8.36

对于RectTopLeftX,我们发现我们的BitmapPixelX值为119,然后是四分之一的像素.那么,如果我们把这个值和对应的BitmapPixelY的值为8.36,我们将把像素(119,8)发送到服务器进行处理.如果我们要ceil()这些值,我们将发送像素(120,9)到服务器进行处理.这是完全取决于你的部分.

你将(几乎)总是落在像素的一些小数部分.无论你发送你所在的像素,还是旁边的像素是你的电话.我会说这是你的用户完全不明显的,所以要重申一下,只要让抛出一个整数就会发生的floor() – .

希望有帮助!

[编辑]

再次慢慢阅读这个问题,我想我更好的理解你的问题/困惑.我将用上面的例子来说明.

您说的是Bitmap中有176个像素,ImageView中有400个像素.因此,从一个到另一个的映射不是1:1,这将在找出要提取的像素进行处理时会产生问题.

但它不!当您将ImageView中的矩形边界的坐标转换为位图中的坐标时,您只需在“位图”中给出像素范围即可迭代.它不是描述ImageView中每个像素如何映射到Bitmap中相应的像素.

我希望清除我对你的困惑的困惑.

以上是来客网为你收集整理的java – ImageView坐标与位图像素之间的对应关系 – Android全部内容,希望文章能够帮你解决java – ImageView坐标与位图像素之间的对应关系 – Android所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。