首頁 > Java > java教程 > 主體

Java程式旋轉影像

PHPz
發布: 2023-09-01 16:25:03
轉載
1221 人瀏覽過

一個影像檔案可以順時針或逆時針旋轉。要旋轉圖像,需要下載一個隨機的圖像檔案並將其保存在系統的任何資料夾中。此外,需要一個.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 anchorpoint to produce the exuffered 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:\path\to\image 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:\path\to\rotatedImagename.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:\Users\lenovo\Desktop\RDD\Logo 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");
      }
   }
}
登入後複製

OutputJava程式旋轉影像

#透過在程式中使用可能的方法進行編碼,控制台顯示尺寸並執行彈出視窗。旋轉後儲存了一張新的圖像。

###Conclusion### ###在本文中,上述討論的過程有助於旋轉圖像檔案。對於90度的旋轉,程式需要設定新的圖像,所有參數都需要被改變。由於錨點仍然位於影像的中心,且順時針和逆時針旋轉的操作是相同的。 ###

以上是Java程式旋轉影像的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!