Question

In my application I have detected the edges of a Image using Android opencv methods. from based on the resulted coordinates(four coordinates) I have drawn a lines and circles in the four corners.These coordinates are returned for selected image(by considering image's top left as(0,0)).I need to add the task of,user may change the drawn lines in OnTouch() by touch the drawn circles in Imageview. for that I need (x,y) co-ordinates of the selected touched part in ImageView by considering ImageView top left as (0,0). I got the x,y co-ordinates for touched part by using this code event.getX(),event.getY() but it returned the co-ordinates based on the screen.so if I touch the top left circle in ImageView it would returned the varying (x,y) co-ordinates, it is not same as what I drawn circle by edge detection methods.

My tried Code

 private int fieldImgXY[] = new int[2];


    public static  float ptX1 = 0;
    public static float ptY1 = 0;
    public static float ptX2 = 0;
    public static float ptY2 = 0;
    public static float ptX3 = 0;
    public static float ptY3 = 0;
    public static float ptX4 = 0;
    public static float ptY4 = 0;
 /* ptx1,pty1,ptx2,ptY2 ,ptX3,ptY3,ptX4,ptY4     will get values from Opencv returned coordinates */


        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);

            imageView.getLocationOnScreen(fieldImgXY);

        }

@Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            float x1 = motionEvent.getX();

            float y1 = motionEvent.getY();

             float xOnField = x1 - fieldImgXY[0];
             float yOnField = y1 - fieldImgXY[1];


            if((x1 >= ptX1 - 10 && x1 <= ptX1 + 10)
                    && (y1 >= ptY1 - 10 && y1 <= ptY1 + 10)) {
                bool = true;
                selectedPos = 1;
            }else if((x1 >= ptX2 - 10 && x1 <= ptX2 + 10)
                    && (y1 >= ptY2 - 10 && y1 <= ptY2 + 10)) {
                bool = true;
                selectedPos = 2;
            }
else if((x1 >= ptX3 - 10 && x1 <= ptX3 + 10)
                    && (y1 >= ptY3 - 10 && y1 <= ptY3 + 10)) {
                bool = true;
                selectedPos = 3;
            }


            else if((x1 >= ptX4 - 10  && x1 <= ptX4 + 10)
                    && (y1 >= ptY4 - 10 && y1 <= ptY4 + 10)) {
                bool = true;
                selectedPos = 4;
            }
            else
            {
                bool = false;
                selectedPos = 0;
            }
            return true;
}
else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
//          float x1 = motionEvent.getX();
//          float y1 = motionEvent.getY();
//          Log.i(LOGCAT, "x1=" + x1 + "::y1=" + y1);
            if (bool) {
                bool1 = true;
                bool = false;
            }
            return true;
        } else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
            float x1 = motionEvent.getX();
            float y1 = motionEvent.getY();
            Log.i(LOGCAT, "x1=" + x1 + "::y1=" + y1);
            if (bool1) {
                clearLine();// for clear the canvas and lines in that image.
                if(selectedPos == 1)
                {
                    ptX1 = x1;
                    ptY1 = y1;
                }
                else if(selectedPos == 2)
                {
                    ptX2 = x1;
                    ptY2 = y1;
                }
                else if(selectedPos == 3)
                {
                    ptX3 = x1;
                    ptY3 = y1;
                }
                else if(selectedPos == 4)
                {
                    ptX4 = x1;
                    ptY4 = y1;
                }
                drawLine(); // draw new lines and circles for latest values
                bool1 = false;
            }
            return true;
        }
}
Was it helpful?

Solution

I used dp = px * (160/dpi) formula for converting the OnTouch event motionEvent.getX(),motionEvent.getY() returned coordinates(pixel coordinates) into dp coordinates. now i can move the circle in screen using MotionEvent.ACTION_MOVE.

DisplayMetrics dm = getResources().getDisplayMetrics();             

                        float  x1 = event.getX()*(160f/dm.densityDpi);
                        float  y1 = event.getY()*(160f/dm.densityDpi);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top