Image from stream

I am trying to fetch an image and use it as an CameraImage. But there is (as always) a great lack of samples. This does not make an image, and I guess it is because I dont understand this magic.

I need to do it this way, ordinary url files when using chrome on a desktop (works fine on phone)

Anyone able to make a short sample?

async onInit() {
	myImage.setStream(async (stream) => {
	    const res = await this.homey.fetch("https://IMGURL");
	    if (!res.ok) {
		throw new Error("Invalid Response");
	    }

	    return res.body.pipe(stream);
	});
	this.setCameraImage('blabla', "this is my testimage", myImage);
}

Please check the type of res.body if it’s a streaming object. I use res.pipe.stream instead, but I don’t use fetch(). Perhaps the body is already converted to string/json.

Example:

I use standard https module to save some kB memory for additional NPM modules :grinning_face_with_smiling_eyes:.

PS: the handler registered in setStream is called every time the image is requestes. This is done in device camera/image view but also when the image token is used in AdvancedFlow (e.g. sending the image as push message).

Great, this was the sample I needed to go further. Thanks!

If it wasn’t, res.body.pipe() would throw an error.

1 Like