1

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

zubair Irfan
  • 103
  • 12

3 Answers3

0

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

tobzilla90
  • 607
  • 5
  • 9
  • Thanks for your response. Let me try this threading approach, will acknowledge you soon. – zubair Irfan Feb 09 '19 at 07:36
  • I tried this approach but getting error in accessing UI elements. It could be a good approach but in my case, need many changes in the previous code. I tried Brandon's answers and that works for me without doing any changes. BTW Thanks for your response. – zubair Irfan Feb 09 '19 at 08:36
  • You can acces the UI elements by passing them through Handlers and Loppers. Because you can't change a UI Element outside if the Main Thread. – tobzilla90 Feb 09 '19 at 10:25
0

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

Brandon
  • 1,158
  • 3
  • 12
  • 22
0

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

sandhya sasane
  • 1,334
  • 12
  • 19