First Of all I'm doing this on Eclipse.
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.
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;
}
}