Using Image Item as attachment

  • Platform information:
    • Hardware: Pi3
    • OS: Raspbian
    • Java Runtime Environment: java version “1.8.0_201”
    • openHAB version: 2.4 Stable

I looked up the forums, but could not find the information i was after.

I have IP camera bindings from which I have created image ITEM.

The Item can be displayed in HABpanel and sitemap without issues. I am trying to attach the image from ITEM for push notification through email and pushover, but runnning into issues.

My Items file for cameras:
Image IPC_Alfresco_Image “IPC Alfresco Image” (gIPC) {channel=“ipcamera:DAHUA:24bcbf06:image”}

Image IPC_Livingroom_Image “IPC Livingroom Image” (gIPC) {channel=“ipcamera:DAHUA:13c74b74:image”}

This is the response of rest api:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT//g/+AHwAAPYYaFwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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MCOtIBTgaBl9WBUN61UuI/LcnHBqS3fqp71LIu9Sp7dKAKVSI+DUR4NOoAuRSHII6g5FdnZTm

But i cannot use the item.state as attachment or saving it to a image variable.

Any hints?

Thanks

The image is encoded as base64 inside the Image Item state. You will need to parse and convert that encoding back to binary and save it as a jpg file before you can send it as an attachment.

I don’t know how off the top of my head but searching the web for “java parse base64” should give you some examples and/or tutorials.

Thanks rlkoshak,

Will check it out…

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

I stumbled onto another method that would work ffmpeg.

To save a picture from the RTSP stream this works but you need to disable authentication for ONVIF for it to work. Normally you can not disable authentication for Dahua cameras but I found in my Amcrest camera it is possible to leave it off for ONVIF whilst the rest stays protected via digest.

In rules you can use the execute method to run this command.

ffmpeg -t 1 -i 'rtsp://192.168.1.50:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif' -vframes 1 /etc/openhab2/html/camerapicture.jpg

More info here…

Hi @Dileepa_R,

thank you for your code on how to “export” an image item to a file.
As I’m not into java, would it be possible to load a jpeg-URL and apply this to an image item from this code?

Thanks & best Regards,
Sascha

Hi,
I am not sure as I am not good with java as well.

In my case i had to save the image from the item file to hard drive so i can add it as a attachment for pushover.

Cheers

The ipcamera binding will now create an animated gif for you that moves which you can send with pushover or telegram.

Further reducing the load on the openhab server, I’ve changed above code to just plain write the base64 decoded image to disk without resizing it:

import javax.xml.bind.DatatypeConverter
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

rule "SavePicture"
when
  Item imgLastGong changed or
  Item Frontdoor_state changed to OPEN
then
  logDebug("Imagerule", triggeringItem.name)
  var String CameraImage = triggeringItem.state.toFullString //Convert image item to base64 encoded string

  val String Image64 = CameraImage.split(",").get(1) //Slice the string up to the delimiter ","
  var byte[] data = DatatypeConverter.parseBase64Binary(Image64)

  try {
    var OutputStream os

    val time = new DateTime().millisOfDay().getMillis()
    var File f = new File("/etc/openhab2/html/" + triggeringItem.name + "_" + time + ".jpg")
    os = new FileOutputStream(f)
    os.write(data);

    os.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
end
2 Likes

can you please to help me to resize it image