简体   繁体   English

Android + OpenCV:OnTouch getX(),getY()不会返回正确的值

[英]Android + OpenCV: OnTouch getX(), getY() doesn't return right values

I am using OpenCV4Android FaceDetection sample. 我正在使用OpenCV4Android FaceDetection示例。 I have global table with rectangles of currently spoted faces: 我有全局表,其中包含当前发现的面孔的矩形:

private Rect[] facesArray;

also global floats to store onClick coordinates, 还有全局浮点数来存储onClick坐标,

private float onClickX;
private float onClickY;

which are generated from onTouchEvent: 从onTouchEvent生成:

@Override
public boolean onTouchEvent(MotionEvent e) {

    onClickX = e.getX();
    onClickY = e.getY();

    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // something not important for this matter happens here 
    }

    return super.onTouchEvent(e);
}

In onCameraFrame method before returning mat with view I am doing: 在onCameraFrame方法中,然后返回带有视图的垫子,我正在这样做:

Core.circle(mRgba,new Point(onClickX,onClickY), 40,  new Scalar(0, 255, 0, 255));

So what happens. 那么会发生什么。 I draw a small, green circle on coordinates that are fetched in onTouchEvent and sent to global variables. 我在onTouchEvent中获取并发送给全局变量的坐标上绘制了一个绿色的小圆圈。 Those variables (onClickX, onClickY) are read by onCameraFrame and used for core.circle() function. 这些变量(onClickX,onClickY)由onCameraFrame读取,并用于core.circle()函数。 My problem is that, circle isn't drawn precisely under my finger but always in lower-right place. 我的问题是,圆圈不是在手指的正下方绘制,而是始终在右下角。 This is what happens: https://dl.dropboxusercontent.com/u/108321090/device-2013-07-24-004207.png 这就是发生的情况: https : //dl.dropboxusercontent.com/u/108321090/device-2013-07-24-004207.png

And this circle is always in the same direction/position to my finger whenever it is on screen, and meets it in top-left corner, dissapears on bottom right corner (goes outside screen). 无论何时在屏幕上,该圆圈始终与我的手指在同一方向/位置,并在左上角与之相遇,在右下角消失(位于屏幕外)。 I tried using getRawX, geyRawY -> result is the same, I don't understeand why get commands doesn't return precise tap position but somewhere near it. 我尝试使用getRawX,geyRawY->结果是相同的,我不明白为什么get命令不会返回精确的分接头位置,而是返回它附近的某个位置。 I have no clue how to fix this. 我不知道如何解决这个问题。

This is the solution to your problem. 这是解决您的问题的方法。

which is, 就是

@Override
public boolean onTouch(View arg0,MotionEvent event) {

double cols = mRgba.cols();// mRgba is your image frame
double rows = mRgba.rows();

double xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
double yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

onClickX = (float)(event).getX() - xOffset;
onClickY = (float)(event).getY() - yOffset;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM