I am making a Camera application. The preview picture was 90 degree rotated. So I changed my code and added the code to set the correct display orientation of the camera. Now when I run the app, it crashes and the error message is
java.lang.RuntimeException: setParameters failed
My surfaceChanged(SurfaceHolder holder,int format,int w,int h) and surfaceCreated(SurfaceHolder holder) methods are as shown.
public void surfaceChanged(SurfaceHolder holder,int format,int w,int h){
Camera.Parameters mparameters;
mholder = holder;
mparameters = mcamera.getParameters();
mparameters.setPreviewSize(mpreviewsizewidth,mpreviewsizeheight);
mparameters.setPictureSize(mpreviewsizewidth,mpreviewsizeheight);
String mCameraFlashMode = mparameters.getFlashMode();
if(mCameraFlashMode != null){
mparameters.setFlashMode(mparameters.FLASH_MODE_ON);
}
if(mparameters.getFocusMode() != null) {
mparameters.setFocusMode(mparameters.FOCUS_MODE_AUTO);
}
Display display = ((WindowManager)mcontext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
mparameters.setPreviewSize(mpreviewsizeheight, mpreviewsizewidth);
mcamera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
mparameters.setPreviewSize(mpreviewsizewidth, mpreviewsizeheight);
}
if(display.getRotation() == Surface.ROTATION_180)
{
mparameters.setPreviewSize(mpreviewsizeheight, mpreviewsizewidth);
}
if(display.getRotation() == Surface.ROTATION_270)
{
mparameters.setPreviewSize(mpreviewsizewidth, mpreviewsizeheight);
mcamera.setDisplayOrientation(180);
}
mcamera.setParameters(mparameters);
try {
mcamera.setPreviewDisplay(mholder);
}catch(IOException e){
e.printStackTrace();
}
mcamera.startPreview();
}
public void surfaceCreated(SurfaceHolder holder){
mcamera = Camera.open();
try{
mcamera.setPreviewDisplay(holder);
mcamera.setPreviewCallback(this);
}catch(IOException e){
mcamera.release();
mcamera = null;
}
}
Please help me how I can correct my code so that app does not crash while setting the camera parameters, and the preview can be displayed with correct orientation.