getBytesFromImage conversion always off by one byte (source is one byte greater)

11 views
Skip to first unread message

shop.servic...@gmail.com

unread,
Jun 27, 2019, 5:03:51 PM6/27/19
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 10 Pro
Simulator Latest
Device PC, Android & IOS

I am creating an Image from bytes: 
Image CustomerPicture = Image.createImage(vehiclepicture, 0, vehiclepicture.length);
A3VehiclePhotoImageViewer = new ImageViewer(CustomerPicture)
Source bytes are 303981, getBytesFromImage returns 303980


    public byte[] getBytesFromImage(com.codename1.ui.Image image) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] BytesFromImage = new byte[0];
        try {
            if (image != null) {
                ImageIO.getImageIO().save(image, outputStream, ImageIO.FORMAT_PNG, 1f);
                BytesFromImage = outputStream.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
            MyLog.e(e);
        }
        return BytesFromImage;
    }

Thoughts?

Regards

Shai Almog

unread,
Jun 27, 2019, 11:38:46 PM6/27/19
to CodenameOne Discussions
That method re-encodes a PNG which can have a different byte count than the one in your original image. If you use EncodedImage you can use use getData() to get the original image file.

shop.servic...@gmail.com

unread,
Jun 28, 2019, 8:27:05 AM6/28/19
to CodenameOne Discussions
Thank you very much!

shop.servic...@gmail.com

unread,
Jun 28, 2019, 8:27:20 AM6/28/19
to CodenameOne Discussions
Reply all
Reply to author
Forward
0 new messages