如何修复"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的这条消息。

我的日志如下

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.
  • 我在onFramePreview()中使用自定义相机预览方法来频繁使用捕获缓冲图像,如果它包含"人脸",然后发送它进行处理。
  • 当执行图像处理部分时,跳过该帧编舞和我没有得到预期的结果。
  • 我使用OpenCV进行图像处理

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

谢谢

使用更多线程,在后台线程上运行密集型进程,要么使用AsyncTask,要么使用this

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

然后调用

    NameOfRunnable.start();

    NameOfRunnable.run();

取决于你想用它做什么,要么是UI,要么只是后台信息,加上回调将帮助你做你需要做的

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

参考以下教程:tutorial

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

相关内容

  • 没有找到相关文章

最新更新