Editing an image

Posted on November 4, 2009. Filed under: blackberry, image manipulation, software development | Tags: , , , |

 

When you are working with a screen, it is pretty obvious how to overlay text, images and custom shapes.  All the heavy-lifting is accomplished in the onPaint functions and through the graphics object.  I recently ran into an issue where I need to give some rudimentary image editing capabilities.  In essence, I need to display an image and allow the user to annotate the image with text and then save the image.

The first part is easy to accomplish (I managed to find a few examples on how to pan around a large bitmap image), but the annotating of the image was a bit more difficult to accomplish. 

My first iteration involved me having a custom class that kept track of the annotation and the annotation co-ordinates.  Something along the lines of …

class annotation {

public int x;

public int y;

public String text;

}

However, this was fine for the onPaint function, but I couldn’t figure out how to merge these array of annotations with the actual bitmap.  After some searching through forums I decided to resort to the intelli-sense built-in to the eclipse pluging, and there it was!  the holy grail that I was looking for: graphics.

It turns out that you can pass a bitmap to the graphics class, and then use all the graphics functions to manipulate the underlaying bitmap.   And because it uses the same graphics class as used in the onPaint, there was no learning curve.

So my code now looks like…

protected void annotateImage(String s)
                {
                        Graphics g = new Graphics(_bmImage);
                        int globalX = _crossHairPos.getLeft() +  _imagePos.getLeft() ;
                        int globalY = _crossHairPos.getTop() + _imagePos.getTop();
                        g.drawText(s, globalX, globalY);
                }

Advertisements
Read Full Post | Make a Comment ( None so far )

EncodedImage – How to Resize

Posted on November 2, 2009. Filed under: blackberry, multiple devices, software development | Tags: , , , |

 

I have been working on an application that has a number of navigation buttons.  This buttons must always be displayed and I want them to take up about half the width of the screen.  This is a simple problem when you have a single width to deal with, but I recently needed to port the application from the Bold to the Pearl handset.

I wanted to keep the same images but needed to resize based on the display’s width.  There are trivial solutions easily found on the web (where the image’s size is cut in half) but nothing to set an arbitrary size.  So here is my solution (so that I can remember it at a later time).

I know this is an obvious thing to do, but I am continually searching through old projects or the web to remember the details on how to accomplish this task.

The key thing to remember is that the value for the scaleImage is the reciprical of what you would expect.  So if the scaled image is one half (1/2) of the original then your scale factor is (2/1).

protected Bitmap scaleBitmap(EncodedImage ei, int newXSize, int newYSize)
    {
        int xScale = 0;
        int yScale = 0;
        int xSize = ei.getWidth();
        int ySize = ei.getHeight();

        xScale = Fixed32.div(xSize,newXSize);       
        yScale = Fixed32.div(ySize,newYSize);
        return ei.scaleImage32(xScale, yScale).getBitmap();
    }

You can add some refinements:

  1. keep the aspect ratio: just decide what dimension is the limiting factor for display and then apply that scale to both dimensions.
  2. keep images a consistent size: put the size into your class that defines your presentation style
Read Full Post | Make a Comment ( None so far )

How to use resource files in your blackberry app

Posted on August 30, 2009. Filed under: blackberry, software development | Tags: , , , , |

 

In a previous post I gave the steps on how to add resource files to your application.  In today’s discourse I will describe how to update your application to use those resource files.

They really are quite simple to use, provided that you realize the couple tricks.

Follow these steps to reference the resource file in your class.

Step 1 – include the resource interface

The magic of blackberry means that the resource files are converted into an interface and by implementing that interface on your class you get access to all the strings.

For example, my resource file is called ‘myApp.rrh’ and to include this resource file on my class, I do something like…

public class myApp extends UiApplication extends myAppResource

Trick #1 – The resource files’ interface name is an exact match to the resource file name + the key word ‘Resource’

Step 2 – include a reference to the resource

create a static variable and include it in your class…

private static ResourceBundle _res = ResourceBundle.getBundle(BUNDLE_ID,BUNDLE_NAME);

Trick #2 – BUNDLE_ID and BUNDLE_NAME are auto-generated by blackberry magic

reference your resource through the key name defined in your header file…

LabelField title = new LabelField(_res.getString(appName), LabelField.USE_ALL_WIDTH)

Step 3 – include a reference to a resource array

You can also create a resource array.  In the resource editor, right click your resource and select ‘ConvertToMultipleValues’.  The interface for adding values to this resource array is a bit clunky but hey “a poor craftsman blames his tools”.

You access the values through a string array…

String[] messages = _res.getStringArray(msg);

And that is about it.

Read Full Post | Make a Comment ( 1 so far )

Blackberry simulator and Internet access

Posted on August 10, 2009. Filed under: blackberry, MDS, software development | Tags: , , , |

“communication is 9 parts listening and 1 part speaking”

I ran across another gotcha today when I was trying to get my application to make a web service call.  I could not communicate with the internet.  Either I’m getting better at this or I got lucky, but figuring out the solution only took about an hour this time.  The help files that I ran across were actually helpful this time.

 

The easiest way to test if your blackberry simulator has internet access is to use the ‘Browser’ from inside the simulator.  If you can open a web page then your applications will have network access.

 

The key to this whole thing is MDS (Mobile Data System?) and activating it automatically when you start the blackberry simulator.  Once I followed these steps (as taken from Eclipse Help system) everything worked just fine!

 

Start the BlackBerry MDS Simulator when you start the BlackBerry Smartphone Simulator
  1. On the Run menu, click Debug Configurations or Run Configurations.
  2. Perform one of the following tasks:
    • To work with an existing start configuration, under BlackBerry Simulator, select a start configuration.
    • To work with a new start configuration, right-click BlackBerry Simulator, select New.
  3. Click the Simulator tab.
  4. Click the General tab.
  5. Select the Launch Mobile Data System Connection Service (MDS-CS) with simulator option.
  6. Click OK.

 

The keywords that I used to find this …

  • Test a BlackBerry Application that uses an HTTP connection.
Read Full Post | Make a Comment ( 1 so far )

Blackberry Applications

Posted on August 7, 2009. Filed under: blackberry, development | Tags: , |

“The secret to winning at roulette is to know when you are ahead.”

First off, this post is more touchy-feely than about any concrete details.  It is more a summary of what has worked and what hasn’t worked.  Later on I will go into more code details but I wanted to get some initial thoughts out.

Well, here I am a week or so later and I have made some great strides forward.  I have been working on a crazy little app that takes a phone number, decodes from the number to letters and displays a list of the resulting ‘words’.

For example: 228 –> { abc, abc, tuv } provides {aat, aau, aav, abt, abu, abv, act, acu, acv, bat, bau, bav, bbt, bbu, bbv, cat, cau, cav, cbt, cbu, cbv, cct, ccu, ccv }

It isn’t terribly useful but it provides a framework to explore the API and learn how to put the pieces together.

Some of the things that I have learned:

  1. Managers are important
  2. Creating your own custom controls by overriding the basic ‘field’  controls are essential
  3. If you want to impress people then you must know about paint()ing
  4. That people are quite forgiving and accepting on the blackberry forums

Importance of managers

It is debatable on whether PHBs are important in any role other than to provide a focus of scorn and ridicule from their employees.  Hey wait a minute, I’m not talking about that talk of manager; I’m talking about this type of manager.

I learned that to correctly position, navigate and style an application you have to understand how the managers work.  I started out by using verticalFieldManager and horizontalFieldManagers to get the layout that I wanted.  However, I ran into problems creating the style that I desired.  I often had colours not extending the full width of the screen or I had navigation issues or the controls were all left-aligned.

Now, looking back, I’m sure that I could have used the provided managers and just provided my own delegates during construction.  However I decided to create my own custom manager and that made a huge difference in reaching my goal of learning about the framework and getting the app looking and reacting how I wanted it to. 

I will definitely give details on my manager experience in a future discussion.

Creating custom controls

As you can imagine with an application that takes in a phone number, I had to have quite a few buttons that match up with a phone’s keypad.  I started out using the ButtonField but it quickly became evident that I would have problems with colours and event handling. 

I decided to override the base ‘Field’ class and create my own button.  It was quite simple, once I learned how to override essential base methods.

  • getPreferredWidth – used to set the width of your field
  • getPreferredHeight – used to set the height of your field
  • layout – used to set the ‘extent’ of your control
  • paint – oh, the key stone of functions
  • onFocus – fires when field receives focus
  • onUnFocus – fires when field loses focus
  • keyChar – fires when field receives a key event
  • navigationClick – fires when field receives a wheel or touch event

Paint, Paint, Paint

Oh yes, get to know it, get to love it, get to hate it.  In my brief experience, this is THE function to know about if you want your application to look anything other than a VT100 terminal application.  Obviously, this is where you create your own look and feel and impose your style ideals on your unsuspecting customers.

Oh, when you look into this function, also get to learn about Graphic class and how to create your own contexts

Blackberry forums

While searching for how to accomplish my goals I have read many a post where individuals asked and (much to my surprise) received help.

I must say that people on the forums are very forgiving and accepting and generally helpful.  The main challenge that I have found is typing the correct keywords into Google to find the appropriate thread.

Technorati Tags: ,
Read Full Post | Make a Comment ( None so far )

Liked it here?
Why not try sites on the blogroll...