0

I have two applications (one in Python and another in Java). They are connected via XML-RPC protocol. I have image files saved in database as binary data and i need to get them in Java.

So i make a query to get the data in python and i return a dictionary as you can see below:

            aux = {
                    'location_id' : location['id'],
                    'name' : location['name'] or " ",
                    'units' : location['units'] or 0.0,
                    'area' : location['area'] or 0.0,
                    'day_meter' : location['day_meter'] or 0.0,
                    'free' : location['free'] or 0.0,    
                    'type' : location['type'] or " ",
                    'loc_type' : location['loc_type'] or " ",
                    'image' : str(location['image']),                                                                    
               }

I receive the data without problems in Java but i can not get the image file from the binary data. Here is my code:

I make a cast to convert the binaty data to a byte [] in Java

        byte[] b = aux.get("image").toString().getBytes("UTF-8");
        l.setByteImg(b);

And here i try to construct a BufferedImage.

        InputStream in = new ByteArrayInputStream(l.getByteImg());
        BufferedImage bImageFromConvert = ImageIO.read(in);

But i get a null in my bImageFromConvert.

What am i doing wrong??

Light1988
  • 77
  • 12

1 Answers1

0

Xml does not support embbeding binary data directly, you need to convert it to a text string. Usually base64 is used to do this.
Try to encode image data in python and convert it back to binary stream in java.

python:

aux = {
                    'location_id' : location['id'],
                    'name' : location['name'] or " ",
                    'units' : location['units'] or 0.0,
                    'area' : location['area'] or 0.0,
                    'day_meter' : location['day_meter'] or 0.0,
                    'free' : location['free'] or 0.0,    
                    'type' : location['type'] or " ",
                    'loc_type' : location['loc_type'] or " ",
                    'image' : base64.b64encode(str(location['image'])),                                                                    
               }

java

byte[] image = javax.xml.bind.DatatypeConverter.parseBase64Binary(aux.get("image").toString());
hago
  • 1,700
  • 2
  • 16
  • 18
  • Thanks for your answer but still doesn't work. I get a null in my BufferedImage. – Light1988 May 06 '14 at 10:25
  • @Light1988 could you check the content of object aux in java and data sent from python to see if they are identical? – hago May 07 '14 at 02:31
  • I've tried that but the String in debug mode is too long so you can not see the entire content. – Light1988 May 09 '14 at 11:05
  • Maybe i am losing information in the process or when i cast to String in Java but how can i know? – Light1988 May 09 '14 at 11:27
  • can you post the raw data you got in java? and the type and content of aux.get("image") in java? – hago May 11 '14 at 06:20
  • This is the data i get in Java in aux.get("image") variable: – Light1988 May 12 '14 at 09:18
  • aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQUU4QUFBQXdDQVlBQUFCUVFDZVNBQUFBQm1KTFIwUUEvZ0QrQVA3ckdOU0NBQUFBQ1hCSVdYTUFBQXhNQUFBTVRBRUFpVStxQUFBQUNYWndRV2NBQUFCUEFBQUFNQUEwbWpOREFBQUR4RWxFUVZSbzN1M2JXNGpVVlJ6QThjOS9adGRkTCt1cVcxaytsTDJFQkZrSVJqMUVsNGRDSWJvL0ZRbUNQV1FSQkJHRTlxaGdKQ3RpUWZUU1F4QVZXMjlCa0FRU1BSaUVGV0dRV1ZKcDVXNnVxNjY3c3pQVHcvblBPcnVzTzVjejYxVHovOEpoZHM3OEx6UGYvKzljOWx3U25VdUNSY2cxZUY0QlU1VUxkQ0pkZUJxUG9i – Light1988 May 12 '14 at 09:19
  • dUI4eEtjd2w1ODNlNGYwUzdXcHhMS1RhYjMwTjBsZlpmU2c1VVl4MW1VV3hxYXk4N1FWY2diSFZqbHprOTc3TjlDM3gvaGJndERBU1BwSzdkTzUrZlQxQ3hyMEpOVWlic0ZyMkFEL3NiYmVBZUZhSUVIbnVYRjE4a1hWcHRZL0xKaTF3T1duT3UxK1YyZTM4SEswek9lWUl2bGZZRmRPRllsc0J2YjhZUlFoR3VSdzAxWW5yNC9oTTBWZVFQNEFQZFduWEFPV3pCRVpPVzQrZ1Q5STNrLzNUeG9xdnU1NmZ5a3hMWmRiSC9WUXRsTEdSTHF1UE5WMFpmRFlyVWpzSndlOXlIdXFwWlhhV25XNDQ1Wkp5M0RRN0hld0lVK1R0NXduVkorMDh5dmxlUHpCem16YXFHYnJudUV5S21taFBOQzlUUmZHa3ZUMU95TFZrSjJ1YmxiblJYQ2s1a1N3NFZsMEt1 – Light1988 May 12 '14 at 09:19
  • VUxKN3pzOG5lQlRXSFhpRVlXa285NVQyZTRueTMrZS8ybHE2TXZQa281WXBLK1J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qb3dNR2NPNXZNQUFBQWxkRVZZZEdSaGRHVTZiVzlrYVdaNUFESXdNVEV0TURRdE1qSlVNVFE2TVRRNk5EUXJNREk2TUREcEp5M05BQUFBSFhSRldIUlRiMlowZDJGeVpRQkhVRXdnUjJodmMzUnpZM0pwY0hRZ09DNDNNUU0vYURRQUFBQUFTVVZPUks1Q1lJST0= – Light1988 May 12 '14 at 09:21
  • I send the data from python in a dictionary so when i get the data in Java i cast it to a HashMap So it is supossed thar aux.get("image") is an Object until i do this: – Light1988 May 12 '14 at 09:26
  • `byte[] b = DatatypeConverter.parseBase64Binary(aux.get("image").toString());` – Light1988 May 12 '14 at 09:27
  • is the data returned by "aux.get("image").toString()" the same with that you sent in python? If yes, the byte array b is the image you need, or you could check the toString() method there. – hago May 13 '14 at 10:14
  • Finally i get it!! I think that the problem was that i was encoding the data in python twice with the line:`base64.b64encode(str(location['image'])))` – Light1988 May 14 '14 at 14:26
  • `'image' : str(location['image']), ` – Light1988 May 14 '14 at 14:27
  • And in Java i put the sentence that you told me: `byte[] b = DatatypeConverter.parseBase64Binary(aux.get("image").toString());` and it works now. So thank you so much! – Light1988 May 14 '14 at 14:29