public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (previewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
////////////////////////////////////////////////////////////
// Check orientation and set size as w, h or h, w.
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
p.setPreviewSize(h, w);
} else {
p.setPreviewSize(w, h);
}
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} catch(IOException e) {
e.printStackTrace();
}
camera.startPreview();
previewRunning = true;
}
The key to this solution is the order of height and width in setPreviewSize, which should be h, w when in Landscape or w,h when in Portrait.