Showing posts with label Camera. Show all posts
Showing posts with label Camera. Show all posts

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
Camera.Parameters.getSupportedPictureSizes
( 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.
  if(myCamera!=null){
    Camera.Parameters allParams=myCamera.getParameters();
    for(Camera.Size pictureSize: allParams.getSupportedPictureSizes ()){
      int sizeInMegapixel=Math.round((pictureSize.width*pictureSize.height)/1000000);
      mpList.add(sizeInMegapixel);
    }
  }

  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.

Sunday, 18 September 2011

Android: Taking pictures without a preview window

Why on earth one would want to take a picture with your Android phone without knowing what's going to be capture? Well, apps have their reasons, like the SOS app NoDistress.


Taking pictures programmatically is easy: do the same thing you'd do but set a dummy surface view to the camera.

Example

public void takePictureNoPreview(Context context){
  // open back facing camera by default
  Camera myCamera=Camera.open();

  if(myCamera!=null){
    try{
      //set camera parameters if you want to
      //...

      // here, the unused surface view and holder
      SurfaceView dummy=new SurfaceView(context)
      myCamera.setPreviewDisplay(dummy.getHolder());    
      myCamera.startPreview(); 
      
      myCamera.takePicture(null, null, getJpegCallback()):
      
    }finally{
      myCamera.close();
    }      

  }else{
    //booo, failed!
  }


  private PictureCallback getJpegCallback(){
    PictureCallback jpeg=new PictureCallback() {   
      @Override
      public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream fos;
        try {
          fos = new FileOutputStream("test.jpeg");
          fos.write(data);
          fos.close();
        }  catch (IOException e) {
          //do something about it
        }
      }
    };
  }
}


---
Programming tricks and tips for android developers