Hello All,
“I THINK” I am close to have this working but hung up “File” not defined. Thanks in advance!
Client side script:
import base64
import java.io.File
import java.io.ByteArrayOutputStream
from javax.imageio import ImageIO
from java.io import File # Explicitly import File here
print ("handleImage started")
def sendImage():
# Define the path to the image file
image_path = "C:/users/public/pictures/Shiny_KPI.jpg" # Change to your image path
# Read the image file
image_file = File(image_path)
image = ImageIO.read(image_file)
# Encode image to base64 string for transmission
output = ByteArrayOutputStream()
ImageIO.write(image, "jpg", output) # Save as jpg
image_bytes = output.toByteArray()
base64_image = base64.b64encode(image_bytes).decode('utf-8')
# Send the request to the gateway
response = system.util.sendRequest(
"LMIDRILL_KPI", # Replace with your actual project name
"handleImage", # Name of the message handler you will define
{"image": base64_image} # Sending the image
)
# Handle the response
if response['success']:
system.gui.messageBox("Success: {}".format(response['message']))
else:
system.gui.messageBox("Failed: {}".format(response['message']))
# Call the function to send the image
sendImage()
The Gateway side script Message handler (its named (called) “handleImage”:
def handleMessage(payload):
"""
This message handler will be called each time a message of this
type is received.
Arguments:
payload: A dictionary that holds the objects passed to this
message handler. Retrieve them with a subscript, e.g.
myObject = payload['argumentName']
"""
logger = system.util.logger("ImageHandler")
logger.info("Image handler started")
try:
# Get the Base64 encoded image from payload
base64_image = payload['image']
logger.info("Received image payload.")
# Decode the image
import base64
from java.awt import Image
from java.awt.image import BufferedImage
from javax.imageio import ImageIO
from java.io import ByteArrayInputStream, File
image_data = base64.b64decode(base64_image)
input_stream = ByteArrayInputStream(image_data)
buffered_image = ImageIO.read(input_stream)
# Save the image to disk
output_file = "C:/path/to/save/image/Shiny_KPI_received.jpg" # Change path as needed
ImageIO.write(buffered_image, "jpg", File(output_file))
logger.info("Image saved successfully.")
return {"success": True, "message": "Image received and saved successfully!"}
except Exception as e:
logger.error("Error processing image: " + str(e))
return {"success": False, "message": "Error processing the image."}
I am having an error on the client side “NameError: global name ‘File’ is not defined”, this should be defined with “import java.io.File” and or “from java.io import File”
Thank You