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>

Learning Odoo 13 Module Development and Review

USING ODOO 13 Introduction This writing supposed to be a cheat sheet for helping myself to learn about Odoo, Odoo Development and also anyone who want to read this.   Odoo already have a good documentation in their websites you can check it here https://www.odoo.com/documentation/13.0/ What you need Odoo source you can get the source from odoo.com or github https://www.odoo.com/page/download or https://github.com/odoo/odoo. Right now I'm using the  https://www.odoo.com/page/download  . I did using the github twice so I'm already familiar with it but now i want to explore something new and maybe we will find something. Python v3.6++ (intermediate level). https://www.python.org/downloads/windows/ You can learn some basic “web development with python” first if you still new at programming. Search with google. Postgresql https://www.postgresql.org/download/ I’m using win Os for development and Visual Studio Code for IDE What should you re

BlackBerry ListField Tutorial Part 2

Blackberry ListField with Clicked Row this time i create based on rtm4bb code. it used manager as TableRowManager to create the field for each row. first i create MenuListField.java