Convery byte array to Image and back issue

19 views
Skip to first unread message

shop.servic...@gmail.com

unread,
Jul 5, 2019, 1:31:37 PM7/5/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 strugling to convert Image to bytes and back in my Mobile app.

General question is the approach to convert bytes to com.codename1.ui.Image and back

Currently I use:
custPhotoBytes.length = 26489
Image CustomerPicture = Image.createImage(custPhotoBytes, 0, custPhotoBytes.length);
inc.getBytesFromImage(CustomerPicture).length = 26471
byte[] imgBytes = inc.getBytesFromImage(A3CustomerPhotoImageViewer.getImage());
imgBytes .length = 26471

conversion dif of 18 bytes. Why?

    public byte[] getBytesFromImage(com.codename1.ui.Image image) {
        EncodedImage img = null;
        try {
            img = createFromImage(image, true);
        } catch (Exception e) {
        }
        return img != null ? img.getImageData() : new byte[0];
    }

Is there a better approach?

Thanks in advance.

Regards

Shai Almog

unread,
Jul 6, 2019, 1:07:49 AM7/6/19
to CodenameOne Discussions
You are re-encoding the image. Different image encoders will produce different results, since we use the native facility for image encoding you'll get different results on iOS, Android, simulator etc.
If the image is an EncodedImage just use getData() to get the encoded bytes.

shop.servic...@gmail.com

unread,
Jul 6, 2019, 9:43:03 AM7/6/19
to CodenameOne Discussions
I am using getImageData(), I don't see a getData() method.

Your thoughts on the best approach to read in bytes into an Image that I can display
Then when the user bails from this module we convert back to bytes and compare against original bytes (If different then we savechanged data)

Thoughts?

Regards

Shai Almog

unread,
Jul 6, 2019, 11:22:28 PM7/6/19
to CodenameOne Discussions
That's a method of EncodedImage, check how you create/load the image.

shop.servic...@gmail.com

unread,
Jul 7, 2019, 10:40:24 PM7/7/19
to CodenameOne Discussions
Good point.

I am using ImageViewer to Display pics. ImageViewer only accepts Image objects.

Converting Image to bytes and back are not reliable.

However EncodedImage in place of Image does work.
EncodedImage.createFromImage(viewer.getImage(),false)

Thanks.

Regards
Reply all
Reply to author
Forward
0 new messages