Android camea startPreview() fails when taking multiple pictures

。_饼干妹妹 提交于 2020-01-25 22:31:26

问题


I'm writing an APP for taking multiple pictures by camera. (Like the burst mode in Camera2, but I'm working on older versions.) It is implemented by takePicture() / onPictureTaken() callback, and work normally on several devices except some performance issues. However I got a RuntimeException of startPreview failed on an HTC Sensation device when trying to start preview for the 2nd picture.

Here is the functions about multiple shot:

final int multishot_count = 3;

....

public void TakePicture()
{
    // Invoke multishot from UI when the camera preview is already started.

    // startup of multishot task
    ...

    m_take_picture_count = 0;

    TakeOnePicture();
}

private void TakeOnePicture()
{
    m_camera.takePicture(null, null, new Camera.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, final Camera camera) 
        {
            m_take_picture_count++;

            // feed the JPEG data to a queue and handle it in another thread
            synchronized(m_jpeg_data_lock)
            {
                int data_size = data.length;
                ByteBuffer buffer = ByteBuffer.allocateDirect(data_size);
                buffer.put(data, 0, data_size);

                m_jpeg_data_queue.offer(buffer);

                if (m_take_picture_count == multishot_count)
                    m_is_all_pictures_taken = true;
            }

            if (m_take_picture_count < multishot_count)
            {
                m_camera.startPreview();
                TakeOnePicture();
            }
            else
            {
                // Finalize the multishot task and process the image data
                EndTakePictureTask end_take_picture_task = new EndTakePictureTask();
                end_take_picture_task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        }
    });
}

After the first picture being taken, the startPreview() function may fail in a rate of 10-20% on the specified device. My APP is always run in foreground and has a visible view to show the preview frames. I also try to bypass the JPEG data without processing it, but the error still occurs.

I found a similar report at here. It seems that the underlying thread for taking picture does not finish its works, but we have no way to check it. So I try to catch the exception from startPreview() and sleep a while:

            if (m_take_picture_count < multishot_count)
            {
                boolean is_try_start_preview = true;
                while (is_try_start_preview)
                {
                    try
                    {
                        m_camera.startPreview();
                        is_try_start_preview = false;
                    }
                    catch (RuntimeException e)
                    {
                        try
                        {
                            Thread.sleep(50);
                        }
                        catch (InterruptedException e2)
                        {
                            e2.printStackTrace();
                        }
                    }
                }

                TakeOnePicture();
            }

But once startPreview fails, it will fail forever no matter how many times I sleep.

I also found that you need to call setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) for the surface holder in older Android versions to let startPreview() work correctly. But the device has Android version 4.0.3 and should not require this call. I still try to add it but the issue remains the same. Moreover, the first call of startPreview() works correctly while the second call after onPictureTaken() gets the error. This is different from the issue of missing setType() in older versions.

Finally I use an workaround that may greatly hurt the performance:

            if (m_take_picture_count < multishot_count)
            {
                try
                {
                    m_camera.startPreview();
                }
                catch (RuntimeException e)
                {
                    // Fail to start preview. Workaround: reopen camera.
                    RestartCamera();
                }

                TakeOnePicture();
            }

That is, when startPreview() fails, I just close the camera, reopen it with the same settings, then start the preview to take another picture.

I want to know if I miss something about the take picture APIs, and is there a solution better than this brute-force workaround.


回答1:


Thanks for the suggestion from Alex Cohn, I found a solution. This solution is only tested on the HTC Sensation device, but it has dramatic effect on this device.

I just put a sleep instruction before startPreview() that would be called in onPictureTaken():

try
{
    Thread.sleep(20);
}
catch (Exception e)
{
    e.printStackTrace();
}

Then I run a test 100 times, where each run takes 3 pictures in succession. With the sleep, I didn't get any error from startPreview() in 100 runs. If There is no sleep, I need to reopen the camera 78 times, and restart the device 8 times in 100 runs.



来源:https://stackoverflow.com/questions/33628881/android-camea-startpreview-fails-when-taking-multiple-pictures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!