In my app i need to set the camera preview size at 640x480. I tried my code on different devices and different android versions (for example 2.3.6 on samsung galaxy S2, 4.0.3 on asus tf101g, 4.3 on galaxy nexus), and at the last on the new Nexus 5 with android kitkat(4.4).
My code works perfectly in all devices, but doesn't work on Nexus 5. Why? In fact after this steps:
int PREVIEW_WIDTH = 640;
int PREVIEW_HEIGHT = 480;
if(isSupportedSize(PREVIEW_WIDTH,PREVIEW_HEIGHT,mCamera)){
//isSupportedSize controls that width and height are supported by mCamera
mCamera.getParameters().setPreviewSize(PREVIEW_WIDTH,PREVIEW_HEIGHT);
Log.d("debug myapp", "Preview sizes: " + mCamera.getParameters().getPreviewSize().width
+ ", "
+ mCamera.getParameters().getPreviewSize().height);
}
or this steps:
int PREVIEW_WIDTH = 640;
int PREVIEW_HEIGHT = 480;
Camera.Parameters param= mCamera.getParameters();
if(isSupportedSize(PREVIEW_WIDTH,PREVIEW_HEIGHT,param)){
//isSupportedSize controls that width and height are supported by mCamera
param.setPreviewSize(PREVIEW_WIDTH,PREVIEW_HEIGHT);
mCamera.setParameters(param);
Log.d("debug myapp", "Preview sizes: " + mCamera.getParameters().getPreviewSize().width
+ ", "
+ mCamera.getParameters().getPreviewSize().height);
}
Only on Nexus5 the Log is 1920, 1080. Why? Has Somebody the same problem?
isSupportedSize():
private boolean isSupportedSize(int width, int height, Camera cam){
Camera.Parameters param = cam.getParameters();
List<Size> suppSizes = param.getSupportedPreviewSizes();
for (Size s: suppSizes){
if (s.width == width && s.height == height){
Log.d("debug", "sizes supported!");
return true;
}
}
return false;
}
It works perfectly.