从图像创建位图

发布于 2024-12-17 18:13:45 字数 737 浏览 1 评论 0原文

我有一个 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();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铜锣湾横着走 2024-12-24 18:13:45

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

videoControl是从player.getControl("VideoControl")获取的,player是从Manager.createPlayer()获取

的图片你有吗?如果我们谈论 EncodedImage,您只需使用其中的 getBitmap() 即可。

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.

半夏半凉 2024-12-24 18:13:45

固定的!
嗯,差不多了。
使用以下方法,但图像旋转了 90 度。
将使用这个

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

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;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文