我成功创建了一个摄像头表面视图,它可以在应用程序启动/重新启动时运行。但它只是显示一个黑屏,我暂停游戏后恢复它(按主屏幕)。我在这里错过了什么吗?
这是表面的代码(从主要活动创建:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
static Camera camera = null;
Surface camSurface;
String TAG = "CameraView";
static SurfaceHolder mHolder;
public CameraView(Context context) {
super(context);
getHolder().addCallback(this);
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// TODO Auto-generated constructor stub
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.e(TAG, "Camera view created");
mHolder = getHolder();
if(camera == null){
camera = Camera.open();
}
if(camera == null){
Log.e(TAG, "Null camera");
}
try {
camera.setPreviewDisplay(getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
camera.release();
camera = null;
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
synchronized(mHolder){
if(camera!=null){
camera.stopPreview();
camera.release();
camera = null;
}
}
}
}
答案 0 :(得分:1)
当您离开并返回应用程序时,您仍需要在某个时刻调用camera.startPreview()。我会在我的surfaceChanged()方法中使用它。
确保您首次启动相机时有一个有效的相机实例。