Update ImageUtil

Penambahan method encoding file to base64 binary
This commit is contained in:
Salman Manoe 2022-01-14 08:16:02 +07:00
parent f4916a6a20
commit 78d0991e64

View File

@ -8,6 +8,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -15,6 +16,7 @@ import java.io.OutputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -31,8 +33,20 @@ public final class ImageUtil {
/** The Constant DEFAULT_IMAGE_FORMAT. The default image format is PNG */ /** The Constant DEFAULT_IMAGE_FORMAT. The default image format is PNG */
private static final String DEFAULT_IMAGE_FORMAT = "PNG"; private static final String DEFAULT_IMAGE_FORMAT = "PNG";
private ImageUtil() { public static String encodeFileToBase64Binary(File file) {
// to prevent instantiation String result = "";
try {
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fileInputStreamReader.read(bytes);
result = new String(Base64.encodeBase64(bytes), "UTF-8");
fileInputStreamReader.close();
return result;
} catch (Exception e) {
return result;
}
} }
/** /**
@ -50,14 +64,13 @@ public final class ImageUtil {
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static byte[] resizeImage(byte[] imageData, int scaledWidth, public static byte[] resizeImage(byte[] imageData, int scaledWidth, int scaledHeight, boolean preserveAlpha)
int scaledHeight, boolean preserveAlpha) throws IOException { throws IOException {
if (ArrayUtils.isEmpty(imageData)) { if (ArrayUtils.isEmpty(imageData)) {
return new byte[] {}; return new byte[] {};
} }
BufferedImage originalImage = createImage(imageData); BufferedImage originalImage = createImage(imageData);
BufferedImage resizedImage = resizeImage(originalImage, scaledWidth, BufferedImage resizedImage = resizeImage(originalImage, scaledWidth, scaledHeight, preserveAlpha);
scaledHeight, preserveAlpha);
return getImageBytes(resizedImage); return getImageBytes(resizedImage);
} }
@ -74,21 +87,16 @@ public final class ImageUtil {
* Set the value to false to keep the transparency * Set the value to false to keep the transparency
* @return Resized image * @return Resized image
*/ */
public static BufferedImage resizeImage(Image image, int scaledWidth, public static BufferedImage resizeImage(Image image, int scaledWidth, int scaledHeight, boolean preserveAlpha) {
int scaledHeight, boolean preserveAlpha) { int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
: BufferedImage.TYPE_INT_ARGB;
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight,
imageType);
Graphics2D g = scaledBI.createGraphics(); Graphics2D g = scaledBI.createGraphics();
if (preserveAlpha) { if (preserveAlpha) {
g.setComposite(AlphaComposite.Src); g.setComposite(AlphaComposite.Src);
} }
// we prefer quality than speed // we prefer quality than speed
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawImage(image, 0, 0, scaledWidth, scaledHeight, null); g.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
g.dispose(); g.dispose();
return scaledBI; return scaledBI;
@ -103,8 +111,7 @@ public final class ImageUtil {
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static BufferedImage createImage(byte[] imageData) public static BufferedImage createImage(byte[] imageData) throws IOException {
throws IOException {
InputStream input = new ByteArrayInputStream(imageData); InputStream input = new ByteArrayInputStream(imageData);
return ImageIO.read(input); return ImageIO.read(input);
} }
@ -123,8 +130,7 @@ public final class ImageUtil {
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static String storeImage(String filename, String extension, public static String storeImage(String filename, String extension, byte[] imageData) throws IOException {
byte[] imageData) throws IOException {
String ext = extension; String ext = extension;
// extension must have dot (.) character in front of it // extension must have dot (.) character in front of it
if (extension.charAt(0) != '.') { if (extension.charAt(0) != '.') {
@ -160,4 +166,5 @@ public final class ImageUtil {
ImageIO.write(image, DEFAULT_IMAGE_FORMAT, output); ImageIO.write(image, DEFAULT_IMAGE_FORMAT, output);
return output.toByteArray(); return output.toByteArray();
} }
} }