I have a weird problem with my Camera-SurfaceHolder.
I want to show the image of the camera in my activity. It all works greatly on my GalaxyS1 (CyanogenMod - Android 4.4) On my S3 (also CyanogenMod - Android 4.4) on the other hand it looks weird.
The landscape image without title bar is okay but when I show the title bar or turn it into portrait mode it looks distorted:
--- EDIT ---
Thank you Alex Cohn for your help. It looks like you are right. The preview for the portraid mode is now working nicely. But still it looks distorted on the landscape view. I checked the preview scale and it looks okay. As far as I can see I set the preview size properly, too. So what is wrong with it?
Here is the current code:
private static final String TAG = CameraView.class.getSimpleName();
private SurfaceHolder surfaceHolder;
private Camera camera;
private List<Size> mSupportedPreviewSizes;
private Size mPreviewSize;
public CameraView(Activity activity) {
super(activity);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
camera = Camera.open();
mSupportedPreviewSizes = camera.getParameters()
.getSupportedPreviewSizes();
setCameraDisplayOrientation(activity, 0, camera);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
camera = null;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
camera = null;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w,
int h) {...}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, Camera camera) {...}
--- EDIT ---
I logged the values for the onMeasure and surface Changed. They seem to be ok:
Portrait:
SURFACE CHANGED: Witdh:720 Height:1134
onMeasure: width:720 height:1134
Landscape:
SURFACE CHANGED: Witdh:1280 Height:590
onMeasure: width:1280 height:590
Preview Size: Width: 704 Height:576
I get the following possible preview sizes.
There are 8 elements in the array:
960 x 720, 1280 x 720, 1184 x 666, 960 x 640, 704 x 576, 640 x 480, 352 x 288, 320 x 240
Hopefully anyone can help!
Thanks, Tobias
you`getOptimalPreviewSize()` choose for 1280x590? – Alex Cohn Mar 29 '14 at 21:04