[Bada] Putting a Resource Image in a Form

When a new bada project is created, a Res directory is created underneath the project folder. This folder holds resources, such as XML files defining Form, for the application. Here, an image of a nebula (I originally took this from http://photojournal.jpl.nasa.gov/browse/PIA04216.jpg) is loaded from a file in the Resources directory and displayed on the screen.

using namespace Osp::Base;
using namespace Osp::Graphics;
using namespace Osp::Io;
using namespace Osp::Media;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

const String MainForm::IMAGE_PATH = "/Res/Images/nebula.jpg";

...

result MainForm::PrepareImage(void)
{
    Bitmap *bitmap = NULL;
    Image image;

    result r = image.Construct();
    TryCatch(r == E_SUCCESS, , "Could not decode the resource image");

    bitmap = image.DecodeN(IMAGE_PATH,
            Osp::Graphics::BITMAP_PIXEL_FORMAT_RGB565);

    AppLog("Bit map decoded width [%d] height [%d]", bitmap->GetWidth(), bitmap->GetHeight());

    r = GetLastResult();
    TryCatch(r == E_SUCCESS, , "Could not decode the resource image");
    r = PlaceImage(bitmap);

CATCH:
    if (bitmap != NULL)
    {
        delete bitmap;
    }

    return r;
}

The resources files in the Res directory have paths beginning with “/Res”. In this particular case, the path would be /Res/Images/nebula.jpg. It is also possible for the image to come from other locations, such as /Media/Images or /Storagecard/Media/Images. A list of important locations can be found in the documentation under bada Developer Guide -> Basics of bada Programming -> bada File System.

To draw the image to the screen, obtain a Canvas and use its DrawBitmap method. One way to obtain a Canvas is to get it from a control on the form:

result MainForm::PlaceImage(Bitmap *image)
{
    result r = E_SUCCESS;
    AppLog("Placing image on view");
    Label *panel = static_cast<Label*> (GetControl(L"ImagePanel"));

    TryReturn(panel != NULL, E_UNKNOWN, "Could not locate the image panel");
    Canvas *canvas = panel->GetCanvasN();
    TryCatch(canvas != NULL, r = GetLastResult(), "Could not obtain canvas for drawing image");

    r = canvas->DrawBitmap(panel->GetPosition(), *image);

    TryCatch(r == E_SUCCESS, , "Could not draw image");
    canvas->Show();

CATCH:
    if (canvas != NULL)
    {
        delete canvas;
    }

    return r;
}

Lastly, the image should only be drawn once the Form containing the control is displayed. Otherwise, the image will not be visible on the screen. The form’s

void
ResourceImages::OnForeground(void)
{
	pForm->PrepareImage();
}

The source code for this tutorial can be downloaded from here.

Advertisements

2 Responses to [Bada] Putting a Resource Image in a Form

  1. mike says:
    __pLabel = new Label();
    __pLabel->Construct(Rectangle(0,0, WIDTH, HEIGHT), L"");
    __pLabel->SetBackgroundBitmap(*image);
    
    • kahgoh says:

      Haven’t had the chance to try it out yet, but it is definitely something that I hadn’t thought of (or, at least, aware of)! Thanks for the suggestion!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: