Hi there,
I am trying to print an image onto a Paintable Canvas component (named “tape_image”) by passing the image bytes (called “imageData”) as a string type custom property on it. Converting the String to a ByteArrayInputStream works, but when calling ImageIO.read on that returns null. Why could this be happening?
Paintable Canvas repaint script:
[code]from javax.imageio import ImageIO
from java.io import ByteArrayInputStream
width = event.source.getWidth()
height = event.source.getHeight()
bais = ByteArrayInputStream(event.source.imageData)
print bais.available()
image = ImageIO.read(bais) #error here, image = null
print image
bais.close()
finalWidth = width
finalHeight = height
imgRatio = float(image.getWidth())/float(image.getHeight())
#-If Image Ratio exists, determine final width and height to fit canvas-
if (imgRatio > 0):
finalWidth = int(height * imgRatio)
if (finalWidth > width):
finalWidth=width
finalHeight = int(finalWidth / imgRatio)
#Center the picture on the canvas
y = (height-finalHeight) / 2
x = (width-finalWidth) / 2
g = event.graphics
#-Draw the Image on the canvas-
g.drawImage(image,x,y,finalWidth,finalHeight,event.source)
[/code]
Button that uploads the image data script:
path = system.file.openFile()
if path != None:
bytes = system.file.readFileAsBytes(path)
event.source.parent.getComponent("tape_image").imageData = bytes
print results and error from testing (uploading image of strawberry):
7748437 (result from printing bais.available())
None (result from printing "image")
09:25:21.300 [AWT-EventQueue-2] ERROR com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter - <HTML>Error executing script for event: <code><b>repaint</b></code><BR>on component: <code><b>tape_image</b></code>.
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
File "<event:repaint>", line 18, in <module>
AttributeError: 'NoneType' object has no attribute 'getWidth'
...
...
...
...