本文介绍了如何绘制多个矩形上OpenCV的为Android上触摸的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我在Eclipse上这样做。中

First Of all I'm doing this on Eclipse.

1.我想画一个矩形上接收触摸事件。2.That矩形应该是持久的,在一个又一个的TouchEvent应借鉴另一个矩形。3.我已经成功地得到它迁延后,它根据坐标转移单的TouchEvent。4.So基本上我应该有多个矩形由于不同的触摸事件。

1.I wish to draw a rectangle on reception of Touch event. 2.That rectangle should be persistent and on an another Touchevent should draw another rectangle.3.I have managed to get it persistent for a single TouchEvent after which it shifts according to coordinates. 4.So basically I should have multiple rectangles due to different touch events.

我想通过数组迭代的...但我仍然感到困惑请帮助!这一次也不行......我所要求的改善...谢谢!同时体现和东西是正确和权限得到适当!code是有点像:

I am thinking of iterating through arrays...But I'm still confused pls help!This one does not work... I'm asking for improvements...Thanks! Also manifests and stuff is proper and permissions are taken properly!code is somewhat like:

package code.e14.balldetector;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;

public class MainActivity extends Activity implements CvCameraViewListener2, OnTouchListener {
    private static final String TAG = "OCVBallTracker";

private CameraBridgeViewBase mOpenCvCameraView;
private boolean              mIsJavaCamera = true;
private Mat                    mRgba;
int i=0;
private Double[] h=new Double[20];
private Double[] k=new Double[20]; 
private double x=0;
private double y=0;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.setOnTouchListener(MainActivity.this);
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

public MainActivity() {
    Log.i(TAG, "Instantiated new " + this.getClass());
}


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_main);
    if (mIsJavaCamera)
        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.java_surface_view);
    else
        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.native_surface_view);

    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);

    mOpenCvCameraView.setCvCameraViewListener(this);


}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.i(TAG, "called onCreateOptionsMenu");


    return true;
}



public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);

}

public void onCameraViewStopped() {
     mRgba.release();
}
@Override
public boolean onTouch(View arg0,MotionEvent event) {

    double cols = mRgba.cols();
    double rows = mRgba.rows();

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




     h[i] = (double)(event).getX() - xOffset;
     k[i] = (double)(event).getY() - yOffset;

   h[i]=x;
   k[i]=y;

    Log.i(TAG, "Touch image coordinates: (" + h[i] + ", " + k[i] + ")");


   i++;





    return false;// don't need subsequent touch events
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba=inputFrame.rgba();





     Core.rectangle(mRgba, new Point(x-100,y-100),new Point(x+100,y+100),new Scalar( 0, 0, 255 ),0,8, 0 );



    return mRgba;
}

}

推荐答案

这是很容易储存的矩形在列表中。我已经做了一些小的调整应该工作。

It is quite easy to store the rectangles in a List. I have made some small adjustments that should work.

private List<Rect> ListOfRect = new ArrayList<Rect>();

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

  double cols = mRgba.cols();
  double rows = mRgba.rows();

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

  h[i] = (double)(event).getX() - xOffset;
  k[i] = (double)(event).getY() - yOffset;

 h[i]=x;
 k[i]=y;

 ListOfRect.add(new Rect(x-100, y-100, x+100, y+100));

 Log.i(TAG, "Touch image coordinates: (" + h[i] + ", " + k[i] + ")");

 i++;

 return false;// don't need subsequent touch events
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
   mRgba=inputFrame.rgba();

   for(int i=0; i<ListOfRect.size(); i++){
       Core.rectangle(mRgba, ListOfRect.get(i).tl(), ListOfRect.get(i).br(),new Scalar( 0, 0, 255 ),0,8, 0 );}

   return mRgba;
}

但是请记住,你需要释放并清除您的列表,以释放内存,当你不需要的矩形了。我希望它帮助。

However keep in mind that you need to release and clear your list to free memory when you don't need the rectangles anymore. I hope it helped.

这篇关于如何绘制多个矩形上OpenCV的为Android上触摸的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 06:52