How I run code during 5 second then jump to other code in Android studio Java?

265 views Asked by At

I need to try these code for when running this code after 5 seconds then will jump to another function. How i can combine, is using thread?

private Mat  get_template(CascadeClassifier clasificator, Rect area,int size){
    Mat template = new Mat();
    Mat mROI = mGray.submat(area);
    MatOfRect eyes = new MatOfRect();
    Point iris = new Point();
    Rect eye_template = new Rect();
    clasificator.detectMultiScale(mROI, eyes, 1.15, 2,Objdetect.CASCADE_FIND_BIGGEST_OBJECT|Objdetect.CASCADE_SCALE_IMAGE, new Size(30,30),new Size());

    Rect[] eyesArray = eyes.toArray();
    for (int i = 0; i < eyesArray.length; i++){
        Rect e = eyesArray[i];
        e.x = area.x + e.x;
        e.y = area.y + e.y;
        Rect eye_only_rectangle = new Rect((int)e.tl().x,(int)( e.tl().y + e.height*0.4),(int)e.width,(int)(e.height*0.6));
        // reduce ROI
        mROI = mGray.submat(eye_only_rectangle);
        Mat vyrez = mRgba.submat(eye_only_rectangle);
        // find the darkness point
        Core.MinMaxLocResult mmG = Core.minMaxLoc(mROI);
        // draw point to visualise pupil
        Core.circle(vyrez, mmG.minLoc,2, new Scalar(255, 255, 255, 255),2);
        iris.x = mmG.minLoc.x + eye_only_rectangle.x;
        iris.y = mmG.minLoc.y + eye_only_rectangle.y;
        eye_template = new Rect((int)iris.x-size/2,(int)iris.y-size/2 ,size,size);
        Core.rectangle(mRgba,eye_template.tl(),eye_template.br(),new Scalar(255, 0, 0, 255), 2);
        // copy area to template
        template = (mGray.submat(eye_template)).clone();
        return template;
    }
    return template;
}

Resource from: http://romanhosek.cz/android-eye-detection-and-tracking-with-opencv/

Is using these code?

try{
    //print something here
    Thread.sleep(3000); //sleep for 3 seconds
    //print something else here
}
catch(InterruptedException e){    
    System.out.println("got interrupted!");
}

How should I combine? Actually the part of the code is detect the eye, now I want to do is detect the eye with 5 seconds after that will jump to another function code.

What code I can put to combine with them? Need help to answer if you have any idea.

Thanks

1

There are 1 answers

8
Charuක On BEST ANSWER
  1. Run first method

2.For executing something in the UI Thread after 5 seconds:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
     // Stop 1st one 
            // run 2nd 
  }
}, 5000);