Java 如何修复“应用程序可能在其主线程上做了太多工作”(Android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23509060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:27:42  来源:igfitidea点击:

How to fixed "The application may be doing too much work on its main thread" (Android)

javaandroidmultithreadingopencvimage-processing

提问by Bhadresh

I am developing an android application in which I can do high level image processing and I used custom camera preview to capture images, but when I check logcat after launching the application, I found this message from Choreographer Skipped 440 frames! The application may be doing too much work on its main thread.

我正在开发一个 android 应用程序,我可以在其中进行高级图像处理,并使用自定义相机预览来捕获图像,但是当我在启动应用程序后检查 logcat 时,我发现了来自 Choreographer 的这条消息Skipped 440 frames! The application may be doing too much work on its main thread

My log is as bellow

我的日志如下

05-07 10:04:11.480: I/CameraPreview(28340): Orientation Changed
05-07 10:04:11.990: I/Choreographer(28340): Skipped 145 frames!  The application    may be doing too much work on its main thread.
05-07 10:04:15.620: I/Choreographer(28340): Skipped 212 frames!  The application      may be doing too much work on its main thread.
05-07 10:04:20.590: I/CameraPreview(28340): +++++++++++++++++++++++++++++++++++++++
05-07 10:04:20.590: I/CameraPreview(28340): starting recognition process
05-07 10:04:22.490: I/FaceDetector(28340): Number of faces found: 1
05-07 10:04:22.490: I/Value X(28340): 273
05-07 10:04:22.500: I/Value Y(28340): 106
05-07 10:04:22.500: I/Bitmap Width :(28340): 640
05-07 10:04:22.510: I/Bitmap Height :(28340): 480
05-07 10:04:22.520: I/CameraPreview(28340): Bitmap Size : 205 : 201
05-07 10:04:22.520: I/Face Recognition(28340):        ===========================================
05-07 10:04:22.520: I/Face Recognition(28340): recognizeFace (single face)
05-07 10:04:22.550: I/CameraPreview(28340): Previwe Stoped Face Detected....
05-07 10:04:22.550: I/CameraPreview(28340): Stopping preview in SurfaceDestroyed().
05-07 10:04:22.720: I/CameraPreview(28340): Total Recognition process took:       2.127638333
05-07 10:04:22.720: I/Choreographer(28340): Skipped 440 frames!  The application may    be doing too much work on its main thread.
  • I used custom camera preview in onFramePreview()method to frequently capture buffer images and if it contains a "Human Face" then send it for processing.
  • When the image processing part is executed, it's frame is skipped by Choreographer and im not getting expected result.
  • I used OpenCVfor the image processing
  • 我在onFramePreview()方法中使用自定义相机预览来频繁捕获缓冲图像,如果它包含“人脸”,则将其发送以进行处理。
  • 当执行图像处理部分时,它的帧被编舞者跳过,我没有得到预期的结果。
  • 我用于OpenCV图像处理

Please help me to solve this stuff. I searched many times in StackOverflow but not getting a proper answer.

请帮我解决这个问题。我在 StackOverflow 中搜索了很多次,但没有得到正确的答案。

Thanks

谢谢

采纳答案by Adam

Use more threading, Run intensive processes on the background thread, either AsyncTask or use this.

使用更多线程,在后台线程上运行密集的进程,或者 AsyncTask 或者使用这个。

    public Runnable NameOfRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            while (true)
            {
            // TODO add code to refresh in background
                try
                {

                    Thread.sleep(1000);// sleeps 1 second
                    //Do Your process here.
                } catch (InterruptedException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }

    }
};

then call

然后打电话

    NameOfRunnable.start();

or

或者

    NameOfRunnable.run();

depends on what you want to do with it, either ui or just back ground info, plus callbacks will help do what you need to do

取决于你想用它做什么,用户界面或只是背景信息,加上回调将帮助你做你需要做的事情

回答by Kakarot

Is the image processing part being done on the main thread ? If yes then try to do all heavy processing in another thread and after processing is done pass it back to the Main thread. A better alternative would be to use AyncTask and perform the image processing in the doInBackground() method.

图像处理部分是否在主线程上完成?如果是,则尝试在另一个线程中进行所有繁重的处理,并在处理完成后将其传递回主线程。更好的选择是使用 AyncTask 并在 doInBackground() 方法中执行图像处理。

Refer to the following tutorial : tutorial

请参阅以下教程:教程

回答by user1969053

Its like u r doing lot of processing in main thread.You should use a separate thread or asynctask for doing processing or buffering of images and after that can update ui . If using asychtask do background operation in doinBackground() and update ui in onPostExecute and If u use thread then for updating ui u need to call runOnUithread to update the UI

它就像你在主线程中做很多处理一样。你应该使用一个单独的线程或异步任务来处理或缓冲图像,然后可以更新 ui 。如果使用 asychtask 在 doinBackground() 中执行后台操作并在 onPostExecute 中更新 ui 如果你使用线程然后更新 ui 你需要调用 runOnUithread 来更新 UI