Display images stored in a DB on canvas

Hi guys,

I’m trying to display images on canvas. The images are storage in a DB (mysql) in blob format. I read this post and I did the same but I’m getting an error when the blob data coming from the DB is converted to stream and then used by image class. The part of the code throwing the error is this:

bais = ByteArrayInputStream(bytes) image = ImageIO.read(bais)

and the error is:

java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: -1 caused by ArrayIndexOutOfBoundsException: -1

I have checked the info coming from the DB and it seems to be ok. The data type is array.

               <type 'array.array'>

On the other hand I tried using java.net.URL class to read an image on internet and then use it as Image input and that worked fine.

img_url = URL("http://viewik.ru/wp-kantent/uploads/2013/01/foto-0001.jpg") image = ImageIO.read(img_url)

I would really appreciate a solution for this, using ByteArrayInputStream or something different.

thanks

When possible, you should try to send as much code as you can. Also send the entire error message (or at least the line number where the error occurred). Your code looks fine.

Here is all the code:

[code]from java.awt import Color
from java.awt import GradientPaint
from java.util import Date
from java.text import SimpleDateFormat

Obtain date

#df = SimpleDateFormat(‘yyyy-MM-dd HH:mm’)
#fecha = event.source.parent.getComponent(“Group”).getComponent(“calendario”).date
#fecha_formateada = df.format(fecha)

Var used to create the query

#id_pozo = event.source.id_pozo

Hardcoded query for testing

query = “select imagen from Pozo_Fotos where fecha_captura like ‘2014-03-27 20:24%’”

Run query on DB.

data = system.db.runQuery(query, “rkn”)

g = event.graphics

Verify query result

if len(data) >= 1:
bytes = data[0][0]

from javax.imageio import ImageIO
from java.io import ByteArrayInputStream
from java.net import URL

# USING THIS THE IMAGE IS DEPLAYED
#img_url = URL("http://viewik.ru/wp-kantent/uploads/2013/01/foto-0001.jpg")
#image = ImageIO.read(img_url)

# USING THIS I GOT THE ERROR
#bais = ByteArrayInputStream(bytes)
#image = ImageIO.read(bais)
#bais.close()

####BEGIN SCALED IMAGE####
scaledImage = image.getScaledInstance(640,-1,0)
event.graphics.drawImage(scaledImage, 0, 0, event.source)
####END SCALED IMAGE####[/code]

And this is the error:

[code]Traceback (most recent call last):
File “event:repaint”, line 36, in

java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: -1

caused by ArrayIndexOutOfBoundsException: -1

Ignition v7.6.4 (b2013112117)
Java: Oracle Corporation 1.7.0_51[/code]

See if bytes is None. Uncomment out your lines giving the error and paste the error message too.

I expressed myself wrong, when I got the error the lines are uncomment.

bais = ByteArrayInputStream(bytes) image = ImageIO.read(bais) bais.close()

The error is:

[code]Traceback (most recent call last):
File “event:repaint”, line 36, in

java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: -1

caused by ArrayIndexOutOfBoundsException: -1

Ignition v7.6.4 (b2013112117)
Java: Oracle Corporation 1.7.0_51[/code]

On the other hand I checked the type of “bytes” and it’s an array

I found the problem, I was saving the images on the DB using " MySQLdb.escape_string" and that was causing the problem. Now I’m saving the image in blob format whiout escape_string on the DB and the image is displayed using the same code I posted.