从图像创建位图

时间:2011-11-22 20:27:39

标签: blackberry java-me blackberry-jde

我有一个Image对象,它是相机拍摄的jpg照片,我需要从中创建一个Bitmap。

除了使用BMPGenerator课外,还有什么方法可以做到吗?我正在开展一个商业项目,由于GPLv3许可,我认为我不能使用它。

到目前为止,这是我的代码。我可以用它做点什么吗?

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

我尝试了这个,但我无法获得正确的文件,而且图片卡在空

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

2 个答案:

答案 0 :(得分:6)

您可以使用videoControl.getSnapshot(null)然后使用Bitmap myBitmap = Bitmap.createBitmapFromBytes(raw, 0, raw.length, 1)从相机获取位图。

videoControl来自player.getControl("VideoControl")player来自Manager.createPlayer()

顺便问一下,你有什么样的形象?如果我们谈论的是EncodedImage,你可以使用getBitmap()

答案 1 :(得分:0)

固定! 好吧,差不多。 使用以下方法,但图像旋转90度。 要用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;
}