Tuesday, 10 September 2013

Nullpointer exception at startDisplayPreview()

Nullpointer exception at startDisplayPreview()

Objective: To draw a re-sizable rectangle on camera preview.
I could not find any definitive answer for the nullpointer exception
issue. So I am hoping to solve my problem by posting the question. What I
already have:
correct Android Manifest file(camera permissions and such)
correct xml file
one of the answers for a similar question required adding
getBestPreviewSize() method, which I tried
Here is code:
Preview.java
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public Preview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
// mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// without this constructor, xml which uses this class, will throw this
// error: class cannot be instantiated
public Preview(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
// TODO Auto-generated method stub
Camera.Parameters params = mCamera.getParameters();
Camera.Size size = getBestPreviewSize(params, w, h);
if (size != null) {
params.setPreviewSize(size.width, size.height);
}
if (mHolder.getSurface() == null) {
return;
}
mCamera.startPreview();
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("CameraView",
"Error setting camera preview: " + e.getMessage());
}
}
private Size getBestPreviewSize(Parameters params, int w, int h) {
// TODO Auto-generated method stub
Camera.Size result = null;
for (Camera.Size size : params.getSupportedPreviewSizes()) {
if (size.width <= w && size.height <= h) {
if (result == null) {
result = size;
} else {
int resultDelta = w - result.width + h - result.height;
int newDelta = w - size.width + h - size.height;
if (newDelta < resultDelta) {
result = size;
}
}
}
}
return result;
}
public void onPause() {
mCamera.release();
mCamera = null;
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
if (mCamera == null) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("", "Error setting camera preview: " + e.getMessage());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
}
CameraPreview.java with MainActivity()
public class CameraPreview extends Activity {
private Preview mPreview;
private ImageView mTakePicture, cameraIcon;
private DrawView dV;
private boolean mAutoFocus = true;
private boolean mFlashBoolean = false;
private Button bDisplayText;
private RelativeLayout rl;
private FrameLayout fl;
Camera c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
setContentView(R.layout.activity_main);
mTakePicture = (ImageView) findViewById(R.id.cameraButton);
bDisplayText = (Button) findViewById(R.id.bDispText);
fl = (FrameLayout) findViewById(R.id.preview);
rl = (RelativeLayout) findViewById(R.id.rl);
c = getCameraInstance();
dV = new DrawView(this);
dV.invalidate();
rl.addView(dV);
mPreview = new Preview(this, c);
fl.addView(mPreview);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mPreview != null) {
mPreview.onPause();
mPreview = null;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
private static Camera getCameraInstance() {
// TODO Auto-generated method stub
Camera c = null;
c = Camera.open();
return c;
}
}

No comments:

Post a Comment