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
Regular Contributor
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket
Accepted Solution

camera / snapshot...

[ Edited ]

I have scoured the forums, found multiple "solutions" to this problem but not a single "solution" in any threaded discussion works. Since I am new, I have the latest of the SDK running on Eclipse, the steps I am trying to figure out is as follows:

 

1) from a UI screen, initialize the camera object full screen (not in a field)

2) allow the user to take a picture then immediately close the camera window after the picture is taken

3) add a thumbnail preview to the current UI Screen form (FIELD) that is simple and shows the picture

4) save the captured image to either the device memory in a folder that I have created or on the SD card in a folder that I have created

5) get the name of that file when it was saved to the folder, the file name has to be unique similar to GUID and returned at the time that the file was saved

 

I have been working on this for the past few days, the current code that I have which still does not work is as follows:

private void initCamera()
{
  try
   {
     //Create a player for the Blackberry's camera.
     Player player = javax.microedition.media.Manager.createPlayer( "capture://video" );

     //Set the player to the REALIZED state (see Player docs.)
     player.realize();

     //Grab the video control and set it to the current display.
     vc = (VideoControl)player.getControl( "VideoControl" );

     if (vc != null)
     {
          //Create the video field as a GUI primitive (as opposed to a
          //direct video, which can only be used on platforms with
          //LCDUI support.)
          _videofeed = (Field) vc.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
          //Display the video control
          vc.setVisible(true);
      }

      //Set the player to the STARTED state (see Player docs.)
      player.start();
  }
  catch(Exception e)
  {
      log( "ERROR " + e.getClass() + ":  " + e.getMessage() );
  }
}
	
private void setupSnapshot()
{
  encoding = "encoding=jpeg&width=640&height=480&quality=normal";
		
  snapshotButton = new ButtonField("Capture", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY | ButtonField.HCENTER);
  listenerSnapshot = new FieldChangeListener() {
    public void fieldChanged(Field field, int context) {
       	 log(encoding);
        	 snapShot();
    }
  };
  snapshotButton.setChangeListener(listenerSnapshot);
  add(snapshotButton);
}
	
private void cleanSnapshot()
{
  Manager m;
  try
  {
    if(snapshotButton != null)
    {
      m=snapshotButton.getManager();
      if(m != null)
        m.delete(snapshotButton);
    }
  }
  catch(Exception ex)
  {
    log("Error: " + ex.toString());
  }
}

 

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Developer
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: camera / snapshot...

 

Can you tell us more about what is not working? The code you have posted doesn't cover all steps that you are trying to achieve. 

 

I assume that you are capturing video snapshot  (vc.getSnapshot( encoding )) when Capture button is clicked as I see you are calling snapshot() method (not shown in code posted) in fieldchangelistener. 

Regular Contributor
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket

Re: camera / snapshot...

Initially the snapshot code was initializing (or attempting to) the camera:

private void snapShot()
{
  try
  {
    if(vc!=null)
    {
      p = javax.microedition.media.Manager.createPlayer(storeLocation);
      p.realize();
      p.start(); 
      vc = (VideoControl) p.getControl("VideoControl");
      vc.setDisplayFullScreen(true);
      byte[] imageBytes = vc.getSnapshot(encoding);
      Bitmap image = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 5);
		
      // do something with it.. test at screen first
      BitmapField bitmapField = new BitmapField();
      bitmapField.setBitmap(image);
      add(bitmapField);
    }
  }
  catch(Exception ex)
  {
     log("Error: " + ex.toString());
  }
}

 But this also would not open up the camera to take the picture, no error thrown either, so I then started to move the camera initialization outside of the snapshot code so that it was initialized when the app was initialized so that it only happens 1 time, howere I seem to be missing some piece of code, still looking through examples and threads.

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Highlighted
Developer
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: camera / snapshot...

 

 

Do you add  your camera field to screen? I don't see it.

 

_videofeed = (Field) vc.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
add(_videofeed);

 

Have you looked at camera sample from RIM that comes with eclipse plug-in? It does exactly same what you are trying to achieve. It's fairly simple and very well commented. 

 

 

Regular Contributor
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket

Re: camera / snapshot...

I add it elsewhere, I am going to rewrite that capture code from scratch and go over line by line what each section is supposed to be doing, I have looked at the CameraDemo code from importing the project in Eclipse but I did not see where it returns to me the file name of the image captured, probably overlooked that, will look again. Thanks

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Developer
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: camera / snapshot...

 

 

When you capture snapshot, it won't create image file and save it to memory. It returns an array of raw image data and you have to create image from row data and save it to file system. You are already creating image from row data.

 

byte[] imageBytes = vc.getSnapshot(encoding);
      Bitmap image = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 5);

 

Now you have to save this image using FileConnection API to device memory or SDCard. Here you have to assign the image unique name which can be current date & time.

 

 

public static String dateFormat()
{
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	return sdf.format(Calendar.getInstance().getTime());
}

 

 

Here is snapshot from RIM sample, that saves image.

 

 

/**
    * A listener used for the "Save" button
    */
    private class SaveListener implements FieldChangeListener
    {
        /** A byte array representing an image */
        private byte[] _raw;

       /**
        * Constructor.
        * @param raw A byte array representing an image
        */
        SaveListener(byte[] raw)
        {
            _raw = raw;
        }

       /**
        * Saves the image as a file in the BlackBerry filesystem
        */
        public void fieldChanged(Field field, int context)
        {
            try
            {       
                // Create the connection to a file that may or
                // may not exist.
                FileConnection file = (FileConnection)Connector.open( FILE_NAME + _counter + EXTENSION );

                // If the file exists, increment the counter until we find
                // one that hasn't been created yet.
                while( file.exists() )
                {
                    file.close();
                    ++_counter;
                    file = (FileConnection)Connector.open( FILE_NAME + _counter + EXTENSION );
                }

                // We know the file doesn't exist yet, so create it
                file.create();

                // Write the image to the file
                OutputStream out = file.openOutputStream();
                out.write(_raw);

                // Close the connections
                out.close();
                file.close();
            }
            catch(Exception e)
            {
                CameraDemo.errorDialog("ERROR " + e.getClass() + ":  " + e.getMessage());
            }

            // Inform the user where the file has been saved
            Dialog.inform( "Saved to " + FILE_NAME + _counter + EXTENSION );

            // Increment the image counter
            ++_counter;

            // Return to the main camera screen
            UiApplication.getUiApplication().popScreen( _imageScreen );
        }
    }

 

 

in your case you can replace

FILE_NAME + _counter + EXTENSION

with 

 

String fileName = dateFormat + EXTENSION;

 which will give image unique name.

 

I hope it will solve your problem otherwise you can always come back hereSmiley Happy

 

Regular Contributor
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket

Re: camera / snapshot...

Thanks, this got me past my hump and solved my problem Smiley Happy

Best regards

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Developer
Posts: 191
Registered: ‎04-02-2013
My Device: BlackBerry Bold 9900
My Carrier: developer

Re: camera / snapshot...

[ Edited ]

hi Zumwaltwood

I am also developing a  camera application and it has all the features that you have mentioned except for the one and that is view images in thumbnail format.

So I want the solution to view the pictures in thumbnail format and i am not getting the correct solution for this .Can you help me.

 

Thanks in advance