Android Asynctask to process live video frames

前端 未结 2 1809
不知归路
不知归路 2020-12-31 22:21

I am using OpenCV to attempt to do some live video processing. Since the processing is fairly heavy, it delays the output frames significantly, making the live stream look

相关标签:
2条回答
  • 2020-12-31 22:53

    I would do something like that :

    // Example value for a timeout.
    private static final long TIMEOUT = 1000L;
    
    private BlockingQueue<Mat> frames = new LinkedBlockingQueue<Mat>();
    
    Thread worker = new Thread() {
        @Override
        public void run() {
            while (running) {
                Mat inputFrame = frames.poll(TIMEOUT, TimeUnit.MILLISECONDS);
                if (inputFrame == null) {
                    // timeout. Also, with a try {} catch block poll can be interrupted via Thread.interrupt() so not to wait for the timeout.
                    continue;
                }
                cropToCenter(inputFrame);
                String result = doImgProcessing(inputFrame);
            }
        }
    };
    worker.start();
    
    public Mat onCameraFrame(Mat inputFrame) {
        inputFrame.copyTo(mRgba);//this will be used for the live stream
        frames.put(inputFrame);
        return mRgba;
    }
    

    The onCameraFrame puts the frame on the Queue, the worker Thread polls from the Queue.

    This decorelate the reception and the treatment of the frame. You can monitor the growth of the Queue using frames.size().

    This is a typical producer-consumer example.

    0 讨论(0)
  • 2020-12-31 23:05

    If you're doing this on each frame, it sounds like you need a thread instead. An AsyncTask is for when you want to do a one-off activity on another thread. Here you want to do it repeatedly. Just create a thread, and when it finishes a frame have it post a message to a handler to run the post step on the UI thread. It can wait on a semaphore at the top of its loop for the next frame to be ready.

    0 讨论(0)
提交回复
热议问题