1

I am developing an android custom camera application without using the intent (to avoid getting android's built in camera features). I have enabled auto focus feature in my app. I am taking the picture on press of a keyVolume Button. And I am using the below code for setting the parameters.

    Camera.Parameters p = camera.getParameters();
    camera.autoFocus(autoFocusCallback);
    p.setFocusMode(Parameters.FOCUS_MODE_AUTO);
    camera.setParameters(p1);
    camera.takePicture(shutterCallback, rawCallback, jpgCallback);


    void setHandler(Handler autoFocusHandler, int autoFocusMessage) 
    {
           this.autoFocusHandler = autoFocusHandler;
           this.autoFocusMessage = autoFocusMessage;
    }

    private AutoFocusCallback autoFocusCallback = new AutoFocusCallback() 
    {
         private Object success;
         @Override
         public void onAutoFocus(boolean autoFocusSuccess, Camera camera)
         {  
              if (autoFocusHandler != null)
              {
                    Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success);
                    autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS);
                    autoFocusHandler = null;
              }
              else
              {

              }
         }
};

But the problem is that, this code works fine only for LG phone. and i am getting force close on all other phones after running it.

And the Error Log looks like this

http://textuploader.com/?p=6&id=kOc9G

Not getting where i am going wrong. Please Help! Thanks!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
sanjana
  • 641
  • 2
  • 15
  • 36

3 Answers3

2

Different phones have different camera params. Check if mode available befire actually setting it.

For example, in your case there is public List<String> getSupportedFocusModes () function of Camera.Parameters class.

Afaik, cheap phones like acer or zte or some others, have very weak programming support for their cameras.

UPD: code sample

    Camera.Parameters p = camera.getParameters();
    List<String> modes = p.getSupportedFocusModes();
    if(modes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
    {
        p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        camera.setParameters(p);
        camera.autoFocus(autoFocusCallback);

    }
    else
    {
        // this is default focus mode if autofocus unsupported.
        // also, we should not call camera.autoFocus(autoFocusCallback) here
        p.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
        camera.setParameters(p);
    }
Raiv
  • 5,731
  • 1
  • 33
  • 51
  • In addition to @Raiv's suggestion, I would recommend to use `try {} catch` around Camera.setParameters() because there are too many different devices with their crazy incompatible cameras. – Alex Cohn Sep 03 '13 at 20:46
  • Could u please post how to implement getSupportedFocusModes() method in the code? @Raiv – sanjana Sep 04 '13 at 04:21
  • I used the try catch block around Camera.setParameters. But After taking the picture, the camera preview doesn't restart for taking the next pic. I need to open the app again to take the next pic. @Alex Cohn – sanjana Sep 04 '13 at 04:24
1

you are using

 Camera.Parameters p = camera.getParameters();

so replace

 camera.setParameters(p1);

with

 camera.setParameters(p);

I think this should help you....

Camera.Parameters p = camera.getParameters();
List<Size> sizes = p.getSupportedPictureSizes();
// Choose any one you want among sizes
size = sizes.get(0);
p.setPictureSize(size.width, size.height);
camera.setParameters(p);
Piyush
  • 18,895
  • 5
  • 32
  • 63
1

Don't use "p.setFocusMode(Parameters.FOCUS_MODE_AUTO);" line.

By default focus mode will be FOCUS_MODE_AUTO.

Vivek Bansal
  • 2,471
  • 2
  • 19
  • 24