Validate a Base64 encoded byte array is a valid Image or not

Hi,
I want to validate an incoming Base64 byte array is a valid Image or not. I need to identify any invalid/corrupt image and proceed accordingly. Is there an Aspose functionality to achieve it (in java) ?

Need assistance on this issue.

Thanks in advance

@pasa2018, hello,
Here is a function that might be useful:

using System.IO;
using System.Text;
using Aspose.Imaging;

public static bool IsImage(byte[] base64Bytes)
{
    var bytes = System.Convert.FromBase64String(Encoding.UTF8.GetString(base64Bytes));
    using var ms = new MemoryStream(bytes);
    return Image.CanLoad(ms);
}

Hi @Denis.Sitko,
Thanks for the information, but I am using Java 11 so could you please provide the equivalent code in Java Instead ?

@pasa2018
Hello!
For Java you can use the following code


public static boolean isImage(String base64String)
{
	byte[] decodedImage = Base64.getDecoder().decode(base64String);
	try (ByteArrayInputStream stream = new ByteArrayInputStream(decodedImage))
	{
		return Image.canLoad(stream);
	}
}

public static boolean isImage(byte[] base64Bytes)
{
	byte[] decodedImage = Base64.getDecoder().decode(base64Bytes);
	try (ByteArrayInputStream stream = new ByteArrayInputStream(decodedImage))
	{
		return Image.canLoad(stream);
	}
}