gpt4 book ai didi

java - 如何在 OpenCV for Android 的 Touch 框架上绘制多个矩形?

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:52 25 4
gpt4 key购买 nike

首先,我在 Eclipse 上执行此操作。

1.我希望在接收到 Touch 事件时绘制一个矩形。2.那个矩形应该是持久的,并且在另一个 Touchevent 上应该绘制另一个矩形。3.我已经设法让它在单个 TouchEvent 中保持不变,之后它会根据坐标移动。4.所以基本上,由于不同的触摸事件,我应该有多个矩形。

我正在考虑遍历数组...但我仍然很困惑请帮忙!这个不行……我要求改进……谢谢!还 list 和东西是正确的,权限是正确的!代码有点像:

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;
}

最佳答案

将矩形存储在列表中非常容易。我做了一些应该有效的小调整。

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;
}

但是请记住,当您不再需要矩形时,您需要释放并清除您的列表以释放内存。希望对您有所帮助。

关于java - 如何在 OpenCV for Android 的 Touch 框架上绘制多个矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com