我需要在我的应用中提高fps率。现在我有6-10 FPS,这是非常低的imo,因为我有Nexus4。所以我决定从setPreviewCallback切换到setPreviewCallbackWithBuffer,但我没有看到帧率有任何差异。在日志中,我可以看到缓冲区的数据正在改变循环,但我仍然只有6 - 10 fps(有时是12 fps但很少)... 你能给我一些建议吗? BTW。我有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);
对于带有缓冲区的版本,帧之间的延迟减少到大约〜30ms,而没有缓冲区的版本减少到大约60的延迟。无论如何,如果有人能给我一些建议,我会很高兴。
更深入的测试表明,现在我使用带有缓冲和两个缓冲区的版本以及使用默认版本的15到22之间的24到31 fps。