Does anyone know if there’s a way I can resize an image/blob before I upload it to my database?
Here’s what I’m doing right now to upload/
import fpmi
from java.net import URL
from javax.imageio import ImageIO
#set the id
myID = event.source.parent.ComplaintId
if myID != None:
#prompt user to select file
path = fpmi.file.openFile()
#if user selects a file, and does not click cancel, do this
if path != None:
bytes = fpmi.file.readFileAsBytes(path)
#write the data to the database, and return the id of the new row
fpmi.db.runPrepStmt("INSERT INTO TBL_IMAGE (COMPLAINT_ID, IMAGEFILE) VALUES(?,?)", (myID,bytes))
I guess another question could also be; ‘Is there a way to scale an image with the printable canvas’? That might be easier since that’s where this will eventually be displayed.
You should be able to set your scale in the paintable canvas, it’s just using the Java 2d drawing in there. I think you can call scale() to change it just like in the sample code, but I haven’t tried with an image.#### Scale graphics to actual component size
dX = (event.width-1)/100.0
dY = (event.height-1)/100.0
g.scale(dX,dY)
Hmm. BufferedImage is a subclass of Image that implements RenderedImage. .getScaledInstance() doesn’t. So it won’t be simple. You’ll have to use an AffineTransform with an AffineTransformOp. I use these in my streamer module but at at fixed scales, so I don’t have an example for you.
Are you using this within a perspective “onFileReceived” script? what librarys are you calling in? I get an error - global name 'java' is not defined on the first line of this code.
For displaying you can use a Paintable Canvas or download from database and display
Download Script:
x = system.db.runScalarQuery('select filedata from Avaliacao5SImages where id = 107',"DB_IGNITION")
from org.apache.commons.io import IOUtils
from java.io import ByteArrayInputStream,FileOutputStream
Is there a way to use this by opening a file from a path first? Where it has:
filedata = event.file.getBytes()
I'm trying to replace it with something to open the image from a path (mainly for testing from the script console) but doesn't get a recognizable format.
Answered my own question, but in case anyone else want to know, just need to read the file in bytes in the beginning. I also included a bonus method to scale the size to a ratio instead of static (this keeps the picture in the same ratio).