private void imageRotateScale(String imageFilePath, String outImagePath) {
File input = new File(imageFilePath);
File output = new File(outImagePath);
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(input);
//Create a blank, RGB, same width and height, and a white background
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
//Scale the Image by half, Change 0.5 to Whatever Value you want it to Shrink
newBufferedImage = scale(newBufferedImage, (int) Math.round(bufferedImage.getWidth() * 0.5), (int) Math.round(bufferedImage.getHeight() * 0.5));
//Time to Rotate the Image
int w = newBufferedImage.getWidth();
int h = newBufferedImage.getHeight();
AffineTransform scaleTransform = new AffineTransform();
scaleTransform.translate(h/2,w/2);
scaleTransform.rotate(-1 * (Math.PI / 2));
scaleTransform.translate(-w/2,-h/2);
AffineTransformOp scaleOp = new AffineTransformOp(
scaleTransform, AffineTransformOp.TYPE_BILINEAR);
newBufferedImage = scaleOp.filter(newBufferedImage, null);
//Write the output Image file
ImageIO.write(newBufferedImage, "jpg", output);
} catch (IOException e) {
e.printStackTrace();
}
}
private BufferedImage scale(BufferedImage image, int targetWidth, int targetHeight) {
int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage newImage = image;
BufferedImage scratchImage = null;
Graphics2D g2 = null;
int w = image.getWidth();
int h = image.getHeight();
int prevW = w;
int prevH = h;
do {
if (w > targetWidth) {
w /= 2;
w = (w < targetWidth) ? targetWidth : w;
}
if (h > targetHeight) {
h /= 2;
h = (h < targetHeight) ? targetHeight : h;
}
if (scratchImage == null) {
scratchImage = new BufferedImage(w, h, type);
g2 = scratchImage.createGraphics();
}
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(newImage, 0, 0, w, h, 0, 0, prevW, prevH, null);
prevW = w;
prevH = h;
newImage = scratchImage;
} while (w != targetWidth || h != targetHeight);
if (g2 != null) {
g2.dispose();
}
if (targetWidth != newImage.getWidth() || targetHeight != newImage.getHeight()) {
scratchImage = new BufferedImage(targetWidth, targetHeight, type);
g2 = scratchImage.createGraphics();
g2.drawImage(newImage, 0, 0, null);
g2.dispose();
newImage = scratchImage;
}
return newImage;
}
All one can think and do in a short time is to think what one already knows and to do as one has always done!
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.