简体   繁体   中英

Does anyone know how to display a BitmapField?

I have a code that encode a image to bitmap. But I am not sure on how to display it on the screen. By the way, I am using Blackberry JavaME. This is the code where i use to encode the image. Is this the way I can use in order to get image from a sd card and display it on the screen?

FileConnection conn = 
    (FileConnection)Connector.open("image1.png",Connector.READ_WRITE);
if(conn.exists()) {
    InputStream is = conn.openInputStream();
    BitmapField bitmap = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int ch;
    while ((ch = is.read()) != -1)
    {
        baos.write(ch);
    }
    byte imageData[] = baos.toByteArray();
    bitmap = new BitmapField(
        EncodedImage.createEncodedImage(imageData, 0, imageData.length).getBitmap());
    add(bitmap);
}

Secko is correct that you can override paint but I think you are getting stuck because you have not created a ui application.

Here is a very simple ui application example for displaying a bitmap field, if you copy it exactly you will need an images folder under src with image.png inside of it.

This was modified from the HelloWorldDemo that comes with the SDK. I recommend that if you are just starting out you look at the samples in the folder. \\plugins\\net.rim.ejde.componentpack5.0.0_5.0.0.25\\components\\samples\\

good luck

Ray

public class DisplayBitmaps extends UiApplication
{
    public static void main(String[] args)
    {
        DisplayBitmaps theApp = new DisplayBitmaps();       
        theApp.enterEventDispatcher();
    }

    public DisplayBitmaps()
    {        
        pushScreen(new DisplayBitmapsScreen());
    }    
}

final class DisplayBitmapsScreen extends MainScreen
{
    DisplayBitmapsScreen()
    {    
     Bitmap bitmap = EncodedImage.getEncodedImageResource("images/image.png").getBitmap();
     BitmapField bitmapField = new BitmapField(bitmap);
        add(bitmapField);
    }

    public void close()
    {  
        super.close();
    }   
}

Edit for when the image is on the sdcard

DisplayBitmapsScreen()
{    
 //Bitmap bitmap = EncodedImage.getEncodedImageResource("images/image.png").getBitmap();
 try {
  FileConnection fc = (FileConnection) Connector.open("file:///SDCard/BlackBerry/pictures/image.png");
  if (fc.exists()) {
   byte[] image = new byte[(int) fc.fileSize()];
   InputStream inStream = fc.openInputStream();
   inStream.read(image);
   inStream.close();
   EncodedImage encodedImage = EncodedImage.createEncodedImage(image, 0, -1);
   BitmapField bitmapField = new BitmapField(encodedImage.getBitmap());
   fc.close();
   add(bitmapField);
  }
 } catch (Exception e) { System.out.println("EXCEPTION " + e); }
}

Overriding paint in Field or any extension Field class could also display an image, but I didn't really understand from Secko's example where he would display the image so I have included drawImage in this example below.

protected void paint(Graphics graphics) {
   graphics.drawImage(x, y, width, height, image, frameIndex, left, top);
   super.paint(graphics);
}

You can do it with:

paint(Graphics g);

Perhaps in a function:

protected void DrawStuff(Graphics g) {              
    this.paint(g);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM