Search code examples
blackberryjava-meblackberry-jde

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();

Solution

  • 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;
    }