My application is visualizing images from Database. I am using Paintable Canvas to view images, but the image doesn´t refresh when I selected registers continuously from database table
Where is this code? In the paint method? If so, break it up so your db query runs separately (on some propertyChange) to obtain and set the client property. Say, on your Documents table, have a propertyChange that does most of the work, but only on image change, like so:
if event.propertyName == 'id':
from javax.imageio import ImageIO
from java.io import ByteArrayInputStream
blob = system.db.runPrepQuery(“SELECT file FROM magna_silao.documents where id= ?”, [event.newValue])
bytes = blob[0][0]
image = ImageIO.read(ByteArrayInputStream(bytes))
canvas = event.source.parent.getComponent("Paintable canvas")
canvas.putClientProperty("cached-image", image)
canvas.repaint()
Then your paint event only has to paint, and only if the cached image has been processed:
image = self.getClientProperty(“cached-image”)
if image:
event.graphics.drawImage(image, 0, 0, event.source)
Note that painting can be called really often – any time the anything obscures the canvas or updated. Or various window/container events.
{ Also, when pasting code or other plain text in the forum, use a line with just three backquotes above the code, and again below the code, so it formats nicely. }
You’ve got fancy non-ASCII quotes on line five from copy-pasting from the forum (notice the syntax highlighting, or lack thereof). Replace them with standard ".
Great catch, thanks! Now that part works, however the paintable canvas only pulls up the default image when I select a value. I assume I have to put something besides the default scripting into the repaint, is that correct?
Hmm, is it the table name it doesn’t like or some other syntax issue? When I use the DB Query browser, it accepts that table name. Maybe I need to specify the DB source name, since it is not the default DB?
Would I add the database like this?
#print event.propertyName
if event.propertyName == 'selectedStringValue':
from javax.imageio import ImageIO
from java.io import ByteArrayInputStream
blob = system.db.runPrepQuery("SELECT ImageSource FROM Image_Classification_Master WHERE ImagePath= ?", [event.newValue], "MyDatabaseName")
bytes = blob[0][0]
image = ImageIO.read(ByteArrayInputStream(bytes))
canvas = event.source.parent.getComponent("Paintable Canvas")
canvas.putClientProperty("cached-image", image)
canvas.repaint()
The general functionality seems to be working now, however the images are just showing up as a blank white image. I’m using SQL server, if that makes a difference, I’m not sure. You can see my script for the dropdown in my last post. On the paintable canvas, I have the code from @pturmel above in the repaint:
image = self.getClientProperty(“cached-image”)
if image:
event.graphics.drawImage(image, 0, 0, event.source)
I figured out the issue, the image was being pulled properly, but the canvas was too small to show it properly, so I was only seeing the blank white corner of the image. I found a way to scale it to the canvas in this post:
image = event.source.getClientProperty("cached-image")
if image:
# 1. Get the width and height of the BufferedImage object returned from ImageIO.read call:
imageW = image.getWidth()
imageH = image.getHeight()
# 2. Get the width and height of the Paintable Canvas component
canvasW = event.width
canvasH = event.height
# 3. Calculate the scale factors;
# Account for 1 pixel of the component's border dimensions
scaleX = (canvasW-1.0) / imageW
scaleY = (canvasH-1.0) / imageH
# 4. Apply scaling and draw the image
g = event.graphics
g.scale(scaleX, scaleY)
g.drawImage(image,0,0,event.source)
#previous code, kept for reversion only
#if image:
# event.graphics.drawImage(image, 0, 0, event.source)