Back

Remove/replace the escape character in the base64 compressed image in Java

  • 0
  • Functions
Tin
5 Aug, 2023, 02:43

Hello, My team and I are currently working on issue #4125 as part of our CodeDay internship. Our goal is to create a compressed image function in Java.

The compressed image returned in base64 format is generally good, but we've encountered some cases where escape characters appear (as shown in the attached screenshot 1 & 2), which renders the result invalid.

I attempted to remove these characters using the "replaceAll("", "")" method, but encountered an error (also shown below screenshot 3).

Could you please advise on the correct way to resolve this issue? Thank you.

TypeScript
public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception {
...
// compressed image in Base64 string
        String compressedImage = "";
        Map<String, Object> responseData = new HashMap<>();

        if (Provider.TINY_PNG.getName().equals(provider)) {
            // Decode image from Base64 string
            byte[] imageByte = convertToByte(image);

            // Compress image
            byte[] compressedImageByte = tinifyCompress(imageByte, apiKey);

            // Encode image to Base64 string
            compressedImage = convertToBase64(compressedImageByte).replaceAll("\\", "");
        }

        responseData.put("success", true);
        responseData.put("image", compressedImage);
        return res.json(responseData);
}
TL;DR
The user is working on a Java code where they are implementing a compressed image function. They are encountering escape characters in the base64 format of the compressed image, which is causing the result to be invalid. They tried to remove the escape characters using "replaceAll("\\", "")", but it resulted in an error. They are seeking advice on how to correctly resolve this issue. Solution: Instead of using "replaceAll("\\", "")", the user should use "replace("\\", "")" to remove the escape characters from the base64 string.
Drake
5 Aug, 2023, 03:20

Maybe that backslash is just because it's JSON encoded?

Tin
5 Aug, 2023, 04:19

Yes, I think it’s JSON encoded. Should we just leave it?

Drake
5 Aug, 2023, 04:19

Go ahead and submit the PR and we'll see

Tin
5 Aug, 2023, 04:34

Thank you so much, I will do that.

joeyouss
8 Aug, 2023, 10:11

Adding the link here for reference : https://github.com/appwrite/appwrite/issues/4125

Drake
8 Aug, 2023, 15:53

Did you submit the PR yet?

Tin
8 Aug, 2023, 18:31

I intend to submit the PR once we finish implementing the compressed image Kraken IO method. Would you like me to submit the current code?

Drake
8 Aug, 2023, 18:34

Up to you. I thought it was already submitted but I didn't see the PR in the issue

Tin
8 Aug, 2023, 18:46

Once we complete the final method, expected to be done within this week, I will submit it and inform you. Thank you!

Tin
11 Aug, 2023, 19:09

Hi, I want to inform you that we submitted the PR. Here is the link: https://github.com/open-runtimes/examples/pull/174 Thank you.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more