Update ImageUtil
Penambahan method encoding file to base64 binary
This commit is contained in:
parent
f4916a6a20
commit
78d0991e64
@ -8,6 +8,7 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -15,6 +16,7 @@ import java.io.OutputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
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 */
|
||||
private static final String DEFAULT_IMAGE_FORMAT = "PNG";
|
||||
|
||||
private ImageUtil() {
|
||||
// to prevent instantiation
|
||||
public static String encodeFileToBase64Binary(File file) {
|
||||
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
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static byte[] resizeImage(byte[] imageData, int scaledWidth,
|
||||
int scaledHeight, boolean preserveAlpha) throws IOException {
|
||||
public static byte[] resizeImage(byte[] imageData, int scaledWidth, int scaledHeight, boolean preserveAlpha)
|
||||
throws IOException {
|
||||
if (ArrayUtils.isEmpty(imageData)) {
|
||||
return new byte[] {};
|
||||
}
|
||||
BufferedImage originalImage = createImage(imageData);
|
||||
BufferedImage resizedImage = resizeImage(originalImage, scaledWidth,
|
||||
scaledHeight, preserveAlpha);
|
||||
BufferedImage originalImage = createImage(imageData);
|
||||
BufferedImage resizedImage = resizeImage(originalImage, scaledWidth, scaledHeight, preserveAlpha);
|
||||
return getImageBytes(resizedImage);
|
||||
}
|
||||
|
||||
@ -74,21 +87,16 @@ public final class ImageUtil {
|
||||
* Set the value to false to keep the transparency
|
||||
* @return Resized image
|
||||
*/
|
||||
public static BufferedImage resizeImage(Image image, int scaledWidth,
|
||||
int scaledHeight, boolean preserveAlpha) {
|
||||
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB
|
||||
: BufferedImage.TYPE_INT_ARGB;
|
||||
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight,
|
||||
imageType);
|
||||
public static BufferedImage resizeImage(Image image, int scaledWidth, int scaledHeight, boolean preserveAlpha) {
|
||||
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
|
||||
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
|
||||
Graphics2D g = scaledBI.createGraphics();
|
||||
if (preserveAlpha) {
|
||||
g.setComposite(AlphaComposite.Src);
|
||||
}
|
||||
// we prefer quality than speed
|
||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
||||
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
g.setRenderingHint(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY);
|
||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
g.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
|
||||
g.dispose();
|
||||
return scaledBI;
|
||||
@ -103,8 +111,7 @@ public final class ImageUtil {
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static BufferedImage createImage(byte[] imageData)
|
||||
throws IOException {
|
||||
public static BufferedImage createImage(byte[] imageData) throws IOException {
|
||||
InputStream input = new ByteArrayInputStream(imageData);
|
||||
return ImageIO.read(input);
|
||||
}
|
||||
@ -123,8 +130,7 @@ public final class ImageUtil {
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static String storeImage(String filename, String extension,
|
||||
byte[] imageData) throws IOException {
|
||||
public static String storeImage(String filename, String extension, byte[] imageData) throws IOException {
|
||||
String ext = extension;
|
||||
// extension must have dot (.) character in front of it
|
||||
if (extension.charAt(0) != '.') {
|
||||
@ -160,4 +166,5 @@ public final class ImageUtil {
|
||||
ImageIO.write(image, DEFAULT_IMAGE_FORMAT, output);
|
||||
return output.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user