Java程序旋转图像

2023年 9月 1日 20.0k 0

一个图像文件可以顺时针或逆时针旋转。要旋转图像,需要下载一个随机的图像文件并将其保存在系统的任何文件夹中。此外,需要一个.pdf文件,在打开下载的图像文件后,可以在该特定的.pdf文件中旋转一些角度。对于90度的旋转,新图像的锚点可以帮助我们使用Java中的平移变换执行旋转操作。锚点是任何特定图像的中心。

Algorithm to Rotate an Image by Using Java

The "AffineTransformOp" class is the simplest way to rotate an image file by using Java. A user can load the image data as Buffered Image and apply the rotate operation using anchor point to produce the new BufferedImage for the next step. It is always recommended that to use JPEG files for these operation with JDeli(Filetype with wide range).

通过使用Java程序旋转图像,程序员可以使用一些内置的方法,如BufferedImage类和Color c。

使用这个过程,我们需要将一张图像作为BufferedImage加载到Java中,然后使用相同的函数旋转图像,并将数据保存到一个新文件中。

现在,让我们讨论算法,以更广泛的方式理解上述操作 -

  • 步骤 1 − 图像读取和写入,并导入到表示目录路径的文件类。

  • Step 2 − Use IOException: handle errors.

  • Step 3 − For holding the particular image use the object called BufferedImage, a static method to save the data in RAM.

  • 第四步 - 使用ImageIO进行读写操作。

  • Step 5 − Use Graphics2D class, to render 2D shapes.

Syntax

Code declaration: public static Image rotate(Image image, double angle)

登录后复制

根据这个语法,理解的步骤如下:

  • Parameters −

    • Image − 执行旋转操作

    • 角度− 弧度旋转

  • 返回 − 旋转后的图像文件

以下过程可以使用Java程序来旋转图像文件 -

  • Step 1 − Load The Image File As BufferedImage In Java Environment To load an image file in Java −

BufferedImage image = ImageIO.read(new File("C:pathtoimage name.jpg"));

登录后复制

  • Step 2 − Rotate the image by 90 degree

  • To rotate an image file by 90 degree follow the below code −

final double rads = Math.toRadians(90);
final Rotate rotate = new Rotate(90);
BufferedImage rotatedImage = rotate.apply(image);

登录后复制

  • Step 3 − Save the image file

Use Java ImageIO
ImageIO.write(rotatedImage,"JPG",newFile("C:pathtorotatedImagename.jpg"));

登录后复制

Example

import java.awt.*;
import java.awt.image.BufferedImage;
public class Main {
public static Image rotate(Image image, double angle) {
BufferedImage bufImg = toBufferedImage(image);
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = bufImg.getWidth(), h = bufImg.getHeight();
int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
BufferedImage result = new BufferedImage(neww, newh, Transparency.TRANSLUCENT);
Graphics2D g = result.createGraphics();
g.translate((neww - w) / 2, (newh - h) / 2);
g.rotate(angle, w / 2, h / 2);
g.drawRenderedImage(bufImg, null);
g.dispose();
return result;
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
BufferedImage buff = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = buff.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return buff;
}
}

登录后复制

Rotation of an Image Using BufferedImage Function

By using a try-catch blocking method, we can handle the exceptions as they may disturb the flow of the code.

  • 文档类 - 文档类主要用于展示PDF文档。在这个特定的Java程序中,我们需要使用ie.documentobj函数创建一个文档类。这个函数用于打开和关闭PDF文件。

  • PDF Writer Class − The function supports the PDF, XML,RTF file generations to code the image file rotation. The directory function we use here is fileOutputStream() to handle a file for a java code.

  • 输出函数 − output.pdf 是一个函数类,表示使用Java代码执行操作后的输出。该函数帮助获取提供的输入的输出。

  • Functions −

    • image class.imgage.scaleToFit() - The function helps us to set up a size in the input file preset.

    • imageobj.setRotationDegrees() - the coder can use this to rotate the image in certain angle. It can be used as a parameter in the method we used.

    • documentobj.open() - 该函数帮助用户在操作时打开文件。

    • documentobj.close() - 使用此函数关闭一个.pdf文件。

Example

package JavaApplication29;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class JavaApplication29 {
public static void main(String[] args) {
try {
Document documentobj = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter.getInstance(documentobj, new FileOutputStream("output.pdf"));
documentobj.open();
Image imageobj = Image.getInstance("C:UserslenovoDesktopRDDLogo Org.jpg");
imageobj.scaleToFit(200f, 200f);
imageobj.setRotationDegrees(90);
documentobj.add(imageobj);
documentobj.close();
System.out.println("Task completed");
} catch (Exception e) {
System.out.println("Exception occurred");
}
}
}

登录后复制

Output

通过在程序中使用可能的方法进行编码,控制台显示尺寸和执行弹出窗口。旋转后保存了一张新的图像。

Java程序旋转图像

Conclusion

在本文中,上述讨论的过程有助于旋转图像文件。对于90度的旋转,程序需要设置一个新的图像,所有参数都需要被改变。由于锚点仍然位于图像的中心,并且顺时针和逆时针旋转的操作是相同的。

以上就是Java程序旋转图像的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论