BlobDomain object compression using Graphics2D:
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public static BlobDomain compressBlobImage(BlobDomain blobD) {
OutputStream out = null;
BlobDomain blobDomain = new BlobDomain();
try{
BufferedImage img = ImageIO.read(blobD.getInputStream());
out = blobDomain.getBinaryOutputStream();
int width = img.getWidth();
int height = img.getHeight();
int imgWidth, imgHeight;
if (height > width) {
imgWidth = (img.getWidth() * 300)/ img.getHeight(); // 300 is the desired image size
imgHeight = 300;
} else {
imgHeight = (img.getHeight() * 300)/ img.getWidth();
imgWidth = 300;
}
final BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(img, 0, 0, imgWidth, imgHeight, null);
graphics2D.dispose();
ImageIO.write(bufferedImage, "jpg", out); // Desired compress image type as jpg
} catch (IOException e) {
logger.severe(e);
} catch (SQLException e) {
logger.severe(e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
logger.severe(e);
}
}
return blobDomain;
}
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public static BlobDomain compressBlobImage(BlobDomain blobD) {
OutputStream out = null;
BlobDomain blobDomain = new BlobDomain();
try{
BufferedImage img = ImageIO.read(blobD.getInputStream());
out = blobDomain.getBinaryOutputStream();
int width = img.getWidth();
int height = img.getHeight();
int imgWidth, imgHeight;
if (height > width) {
imgWidth = (img.getWidth() * 300)/ img.getHeight(); // 300 is the desired image size
imgHeight = 300;
} else {
imgHeight = (img.getHeight() * 300)/ img.getWidth();
imgWidth = 300;
}
final BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(img, 0, 0, imgWidth, imgHeight, null);
graphics2D.dispose();
ImageIO.write(bufferedImage, "jpg", out); // Desired compress image type as jpg
} catch (IOException e) {
logger.severe(e);
} catch (SQLException e) {
logger.severe(e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
logger.severe(e);
}
}
return blobDomain;
}
No comments:
Post a Comment