我无法让相机工作(我的意思是,它确实有效,但与此同时我得到一个NullPointerException)。它被抛出onSurfaceCreated方法:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
Log.i("xcamerx", String.valueOf(mCamera));
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
Log.i("xcamera", String.valueOf(mCamera));
Log.i("xcamera", String.valueOf(holder));
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("CameraError", "Error starting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
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
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d("CameraError", "Error starting camera preview: " + e.getMessage());
}
}
public void pause()
{
mHolder.removeCallback(this);
}
public void resume()
{
mHolder.addCallback(this);
}
}
在我的SurfaceView中实现。在这个视图的活动中,我得到一个Camera实例:
公共类CameraActivity扩展了Activity {
private Camera mCamera;
private CameraView mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraView(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_view);
preview.addView(mPreview);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mPreview.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mPreview.resume();
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}
然后作为参数传递给SurfaceView构造函数。在LogCat中,当我打印Camera对象时(就像我发布的第一个代码块一样),我得到了几次有效的Camera对象,但是现在有一些空值出现的原因超出了我的理解范围。有什么想法吗?