Juanma Baiutti
Juanma Baiutti

Reputation: 637

Create a Bitmap from an Image

I have an Image object which is a jpg picture taken by the camera and I need to create a Bitmap from it.

Is there any way to do it besides using BMPGenerator class? I'm working on a commercial project and I don't think I can use it due to the GPLv3 license.

So far this is the code I have. Can I do something with it?

    FileConnection file = (FileConnection) Connector.open("file://" + imagePath, Connector.READ_WRITE);
    InputStream is = file.openInputStream();
    Image capturedImage = Image.createImage(is);

I tried this but I wasn't able to get the correct filepaht and the image is stuck in null

    EncodedImage image = EncodedImage.getEncodedImageResource(filePath);
    byte[] array = image.getData();
    capturedBitmap = image.getBitmap();

Upvotes: 1

Views: 501

Answers (2)

Juanma Baiutti
Juanma Baiutti

Reputation: 637

Fixed! Well, almost. Used the following method but the image is rotated 90 degrees. Going to fix that with this

public Bitmap loadIconFromSDcard(String imgname){

    FileConnection fcon = null;
    Bitmap icon = null;

    try {

        fcon = (FileConnection)Connector.open(imgname, Connector.READ);
        if(fcon.exists()) {
            byte[] content = new byte[(int) fcon.fileSize()];
              int readOffset = 0;
              int readBytes = 0;
              int bytesToRead = content.length - readOffset;
              InputStream is = fcon.openInputStream();
              while (bytesToRead > 0) {
                readBytes = is.read(content, readOffset, bytesToRead);
                if (readBytes < 0) {
                  break;
                }
                readOffset += readBytes;
                bytesToRead -= readBytes;
              }
              is.close();
            EncodedImage image = EncodedImage.createEncodedImage(content,0,content.length);
            icon = image.getBitmap();

        }

    } catch (Exception e) {

    }finally{
        // Close the connections
        try{ if(fcon != null) fcon.close(); }
        catch(Exception e){}
    }

    return icon;
} 

Upvotes: 0

Andrey Regentov
Andrey Regentov

Reputation: 3737

You can use videoControl.getSnapshot(null) and then Bitmap myBitmap = Bitmap.createBitmapFromBytes(raw, 0, raw.length, 1) to get a bitmap from camera.

videoControl is got from player.getControl("VideoControl") and player is got from Manager.createPlayer()

By the way, what kind of Image do you have? If we are talking of EncodedImage, you can just use getBitmap() from it.

Upvotes: 6

Related Questions