Tuesday, 20 September 2011

Android: Getting all and maximum megapixels supported by the camera (s)

In your camera app you often need to give the user options for picture size, supported by the camera that is. Or even if it's not an options, you might want to set the picture size to the maximum possible.

To do that you first need to get a list of supported image sizes. The method
( documented here) can help us with this task. The returned value is a list of picure sizes which (among other things) contains the width and height of the supported picture size. With some simple math we can get the number of Megapixels supported by the camera.

So less talk more code

public List getAllSupportedMegapixelsByCamera(int cameraID){
  List mpList=new ArrayList();
  Camera myCamera=Camera.open(cameraID) ; // use Camera.open() for the default back facing camera or if you're supporting an older sdk version.
  // make sure the camera is not in use or something.
    Camera.Parameters allParams=myCamera.getParameters();
    for(Camera.Size pictureSize: allParams.getSupportedPictureSizes ()){
      int sizeInMegapixel=Math.round((pictureSize.width*pictureSize.height)/1000000);

  return mpList;
The returned list will contain values like 2 5 7 ... meaning that the camera supports pictures of size 2 5 and 7 megapixels.