how to get byte array from com.codename1.ui.Image in memory

15 views
Skip to first unread message

shop.servic...@gmail.com

unread,
Dec 11, 2018, 6:43:23 PM12/11/18
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA NetBeans 8.2
Desktop OS Windows
Simulator latest
Device PC

In Java, I can retrieve byte [] from Image with this code. However ImageIo class in codename1 does not support a write method.
    public byte[] getBytesFromImage(java.awt.Image image) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write((RenderedImage) image, "jpeg", stream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stream.toByteArray();
    }

Can you please advise an alternate codename1 method to convert a com.codename1.ui.Image in memory to a byte array?

Shai Almog

unread,
Dec 11, 2018, 11:16:22 PM12/11/18
to CodenameOne Discussions
What's RenderedImage? That's an AWT class.
Try:

ImageIO.save(img, outputStream, ImageIO.FORMAT_JPEG, 0.9f);

shop.servic...@gmail.com

unread,
Dec 12, 2018, 2:40:01 PM12/12/18
to CodenameOne Discussions
Please disregard the  RenderedImage cast as this was copied from my Java Swing project

Getting "error: non-static method save(Image,OutputStream,String,float) cannot be referenced from a static context" 

I have attached a basic class MyApplication to demonstrate the error.

Thanks in advance.
MyApplication.java

Shai Almog

unread,
Dec 12, 2018, 11:25:47 PM12/12/18
to CodenameOne Discussions
Sorry it should have been ImageIO.getInstance().save...

shop.servic...@gmail.com

unread,
Dec 13, 2018, 9:10:36 AM12/13/18
to CodenameOne Discussions
Thanks for all your help!

Happy Holidays!


On Wednesday, December 12, 2018 at 11:25:47 PM UTC-5, Shai Almog wrote:
Reply all
Reply to author
Forward
0 new messages