rotate image

This commit is contained in:
krrishg
2023-12-11 14:35:12 +05:45
parent 9791e4bb0f
commit 57cbf3a398
@@ -3,6 +3,8 @@ package com.krrishg;
import org.apache.commons.io.FileUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -12,14 +14,14 @@ import java.util.Base64;
import java.util.Objects;
import java.util.Optional;
public class ImageService {
public class ImageUtils {
public String encodeImageToBase64(File file) throws IOException {
public static String encodeImageToBase64(File file) throws IOException {
byte[] fileContent = FileUtils.readFileToByteArray(file);
return Base64.getEncoder().encodeToString(fileContent);
}
public String encodeImageToBase64(BufferedImage image, String format) throws IOException {
public static String encodeImageToBase64(BufferedImage image, String format) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, format, outputStream);
outputStream.flush();
@@ -30,7 +32,7 @@ public class ImageService {
return Base64.getEncoder().encodeToString(imageBytes);
}
public String combineTwoImagesVertically(String firstImageBase64, String secondImageBase64, String format)
public static String combineTwoImagesVertically(String firstImageBase64, String secondImageBase64, String format)
throws IOException {
if (firstImageBase64 == null) {
firstImageBase64 = "";
@@ -80,7 +82,7 @@ public class ImageService {
return ImageIO.read(inputStream);
}
public String combineTwoImagesHorizontally(String firstImageBase64, String secondImageBase64, String format)
public static String combineTwoImagesHorizontally(String firstImageBase64, String secondImageBase64, String format)
throws IOException {
if (firstImageBase64 == null) {
firstImageBase64 = "";
@@ -124,7 +126,7 @@ public class ImageService {
return Base64.getEncoder().encodeToString(imageBytes);
}
public BufferedImage cropImage(String image, int startX, int startY, int width, int height) throws IOException {
public static BufferedImage cropImage(String image, int startX, int startY, int width, int height) throws IOException {
BufferedImage originalImage = getImageFromBase64(image);
width = Math.min(width, originalImage.getWidth() - startX);
@@ -133,12 +135,12 @@ public class ImageService {
return originalImage.getSubimage(startX, startY, width, height);
}
protected BufferedImage getBufferedImage(String imageBase64) throws IOException {
public static BufferedImage getBufferedImage(String imageBase64) throws IOException {
byte[] imageContent = Base64.getDecoder().decode(imageBase64);
return ImageIO.read(new ByteArrayInputStream(imageContent));
}
public BufferedImage adjustContrast(BufferedImage image, double contrastFactor) {
public static BufferedImage adjustContrast(BufferedImage image, double contrastFactor) {
BufferedImage contrastedImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_RGB);
@@ -165,7 +167,7 @@ public class ImageService {
return Math.min(Math.max(value, 0), 255);
}
public BufferedImage convertToBlackAndWhite(BufferedImage image) {
public static BufferedImage convertToBlackAndWhite(BufferedImage image) {
return changeColorScheme(image, BufferedImage.TYPE_BYTE_BINARY);
}
@@ -176,7 +178,32 @@ public class ImageService {
return blackAndWhiteImage;
}
public BufferedImage convertToGrayScale(BufferedImage image) {
public static BufferedImage convertToGrayScale(BufferedImage image) {
return changeColorScheme(image, BufferedImage.TYPE_BYTE_GRAY);
}
public static BufferedImage rotateImage(BufferedImage image, int degrees) {
int width = image.getWidth();
int height = image.getHeight();
int newWidth = (degrees == 90 || degrees == -90) ? height : width;
int newHeight = (degrees == 90 || degrees == -90) ? width : height;
BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, image.getType());
Graphics2D g2d = rotatedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(degrees), newWidth / 2.0, newHeight / 2.0);
g2d.transform(at);
g2d.drawImage(image, (newWidth - width) / 2, (newHeight - height) / 2, null);
g2d.dispose();
return rotatedImage;
}
}