1

I am developing an app which takes a preview of the device's camera and analyses that feed. I can create the camera preview, but cannot get the camera to adjust its focus automatically.

I know that the underlying hardware is possible of performing an auto focus because the native BlackBerry camera app responds to the 'take photo' media key by auto focussing the image prior to taking the photo.

However, I'm not trying to take a photo, I'm trying to continuously scan the input feed for a barcode.

Here's my code:

Player _player = Manager.createPlayer("capture://video");
_player.realize();
_player.start();
_vc = (VideoControl) _player.getControl("VideoControl");

//this is added to the screen
_viewFinder = (Field) _vc.initDisplayMode(
    VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");

FocusControl focusControl = (FocusControl) _player.getControl("javax.microedition.amms.control.camera.FocusControl");

//this has no effect
focusControl.setFocus(FocusControl.AUTO);

I have tested on BlackBerry Storm 9500 and Bold 9700 both running OS5.

donturner
  • 17,867
  • 8
  • 59
  • 81

2 Answers2

2

Try this

this.player = Manager.createPlayer("capture://video");
this.player.realize();
this.videoControl = ((VideoControl)this.player.getControl("VideoControl"));
this.field = ((Field)this.videoControl.initDisplayMode(0, "net.rim.device.api.ui.Field"));
this.videoControl.setVisible(true); 
this.player.start();
try {
        //get focuscontrol
      FocusControl focusControl = (FocusControl)getCurrentObject().player.getControl("javax.microedition.amms.control.camera.FocusControl");
      if (focusControl == null) {
          //no focus control
        Log.Debug("Focus control not available.");
      } else {
        if (focusControl.isMacroSupported()) {
            //setting macro
          Log.Debug("Setting macro mode.");
          focusControl.setMacro(true);
        } else {
            //no macro
          Log.Debug("Macro mode not supported.");
        }
        if (focusControl.isAutoFocusSupported()) {
            //setting autofocus
          Log.Debug("Using autofocus.");
          focusControl.setFocus(-1000);
        } else {
            //no autofocus
          Log.Debug("Autofocus not supported.");
        }
      }

It works for me !!!

rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • 1
    -1000 is the constant value for FocusControl.AUTO (http://www.blackberry.com/developers/docs/5.0.0api/constant-values.html#javax.microedition.amms.control.camera.FocusControl.AUTO) so I think that's what I've already got. – donturner Dec 05 '11 at 23:02
  • it works for me on all phones with autofocus. like 9700 etc. My doubht is about the way you are initialising the code. Try the edited code. – rfsk2010 Dec 06 '11 at 07:36
  • I tried your code on my Bold 9700 running OS5 and it doesn't perform any differently to my original code. The focal length of the camera is fixed and will not refocus as you move the camera around. Does yours automatically refocus? – donturner Dec 09 '11 at 10:25
  • Sorry if I wasnt clear. It will only focus when you click to capture. Unlike the android,iphones which focus automatically while moving. In 9700 try to take a macro picture and see if it focusses just before capture? – rfsk2010 Dec 09 '11 at 10:27
  • Is it possible to 'click to capture' programmatically? I'm trying to scan a barcode, rather than take a photo. Thanks for your help btw :) – donturner Dec 09 '11 at 10:35
  • You could inject event to do a navigationclick() which can be wired up to do a capture . do videoControl.getSnapshot(null); – rfsk2010 Dec 09 '11 at 10:39
  • Many thanks for your help, this is the route I have gone down. I have added an answer which clarifies this, but have also upvoted your answer since it was useful. – donturner Dec 13 '11 at 15:55
1

The only way to focus the camera in OS5 is to use VideoControl.getSnapshot(). There is no other way.

donturner
  • 17,867
  • 8
  • 59
  • 81