Skip to main content

BlackBerry Graphics, Image and Camera Tutorial

This tutorial will consist of Resources, Code and Example from me and others that i found around the web.

why i do this ? simple. i want to create an application that took foto and save it on database.

to extend the app i will try to provide something like social plugin (facebook, twitter, pinterest etc) and also image or graphics manipulation (cropping, adjust brightness, or anything that sound simple).

for the start of the writing it will be rough draft and after few trial and error i will try to make it readable for you or even make a simple tutorial that easy to understands.

hopefully from this post i can learn something new again. :)

im using eclipse plugin for blackberry and using 5.0 plugin, its very old.
the RIM guys now just support webworks stuff which wont work at blackberry os 5.0. i wish i have the money to buy new device with blackberry os 7.1 or better, but with $300 a month as programmer its still hard to just live your life.

ive notice that blackberry have eclipse plugin for 7.1 its here https://developer.blackberry.com/java/download/eclipse

for older os n plugin its here http://docs.blackberry.com/en/developers/subcategories/?userType=21&category=Java+Development+Guides+and+API+Reference

anyway enough that lets just get started

i found some of resources and sample code on http://supportforums.blackberry.com

here is resources for blackberry os 4.6
http://supportforums.blackberry.com/t5/Java-Development/Take-a-snapshot-using-the-built-in-camera-of-a-BlackBerry/ta-p/445100

blackberry os 5.0
http://docs.blackberry.com/en/developers/deliverables/11942/Create_BB_app_disp_image_from_camera_viewFinder_739696_11.jsp

i have blackberry 8900 with os 5.0 and Simulator Blackberry 9550 i think 9550 is using 5.0

now its my turn, this will be called

Checking Image Encoding with Blackberry OS 5.0

the code that i create is based on blackberry os 4.6 code on supporforums.blackberry.com you can view it here.

before using blackberry camera to capture picture you have to know what encoding that support your device.

lets create our app :

CheckEncoding.java


 package checkencoding;  
 import net.rim.device.api.ui.UiApplication;  
 /**  
  * This class extends the UiApplication class, providing a  
  * graphical user interface.  
  */  
 public class CheckEncoding extends UiApplication  
 {  
   /**  
    * Entry point for application  
    * @param args Command line arguments (not used)  
    */   
   public static void main(String[] args)  
   {  
     // Create a new instance of the application and make the currently  
     // running thread the application's event dispatch thread.  
     CheckEncoding theApp = new CheckEncoding();      
     theApp.enterEventDispatcher();  
   }  
   /**  
    * Creates a new CheckEncoding object  
    */  
   public CheckEncoding()  
   {      
     // Push a screen onto the UI stack for rendering.  
     pushScreen(new CheckEncodingScreen());  
   }    
 }  

CheckEncodingScreen.java

 package checkencoding;  
 import net.rim.device.api.ui.UiApplication;  
 import net.rim.device.api.ui.component.EditField;  
 import net.rim.device.api.ui.container.MainScreen;  
 import net.rim.device.api.util.StringUtilities;  
 /**  
  * A class extending the MainScreen class, which provides default standard  
  * behavior for BlackBerry GUI applications.  
  */  
 public final class CheckEncodingScreen extends MainScreen  
 {  
   /**  
    * Creates a new CheckEncodingScreen object  
    */  
      private EditField logField;  
   public CheckEncodingScreen()  
   {      
     // Set the displayed title of the screen      
     setTitle("Encoding Type");  
     logField = new EditField("Log:","");  
     add(logField);  
     String encodingString = System.getProperty("video.snapshot.encodings");  
           String[] properties = StringUtilities.stringToKeywords(encodingString);  
           for(int x = 0; x < properties.length; ++x){  
                log(properties[x]);  
           }  
   }  
   private void log(final String msg) {  
           UiApplication.getUiApplication().invokeLater(new Runnable() {  
                public void run() {  
                     logField.setText(logField.getText() + "\n" + msg);  
                }  
           });  
      }  
 }  

from the simulator these what i got


from the looks of it the simulator can get from 480 to 1024 and with quality fine / superfine / normal.

but when i compile it and run from my device blackberry 8900 it only support 360 to 480.

now for the real deal, well, its a very simple deal

Creating Blackberry Application That Using Camera on Blackberry OS 5.0

After youve got what encoding type you have, then lets start to create the apps. this app is still based on blackberry 4.6 code that i found at supportforums.blackberry.com.

i call my app Imaza. its short for Image Riza.... yeah i know its stupid.

Imaza.java



 package imaza;  
 import net.rim.device.api.ui.UiApplication;  
 /**  
  * This class extends the UiApplication class, providing a  
  * graphical user interface.  
  */  
 public class Imaza extends UiApplication  
 {  
   /**  
    * Entry point for application  
    * @param args Command line arguments (not used)  
    */   
   public static void main(String[] args)  
   {  
     // Create a new instance of the application and make the currently  
     // running thread the application's event dispatch thread.  
     Imaza theApp = new Imaza();      
     theApp.enterEventDispatcher();  
   }  
   /**  
    * Creates a new Imaza object  
    */  
   public Imaza()  
   {      
     // Push a screen onto the UI stack for rendering.  
     pushScreen(new ImazaScreen());  
   }    
 }  

ImazaScreen.java


 package imaza;  
 import javax.microedition.media.Manager;  
 import javax.microedition.media.MediaException;  
 import javax.microedition.media.Player;  
 import javax.microedition.media.control.VideoControl;  
 import net.rim.device.api.system.Bitmap;  
 import net.rim.device.api.ui.Field;  
 import net.rim.device.api.ui.FieldChangeListener;  
 import net.rim.device.api.ui.UiApplication;  
 import net.rim.device.api.ui.component.BitmapField;  
 import net.rim.device.api.ui.component.ButtonField;  
 import net.rim.device.api.ui.component.Dialog;  
 import net.rim.device.api.ui.component.EditField;  
 import net.rim.device.api.ui.component.ObjectChoiceField;  
 import net.rim.device.api.ui.container.MainScreen;  
 /**  
  * A class extending the MainScreen class, which provides default standard  
  * behavior for BlackBerry GUI applications.  
  */  
 public final class ImazaScreen extends MainScreen  
 {  
   ObjectChoiceField qualityField;  
      private EditField logField;  
      private Player p;  
      private VideoControl vc;  
      private Field viewFinder;  
      byte[] imageBytes;  
   public ImazaScreen()  
   {      
        init_2();  
   }  
   public void init_2(){  
        logField = new EditField("Log:","");  
           new BitmapField();  
           String[] choices = {"SuperFine 1600x1200", "Fine 1600x1200", "Normal 1600x1200", "SuperFine 1024x768", "Fine 1024x768", "Normal 1024x768", "SuperFine 640x480", "Fine 640x480", "Normal 640x480"};    
           qualityField = new ObjectChoiceField("Quality", choices);  
         try{                                
                p = Manager.createPlayer("capture://video");                                
                p.realize();  
                //log("Player realized");  
                p.prefetch();  
                //log("Player prefetched");  
                p.start();  
                //log("Player started");                      
                vc = (VideoControl) p.getControl("VideoControl");  
                viewFinder = (Field)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");  
                //log("Initialized.");  
                add(viewFinder);  
                vc.setDisplayFullScreen(false);  
                viewFinder.setFocus();  
                vc.setVisible(true);  
                /*String encodingString = System.getProperty("video.snapshot.encodings");  
                String[] properties = StringUtilities.stringToKeywords(encodingString);  
                for(int x = 0; x < properties.length; ++x){  
                     log(properties[x]);  
                }*/  
           } catch (Exception me){  
                log(me.getMessage());  
           }  
           ButtonField myButton = new ButtonField("SNAP",ButtonField.CONSUME_CLICK | FIELD_HCENTER);  
     FieldChangeListener listener = new FieldChangeListener() {  
          public void fieldChanged(Field field, int context) {  
                     // TODO Auto-generated method stub  
               //Thread.sleep(5000);  
                     try {  
                          imageBytes = vc.getSnapshot("encoding=jpeg&width=640&height=480&quality=superfine");  
                          //log("Encoding: ");  
                       log("Size: " + Integer.toString(imageBytes.length));  
                     } catch (MediaException e) {  
                          // TODO Auto-generated catch block  
                          //e.printStackTrace();  
                          log("Media Error " + e.getMessage());  
                     }  
                     UiApplication.getUiApplication().invokeLater(new Runnable(){  
                          public void run(){  
                               if (imageBytes.length>0 && Dialog.ask(Dialog.D_YES_NO, "Draw image?")==Dialog.YES){                                          
                                    Bitmap image = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 4);  
                                    ImazaPreview myPrev = new ImazaPreview();  
                                    myPrev.bitmapField.setBitmap(image);  
                                    UiApplication.getUiApplication().pushScreen(myPrev);  
                               }  
                          }  
                     });            
                }  
     };  
     myButton.setChangeListener(listener);  
     add(myButton);  
           add(logField);  
   }  
   private void log(final String msg) {  
           UiApplication.getUiApplication().invokeLater(new Runnable() {  
                public void run() {  
                     logField.setText(logField.getText() + "\n" + msg);  
                }  
           });  
      }  
 }  

this imaza screen should show you viewFinder and Snap button to take snapshot photo. the preview of the photo that being taken is in this code

ImazaPreview.java


 package imaza;  
 import net.rim.device.api.system.Bitmap;  
 import net.rim.device.api.ui.component.BitmapField;  
 import net.rim.device.api.ui.container.MainScreen;  
 public class ImazaPreview extends MainScreen {  
      /**  
       *   
       */  
      public BitmapField bitmapField;  
      public ImazaPreview() {  
           super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);  
           // TODO Auto-generated constructor stub  
           bitmapField = new BitmapField();  
           //if(bitmapField != null)  
                add(bitmapField);  
      }  
      public void drawImage(Bitmap bitmap){  
           if(bitmapField != null)  
                bitmapField.setBitmap(bitmap);  
      }  
 }  

this is the screen shot of ImazaScreen and ImazaPreview. the end result that is preview is seem to be ugly in someway but in my device not the simulator it works fine. just dont forget to see encoding type.





thats conclude the Camera Tutorial. i havent move to Files yet. on how to save the image. i think it need file browser. anyway keep learning :)

Comments

Popular posts from this blog

JavaScript Real Time Calculation

I've been around looking for javascript that can do Real Time Calculation. javascript real time calculation, javascript real time calculation textbox. by some lucky keywords i found this code. this is exactly the code that i want. it really do the real time calculation. and it doesn't need onChange or OnBlur function. Just try it Example + = this is the javascript code <script type='text/javascript' > function startCalc(){   interval = setInterval("calc()",1); } function calc(){   one = document.autoSumForm.firstBox.value;   two = document.autoSumForm.secondBox.value;   document.autoSumForm.thirdBox.value = (one * 1) + (two * 1); } function stopCalc(){   clearInterval(interval); } </script> this is the html code <form name="autoSumForm">   <input class="right" type=text name="firstBox" value="" onFocus="startCalc();" onBlur="stopCalc();"><br...

iOS 5 Creating eCommerce with Shopping Cart App Part 1

[Created 11-28-2012] it seems like there are tons of tutorial ios and xcode for beginner out there. you can view the list at my post here http://javalearning-adventure.blogspot.com/2012/11/learning-xcode.html that's the link that i always update if i find anything. this tutorial will consist of step by step in creating Shopping Cart App. There will be Browse Product, Shopping Cart, and Checkout Method. I will update these later after i satisfied with the end result. These tutorial will also include web programming using php. but these php will only using basic sample not the very complex with mysql database and everything. these php pages will only creating json output. i think you don't need to learn php and json. you should only learn how to handle json using objective-c. I am still a beginner in xcode and objective-c, its still only 1 month since i started these tutorial. requirements suppose to be iOS 5 with xcode 4. [i will update these later][postponed...

BlackBerry ListField Tutorial Part 3

This Part 3 will consist of Networking, ListField, JSON and Thread. I'm really sorry for pending this part 3. I've been busy making money and still failed. Its just really hard to live in third world country by coding. anyway, There are few Library that you will need creating ListField with JSON data, but we wont touch that area first. we need to learn the basic. * note added : Aug 26 2013 The basic should be : Networking Thread ListField [if i find anything, will be add here]