Skip to content

Commit ce2650b

Browse files
paodbjavier-godoy
authored andcommitted
fix: update getCroppedImageBase64 method
1 parent 1a2814a commit ce2650b

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/main/java/com/flowingcode/vaadin/addons/imagecrop/ImageCrop.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,29 @@ public String getCroppedImageDataUri() {
344344
}
345345

346346
/**
347-
* Returns the cropped image data URI as a Base64 encoded byte array. If the image data URI does
348-
* not contain "image/*;base64,", it will be decoded to prevent a null pointer exception.
347+
* Decodes the cropped image data URI and returns it as a byte array. If the image data URI is not
348+
* in the format "data:image/*;base64,", it will be decoded assuming it is a Base64 encoded
349+
* string.
349350
*
350-
* <p>
351-
* This method incorporates work licensed under MIT.
352-
* Copyright 2021-2023 David "F0rce" Dodlek https://github.com/F0rce/cropper
351+
* <p>
352+
* This method incorporates work licensed under MIT. Copyright 2021-2023 David "F0rce" Dodlek
353+
* https://github.com/F0rce/cropper
353354
* </p>
354355
*
355-
* @return byte[] the Base64 encoded byte array of the cropped image
356+
* @return byte[] the decoded byte array of the cropped image
356357
*/
357358
public byte[] getCroppedImageBase64() {
358359
String croppedDataUri = this.getCroppedImageDataUri();
359360
if (StringUtils.isBlank(croppedDataUri)) {
360361
return null;
361362
}
362-
String split = croppedDataUri.split(",")[1];
363-
return (split.length() == 0)
364-
? Base64.getDecoder().decode(croppedDataUri.getBytes(StandardCharsets.UTF_8))
365-
: Base64.getDecoder().decode(split.getBytes(StandardCharsets.UTF_8));
363+
364+
String base64Data = croppedDataUri;
365+
if (croppedDataUri.contains("base64,")) {
366+
base64Data = croppedDataUri.split(",")[1];
367+
}
368+
369+
return Base64.getDecoder().decode(base64Data.getBytes(StandardCharsets.UTF_8));
366370
}
367371

368372
}

0 commit comments

Comments
 (0)