1
votes

On UI Thread:

   AsyncCaller asyncCaller = new AsyncCaller();
                           asyncCaller.doInBackground();

In AsyncTask: @Override protected Void doInBackground(Void... params) {

       Mat orignal = new Mat(image1.getHeight(), image1.getWidth(), CvType.CV_8UC3);
       Mat v_blur = new Mat(image1.getHeight(), image1.getWidth(), CvType.CV_8UC3);
       Utils.bitmapToMat(image1, orignal);
       Imgproc.GaussianBlur(orignal, v_blur, new Size(3, 3), 0);
       // blur completed
       Mat to_zero = new Mat(image1.getHeight(), image1.getWidth(), CvType.CV_8UC4);
       Imgproc.threshold(v_blur, to_zero, 100, 100, Imgproc.THRESH_TOZERO);
       Bitmap bmp = image1;
       Log.e("background", "in bg");
       Utils.bitmapToMat(bmp, to_zero);
       for (int j = 0; j < bmp.getWidth(); j++) {
           for (int i = 0; i < bmp.getHeight(); i++) {
               int pixelValue = bmp.getPixel(j, i);
               int red = Color.red(pixelValue);
               int green = Color.green(pixelValue);
               int blue = Color.blue(pixelValue);

               if (red > 10 && blue > 10 && green > 10) {
                   int led_bulb = checkForAlreadyExistance(j, i);
                   if (led_bulb == -1) {
                       int start_x = getMinValue(j);
                       int start_y = getMinValue(i);
                       int end_x = getMaxValue(j, bmp.getWidth());
                       int end_y = getMaxValue(i, bmp.getHeight());
                       Log.e("crop_values", start_x + " " + end_x + " " + start_y + " " + end_y);
                       LEDs.add(new LedBoundingBox(LED++, end_x, start_x, end_y, start_y, 0, 0, 0.0));
                   } else {
                       if (j > LEDs.get(led_bulb).inner_x_max) {
                           LEDs.get(led_bulb).inner_x_max = j;
                       }
                       if (j < LEDs.get(led_bulb).inner_x_min) {
                           LEDs.get(led_bulb).inner_x_min = j;
                       }
                       if (i > LEDs.get(led_bulb).inner_y_max) {
                           LEDs.get(led_bulb).inner_y_max = i;
                       }
                       if (i < LEDs.get(led_bulb).inner_y_min) {
                           LEDs.get(led_bulb).inner_y_min = i;
                       }

                   }

               }
           }
       }
       Log.e("bgComplete", "complete");
       return null;
   }

I am working on image processing using android OpenCV library. First I did all this work on UI thread, but faced UI hanging issue. So I shifted it to background thread. But Still I am facing UI hanging issue. Can anyone explain why this happening and any possible solution? Thanks

3

3 Answers

0
votes

Use asyncCaller.execute():, don’t call the doInBackground() directly

Right now you’re calling this method on the UI thread instead of starting the AsyncTask and getting the task to call the thread on its own.

AsyncTask task = new AsyncTask(); task.execute(paramsForDoInBackground);

Is how it should be called

0
votes

Use a Java Thread instead of the Async Task for your work! An Async Task is only for small operations.

Please check here: https://stackoverflow.com/a/18480297/4632620

0
votes

The Simplest, Best and visual example with demonstration how exactly it should be, if you do not want to freeze the UI. Everyone who are facing this problem or anyone who are unaware of how to code for the android projects, should see and follow this style :

Video Demonstration of Avoiding UI freezing