Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Converting TIFF files to base64 using Java

Hi 

I have two instances where I'm converting a TIFF file before sending the data to google vision AI. Using CLI, the base64 string works fine. I get the expected response

When I use Java I'm getting a different out, which causes my API call to fail. The base64 output is different.

 

imageBytes = Files.readAllBytes(new File(fileLocation).toPath());
String imageString = Base64.getEncoder().withoutPadding().encodeToString(imageBytes);
// API response "message": "Unsupported input file format."

 

I'm looking for suggestions on how to convert a file (tiff, pdf, png) to base64 using plain Java

 

0 1 1,094
1 REPLY 1

You could convert your images as shown in this sample code from this Stack Overflow question:

    File File = new File(args[2]);
    String ImageString;

    FileInputStream fileInputStreamReader = new FileInputStream(File);
    byte[] bytes = new byte[(int)File.length()];
    fileInputStreamReader.read(bytes);
    ImageString = Base64.getEncoder().encodeToString(bytes);

See also: