Cannot convert image in to byte array in groovy

Hi there,

I need to store screenshot result in to excel file, I have triad the code below as I usually used in Java:

InputStream image = new FileInputStream(imagePath);
bytes[] bytes = IOUtils.toByteArray(image);

But I got exception "groovy.lang.MissingPropertyException: No such property: bytes for class:…"

Thanks in advance,

Tarmizi

hi,

import org.apache.commons.io.IOUtils
InputStream image = new FileInputStream(“C:\data\screenshots\test-image2.png”);
bytes = IOUtils.toByteArray(image);

print out will get bytestring
[-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 2, 32, 0, 0, 0, -72…

Just a typo?

byte[] bytes = IOUtrils.toByteArray(image);

Hi @Timo_Kuisma1. It was already imported.
Many thanks for response.

Hi @kazurayam,
I think it was not typo.

Thanks.

I solved this by replacing IOUtils.toByteArray(image) with:

	BufferedImage bImage = ImageIO.read(new File(imagePath));
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	ImageIO.write(bImage, "png", bos );
	byte [] data = bos.toByteArray(); 

I spent one day to solve this issue :frowning: