ImageView的坐标和位图像素之间的对应关系 - 机器人 [英] Correspondence between ImageView coordinates and Bitmap Pixels - Android

查看:1176
本文介绍了ImageView的坐标和位图像素之间的对应关系 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application I want the user to be able to select some content of an Image contained inside an ImageView.

要选择我的子类的的ImageView 类使其实现内容的 OnTouchListener 所以要在它上面绘制一个矩形与边框由用户决定。

To select the content I subclassed the ImageView class making it implement the OnTouchListener so to draw over it a rectangle with borders decided by the user.

下面的图(有它是如何运作的,你可以把它看成是当你用鼠标点击桌面上拖动鼠标一个想法)的结果的一个例子:

Here is an example of the result of the drawing (to have an idea of how it works you can think of it as when you click with the mouse on your desktop and drag the mouse):

现在我需要确定哪些像素位图 图像对应于所选择的部分。它是一种容易确定哪些是的ImageView 属于矩形的点,但我不知道怎么去对应的像素,因为 ImageView的具有比原始图像中的不同的纵横比。

Now I need to determine which pixels of the Bitmap image correspond to the selected part. It's kind of easy to determine which are the points of the ImageView belonging to the rectangle, but I don't know how to get the correspondent pixels, since the ImageView has a different aspect ratio than the original image.

我遵循的方法描述<一href="http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-the-bitmap">especially这里,<一个href="http://stackoverflow.com/questions/8002298/android-imageview-get-coordinates-of-tap-click-regardless-of-scroll-location">but这里也,但我不完全满意,因为在我看来,做对应的是1月1日的 ImageView的像素和点之间的并没有给我所有的对应的像素的原始图像的所选区域上。

I followed the approach described especially here, but also here, but am not fully satisfied because in my opinion the correspondence made is 1 on 1 between pixels and points on the ImageView and does not give me all the correspondent pixels on the original image to the selected area.

调用 hoveredRect ImageView的矩形它里面的要点是:

Calling hoveredRect the rectangle on the ImageView the points inside of it are:

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);
    }   
}

我如何获得矢量&lt;像素&GT; pixelsInImage 包含位图图像对应的像素?

How can I obtain a Vector<Pixels> pixelsInImage containing the correspondent pixels of the Bitmap image?

ADDED说明

我会解释我的问题好一点的环境:

I'll explain a little better the context of my issue:

我需要做一些图像处理所选择的部分,并希望   可以肯定,在矩形的所有像素得到处理。

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.

图片处理将在一个服务器上进行,但它需要知道哪些   像素来处理。服务器可以与图像实际尺寸,   Android应用程序只是告诉哪些像素处理到服务器通过传递一个向量   含的像素坐标

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

为什么我不喜欢的环节提出了上述解决方法:

And why I don't like the solutions proposed in the links above:

给出的1对1的方式变换的坐标的答案。这种做法显然是   不适用于我的任务,因为一个地区说50分的的ImageView 有一定的   在屏幕上的大小不能对应于相同的像素数的区域,在真实   形象,而是应该考虑不同的纵横比。

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 上的应用程序所示:

As example this is the area that should be selected if the image is smaller than the ImageView shown on the app:

推荐答案

利玛窦,

看来这是多大的错误,您可以(主观)容忍将像素您向服务器发送更多的问题。事实是,任何纵横比不出来一个漂亮整洁的整数,你必须决定哪个方向推的选择框。

It seems this is more a question of how much error you can (subjectively) tolerate in which pixels you send to the server. The fact remains that for any aspect ratio that does not come out to a nice neat integer, you have to decide which direction to 'push' your selection box.

您链接到的解决方案是非常好的解决方案。你要问自己:请问,如果我的形象过程中从屏幕上显示的选择框是一个像素注销该用户的通知?我的猜测是可能不会。我无法想象在触摸屏上选择与他们的大胖子的手指一个矩形,当用户将拥有那种像素precision反正:D

The solutions you linked to are perfectly good solutions. You have to ask yourself: Will the user notice if the image I process is one pixel off from the selection box shown on the screen? My guess is probably not. I can't imagine the user will have that sort of pixel precision anyways when selecting a rectangle with their big fat finger on a touchscreen :D

既然是这样的话,我只想让楼() -ing是铸造为整数时,照顾它的像素,你最终会传递到发生服务器。

Since this is the case, I would just let the floor()-ing that occurs when casting to an integer take care of which pixels you end up passing to the server.

让我们来看一个例子。

让我们来定义我们的ImageView和位图的宽度和高度为:

Let's define the width and height of our ImageView and Bitmap to be:

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

然后,你将用它来转换您的​​选择框的纵横比,它们之间将是:

Then the aspect ratios you will use to convert your selection box between them will be:

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

一些不错的难看的数字。无论像素,我在ImageView的会像这样的位图对应一个像素:

Some nice ugly numbers. Whatever pixel I am on in the ImageView will correspond to a pixel in the Bitmap like so:

BitmapPixelX = ImageViewPixelX * WidthRatio
BitmapPixelY = ImageViewPixelY * HeightRatio

现在,我把这个位图在屏幕上在我的ImageView供用户选择一个矩形,并在用户选择与一个矩形的左上角和在ImageView的右下坐标为这样:

Now, I put this Bitmap on the screen in my ImageView for the user to select a rectangle, and the user selects a rectangle with top-left and bottom-right coordinates in the ImageView as such:

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

我如何判断哪些像素在这些对应的位图?容易。的比例我们之前确定的。让我们来看看刚刚左上角坐标了。

How do I determine which pixels in the Bitmap these correspond to? Easy. The ratios we determined earlier. Let's look at just the top-left coordinates for now.

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

有关RectTopLeftX,我们发现自己在119 BitmapPixelX值,然后大约四分之一的方式进入像素。那么,如果我们楼()这个值和8.36,我们将派出像素相应BitmapPixelY值(119 8)来的服务器进行处理。如果我们 CEIL()这些价值观,我们将派出像素(120,9)服务器进行处理。 这是一部分,这是完全取决于你。

For RectTopLeftX, we find ourselves at a BitmapPixelX value of 119, and then about a quarter of a way into the pixel. Well, if we floor() this value and the corresponding BitmapPixelY value of 8.36, we will be sending pixel (119, 8) to the server for processing. If we were to ceil() these values, we will be sending pixel (120, 9) to the server for processing. This is the part that is entirely up to you.

您将(几乎)总是降落在一个像素的某些小数部分。无论你送你的土地,在像素,或者一个旁边是你的电话。我会说,这将是完全无法察觉你的用户,因此重申,只是让楼() -ing是铸造为整数时,小心发生它。

You will (nearly) always land in some fractional part of a pixel. Whether you send the pixel you land in, or the one next to it is your call. I would say that this is going to be entirely unnoticeable by your user, and so to reiterate, just let the floor()-ing that occurs when casting to an integer take care of it.

希望帮助!

一旦再次阅读的问题比较慢,我想我更好地了解你所要求/无所适从。我会用我上面的例子来说明。

Upon reading the question again more slowly, I think I better understand what you are asking/confused about. I will use my example above to illustrate.

您是说有176像素的位图,并在ImageView的400像素。因此,从一个到另一个的映射不是1:1,而这将搞清楚时什么像素拉出以进行处理会导致问题

You are saying that there are 176 pixels in the Bitmap, and 400 pixels in the ImageView. Therefore, the mapping from one to the other is not 1:1, and this will cause problems when figuring out what pixels to pull out for processing.

但事实并非如此!当您转换的矩形范围的坐标中的ImageView的位图的坐标,你只需给像素范围的迭代的 的位图 的这是不是说明对每个像素中的ImageView映射到了位图中的对应像素。

But it doesn't! When you convert the coordinates of the rectangle bounds in the ImageView to coordinates in the Bitmap, you're simply giving the range of pixels to iterate over in the Bitmap. It's not a description of how each individual pixel in the ImageView maps to a corresponding pixel in the Bitmap.

我希望澄清了我的困惑你的困惑。

I hope that clears up my confusion about your confusion.

这篇关于ImageView的坐标和位图像素之间的对应关系 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆