Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
mohunt
Posts: 31
Registered: ‎08-26-2011
My Device: Torch 9810

Form ignore keypress event when in camera snapshot loop

[ Edited ]

I am writing a scanning application takes multiple snapshots until it finds a barcode that it can decode.  

 

I have created a do loop that depends on a boolean field to determine whether the user has decided to cancel the scan.

 

I have found that while it is in the scanning loop, the screen is not responding to the keypress event.

 

My code below:

 

import java.io.*;
import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.system.Alert;
import net.rim.device.api.system.Audio;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.AbsoluteFieldManager;
import net.rim.device.api.ui.container.ComponentCanvas;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.amms.control.camera.*;
import javax.microedition.amms.control.camera.CameraControl;
import javax.microedition.amms.control.camera.ZoomControl;
import javax.microedition.io.Connector;
import javax.microedition.io.file.*;
import javax.microedition.media.*; 
import javax.microedition.media.control.*;
import javax.microedition.lcdui.Image;

public class CameraImageCapture extends MainScreen {
Player _camera;
VideoControl _cameracontrol;
String _results = "";
String _textline = "";
String _outputfile = "";
private EnhancedFocusControl _efc;
private ZoomControl _zoomControl;
private Field _videoField;
boolean _notFound = true;
AbsoluteFieldManager _afm;
BitmapField bfmAimer;


public CameraImageCapture()
{
    try
    {
	_afm = new AbsoluteFieldManager();
	_camera = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
	_camera.realize();
	_cameracontrol = (VideoControl) _camera.getControl("VideoControl");
	_zoomControl =  (ZoomControl)  _camera.getControl("javax.microedition.amms.control.camera.ZoomControl");
	_camera.start();
	if (_cameracontrol != null)
	{
		_videoField = (Field) _cameracontrol.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
		if(_videoField != null)
	        {
		 _afm.add(_videoField);
		 _cameracontrol.setDisplayFullScreen(true);
		 _cameracontrol.setVisible(true);
		 if (_zoomControl != null) {
			 _zoomControl.setDigitalZoom(_zoomControl.getMaxDigitalZoom());
		}
		add(_afm);
		
	}
	}
    }
    catch(Exception e)
    {
	Dialog.alert(e.toString());
    }
}

 protected boolean invokeAction(int action)
    {
     byte[] rawImage = null;
      boolean handled = super.invokeAction(action);
	if(!handled)
	{
	    if(action == ACTION_INVOKE)
	    {   
		try
		{
			while (_notFound)
			{
			   if (_camera.getState() != Player.STARTED)
			   {
				_camera.start();
			    }
			    _efc = (EnhancedFocusControl) _camera.getControl("net.rim.device.api.amms.control.camera.EnhancedFocusControl");
			    if (_efc != null)
			    {
				if (_efc.isFocusModeSupported(EnhancedFocusControl.FOCUS_MODE_SINGLESHOT)) {
					_efc.setFocusMode(EnhancedFocusControl.FOCUS_MODE_SINGLESHOT);                    
				}
				try
				{
				_efc.startAutoFocus();
				}
				catch (Exception e1)
				{
					_notFound = false;
					Dialog.alert(e1.toString());
					break;
				}
			    }
			    rawImage = _cameracontrol.getSnapshot(null);
			    _camera.stop();
			    if(rawImage != null)
			    {
				Dialog.alert("Got Picture");
			    }
			 
			}
							
		}
		catch(Exception e)
		{
			_notFound = false;
		    Dialog.alert(e.toString());
		}
	    }
	}
	terminatescan();
	return handled;                
    }
	 
    private boolean terminatescan()
    {
	try
	{
		_notFound = false;
		if (_camera.getState() == Player.STARTED)
		{
			_camera.stop();
		}
		if (_camera.getState() != Player.CLOSED)
		{
			_camera.close();
		}
		if (!_closed)
		{
			_closed = true;
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					 UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
				}
			});
		}
	}
	catch (Exception e)
	{
	
	}
	return true;
  }

   public boolean keyDown(int keycode, int time) {

	// First convert the keycode into an actual key event, taking
	// modifiers into account
	int key = Keypad.key(keycode);

	// From there we can compare against the escape key constant. If
	// we get it, we stop the scan and pop this screen off the stack
	if (key == Keypad.KEY_ESCAPE) {
		
		_notFound = false;
		terminatescan();
		return true;

	}
	// Otherwise, we'll return false so as not to consume the
	// keyDown event
	return false;
    }
}

 

Any help would be appreciated.

Please use plain text.