Android Asynctask to process live video frames

前端 未结 2 1828
不知归路
不知归路 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条回答
  •  猫巷女王i
    2020-12-31 22:53

    I would do something like that :

    // Example value for a timeout.
    private static final long TIMEOUT = 1000L;
    
    private BlockingQueue frames = new LinkedBlockingQueue();
    
    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.

提交回复
热议问题