android将图像划分为子图像,识别 [英] android divide image into sub images ,recognizing

查看:162
本文介绍了android将图像划分为子图像,识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像划分为子图像,当我点击图像的一部分时会给我区域的名称,例如,这是我的问题如何识别图像中的区域,或者如何将图像划分为子图像并在imageViews中使用

I want to divide the image into sub images and when I click on a part of the image will give me the name of the region, for example, this is my question how to recognize a region from the image, or how to divide the image into sub-images and use it in imageViews

并提前感谢

推荐答案

在我看来,@ fractalwrench的想法非常适合你的情况。基本步骤如下:

In my opinion @fractalwrench's idea is quite good for your case. Basic steps are listed below.


  • 子类Android ImageView 。例如, MultiRegionImageView

  • 覆盖其 onTouchEvent 方法。 (每当用户触摸视图时都会调用此方法)

  • 用户触摸图像,从而调用 onTouchEvent 并提供精确触摸点(x,y)。

  • Subclass Android ImageView. For example, MultiRegionImageView.
  • Override its onTouchEvent method. (This method gets called whenever user touches the view)
  • User touches the image and thereby onTouchEvent is called and provides the exact touch point (x, y).

声明另一个方法或接口,确定给定点在哪个区域。例如, getRegionByPoint(int x,int y)

Declare another method or interface which determines at which region a given point is. For example, getRegionByPoint(int x, int y)

如果您想突出显示该区域边界,你可以使用路径。首先,您应该定义路径并将它们保存到原始文件(例如XML)中,然后使用区域ID,获取其路径并最终在主图像上绘制该路径。

If you would like to highlight that region boundaries, you could use paths. First off, you should define paths and save them into a raw file (XML, for example), then using region ID, fetch its path and finally draw that path over the main image.

要在主图像上绘制路径,还应覆盖 onDraw ImageView 的方法class并使用 canvas.drawPath();

For drawing a path over the main image, you should also override onDraw method of ImageView class and use canvas.drawPath();

public class MultiRegionImageView extends ImageView {
    RegionProvider mRegionProvider;
    int mId = -1;
    private Paint mPaint;

    public MultiRegionImageView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mId = mRegionProvider.getRegionIdByPoint(event.getX(), event.getY());
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mId != -1) {
            canvas.drawPath(mRegionProvider.getRegionBoundaryPath(mId), mPaint);
        }
    }

    public interface RegionProvider{
        int getRegionIdByPoint(float x, float y);
        Path getRegionBoundaryPath(int id);
    }
}

这篇关于android将图像划分为子图像,识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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