Using Image Item as attachment

Here is the final code that is working: Thanks to google and
https://pavel.tcholakov.net/2008/12/quick-and-easy-image-resizing-with-java-imageio/

img.getScaledInstance(800 = the scaled width as per requirement

    var String CameraImage = IPC_Alfresco_Image.state.toFullString //Convert image item to base64 encoded string
    //logInfo("Imagerule", CameraImage)
    val String Image64 = CameraImage.split(",").get(1) //Slice the string up to the delimiter ","
    //logInfo("Imagerule", Image64)
    
    var byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(Image64) //decode the base64

    var BufferedImage img 
	try {
		img = ImageIO.read(new ByteArrayInputStream(imageBytes)) //read the decoded file into buffer	
        var thumbnail = img.getScaledInstance(800, -1, Image.SCALE_SMOOTH) //Scaling the buffered image
        var BufferedImage bufferedThumbnail = new BufferedImage(thumbnail.getWidth(null),
                                                    thumbnail.getHeight(null),
                                                    BufferedImage.TYPE_INT_RGB)
        bufferedThumbnail.getGraphics().drawImage(thumbnail, 0, 0, null)
        // write the image to a file
        var String path = File.separator + "etc" + File.separator + "openhab2" + File.separator + "html" + File.separator + "image.png"
        logInfo("File location", path)
	    val File outputfile = new File(path)
        ImageIO.write(bufferedThumbnail, "png", outputfile)
    }
        catch (IOException e) {
		                        // TODO Auto-generated catch block
		                        logInfo("Testing",e.printStackTrace())
	                            }
    
end
3 Likes