OVH Cloud OVH Cloud

Jython et swing.ImageIcon()

2 réponses
Avatar
Sébastien Ramage
Bonjour,
j'ai un petit probl=E8me en Jython avec swing.ImageIcon()

voil=E0 je voudrais pouvoir charger mon image depuis un file-like object
(StringIO) mais je ne sais pas comment le passer =E0 ImageIcon()?

il est possible (d'apr=E8s la doc) de passer "an array of bytes which
were read from an image file containing a supported image format, such
as GIF or JPEG."

mon fichier est au format jpeg mais comment le transformer en array of
bytes???

Seb

2 réponses

Avatar
kaerbuhez
Bonjour,
j'ai un petit problème en Jython avec swing.ImageIcon()

voilà je voudrais pouvoir charger mon image depuis un file-like object
(StringIO) mais je ne sais pas comment le passer à ImageIcon()?

il est possible (d'après la doc) de passer "an array of bytes which
were read from an image file containing a supported image format, such
as GIF or JPEG."

mon fichier est au format jpeg mais comment le transformer en array of
bytes???

Seb



# donner le path de l'icone en premier argument
import sys
from javax import swing
bytes_string=open(sys.argv[1]).read()
bytes_array=[ord(x) for x in bytes_string]
i=swing.ImageIcon(bytes_array)
print i.iconHeight!=-1 and 'Réussi !' or 'Raté !'

Script testé sous jython2.1/java1.5/WinXP.

Avatar
Sébastien Ramage
ooh ça fonctionne à merveille
yavait rien de bien compliqué en fait, et j'apprends une nouvelle
syntaxe au passage, c'est court et efficace

merci beaucoup :)