Skip to content

Commit

Permalink
fix: update getCroppedImageBase64 method
Browse files Browse the repository at this point in the history
  • Loading branch information
paodb authored and javier-godoy committed Aug 1, 2024
1 parent 1a2814a commit ce2650b
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,29 @@ public String getCroppedImageDataUri() {
}

/**
* Returns the cropped image data URI as a Base64 encoded byte array. If the image data URI does
* not contain "image/*;base64,", it will be decoded to prevent a null pointer exception.
* Decodes the cropped image data URI and returns it as a byte array. If the image data URI is not
* in the format "data:image/*;base64,", it will be decoded assuming it is a Base64 encoded
* string.
*
* <p>
* This method incorporates work licensed under MIT.
* Copyright 2021-2023 David "F0rce" Dodlek https://github.com/F0rce/cropper
* <p>
* This method incorporates work licensed under MIT. Copyright 2021-2023 David "F0rce" Dodlek
* https://github.com/F0rce/cropper
* </p>
*
* @return byte[] the Base64 encoded byte array of the cropped image
* @return byte[] the decoded byte array of the cropped image
*/
public byte[] getCroppedImageBase64() {
String croppedDataUri = this.getCroppedImageDataUri();
if (StringUtils.isBlank(croppedDataUri)) {
return null;
}
String split = croppedDataUri.split(",")[1];
return (split.length() == 0)
? Base64.getDecoder().decode(croppedDataUri.getBytes(StandardCharsets.UTF_8))
: Base64.getDecoder().decode(split.getBytes(StandardCharsets.UTF_8));

String base64Data = croppedDataUri;
if (croppedDataUri.contains("base64,")) {
base64Data = croppedDataUri.split(",")[1];
}

return Base64.getDecoder().decode(base64Data.getBytes(StandardCharsets.UTF_8));
}

}

0 comments on commit ce2650b

Please sign in to comment.