3

我需要在我的应用程序中提高 fps 速率。现在我有 6 - 10 FPS,这是非常低的 imo,因为我有 Nexus4。所以我决定从 setPreviewCallback 切换到 setPreviewCallbackWithBuffer 但我没有看到帧速率有任何差异。在日志中,我可以看到缓冲区的地址正在循环变化,但我仍然只有 6 - 10 fps(有时 12 但很少)......你能给我一些建议吗?顺便提一句。我有 Android 4.4,我尝试了 OpenCV,但结果几乎相同......我的代码是:

public class XPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

        private SurfaceHolder mHolder;
        private Camera mCamera;
        long mTimer = 0;

        public XPreview(Context context, Camera camera) {
            super(context);
            mCamera = camera;

            mHolder = getHolder();
            mHolder.addCallback(this);
        }

        public void surfaceCreated(SurfaceHolder holder) {

        }

        public void surfaceDestroyed(SurfaceHolder holder) {

        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }

            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }

            try {
                int formatt     = mCamera.getParameters().getPreviewFormat();
                int bytesPerPx  = ImageFormat.getBitsPerPixel( formatt );
                int width       = mCamera.getParameters().getPreviewSize().width;
                int height      = mCamera.getParameters().getPreviewSize().height;
                int size        = (int)( ( width * height * bytesPerPx ) / 8.0);

                Parameters params = mCamera.getParameters();
                mCamera.setParameters(params);

                Log.d(TAG, "Data: " + formatt + " " + bytesPerPx + " " + width + "x" + height + " " + size );

                mCamera.setPreviewDisplay(mHolder);
                mCamera.setPreviewCallbackWithBuffer(this);
                mCamera.addCallbackBuffer( new byte[size] );
                mCamera.addCallbackBuffer( new byte[size] );
                mCamera.addCallbackBuffer( new byte[size] );
                mCamera.addCallbackBuffer( new byte[size] );
                mCamera.addCallbackBuffer( new byte[size] );
                mCamera.startPreview();

            } catch (Exception e){
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }


        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            long time = System.currentTimeMillis();
            Log.d( TAG, "Time between frames: " + ( time - mTimer ) + "ms, FPS: " + ( 1000.0 / (time - mTimer )) + ", data " + data );
            mTimer = time;
            camera.addCallbackBuffer(data);
        }
    }

活动:

public class RTP extends Activity {

      private Camera mCamera;
        private XPreview mPreview; 

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            mCamera = Camera.open();

            mPreview = new XPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById( R.id.frameLayout1 );
            preview.addView(mPreview); 
        } 


        @Override
        public void onPause(){
            super.onPause();
            if( mCamera != null ){
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }
}

编辑:

Parameters params = mCamera.getParameters();
params.setRecordingHint(true);
mCamera.setParameters(params);

这将帧之间的延迟减少到约 30 毫秒,带缓冲区的版本和不带缓冲区的版本将延迟减少到约 60 毫秒。无论如何,如果有人能给我更多建议,我会很高兴。

更深入的测试表明,现在我使用带缓冲和两个缓冲区的版本在 24 到 31 fps 之间,使用默认版本在 15 到 22 之间。

4

0 回答 0