搜尋
首頁Javajava教程Java程式旋轉影像

Java程式旋轉影像

Sep 01, 2023 pm 04:25 PM
旋轉影像java程式

一個影像檔案可以順時針或逆時針旋轉。要旋轉圖像,需要下載一個隨機的圖像檔案並將其保存在系統的任何資料夾中。此外,需要一個.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。如有侵權,請聯絡admin@php.cn刪除
JVM如何促進Java的'寫作一次,在任何地方運行”(WORA)功能?JVM如何促進Java的'寫作一次,在任何地方運行”(WORA)功能?May 02, 2025 am 12:25 AM

JVM通過字節碼解釋、平台無關的API和動態類加載實現Java的WORA特性:1.字節碼被解釋為機器碼,確保跨平台運行;2.標準API抽像操作系統差異;3.類在運行時動態加載,保證一致性。

Java的較新版本如何解決平台特定問題?Java的較新版本如何解決平台特定問題?May 02, 2025 am 12:18 AM

Java的最新版本通過JVM優化、標準庫改進和第三方庫支持有效解決平台特定問題。 1)JVM優化,如Java11的ZGC提升了垃圾回收性能。 2)標準庫改進,如Java9的模塊系統減少平台相關問題。 3)第三方庫提供平台優化版本,如OpenCV。

說明JVM執行的字節碼驗證的過程。說明JVM執行的字節碼驗證的過程。May 02, 2025 am 12:18 AM

JVM的字節碼驗證過程包括四個關鍵步驟:1)檢查類文件格式是否符合規範,2)驗證字節碼指令的有效性和正確性,3)進行數據流分析確保類型安全,4)平衡驗證的徹底性與性能。通過這些步驟,JVM確保只有安全、正確的字節碼被執行,從而保護程序的完整性和安全性。

平台獨立性如何簡化Java應用程序的部署?平台獨立性如何簡化Java應用程序的部署?May 02, 2025 am 12:15 AM

Java'splatFormIndepentEncealLowsApplicationStorunonAnyOperatingsystemwithajvm.1)singleCodeBase:writeandeandcompileonceforallplatforms.2)easileupdates:updatebybytecodeforsimultanane deployment.3)testOnOneOnePlatForforurouniverSalpeforuluniverSalpehavior formafforulululyiversalivernave.444.44.444

Java的平台獨立性如何隨著時間的流逝而發展?Java的平台獨立性如何隨著時間的流逝而發展?May 02, 2025 am 12:12 AM

Java的平台獨立性通過JVM、JIT編譯、標準化、泛型、lambda表達式和ProjectPanama等技術不斷增強。自1990年代以來,Java從基本的JVM演進到高性能的現代JVM,確保了代碼在不同平台的一致性和高效性。

在Java應用程序中緩解平台特定問題的策略是什麼?在Java應用程序中緩解平台特定問題的策略是什麼?May 01, 2025 am 12:20 AM

Java如何緩解平台特定的問題? Java通過JVM和標準庫來實現平台無關性。 1)使用字節碼和JVM抽像操作系統差異;2)標準庫提供跨平台API,如Paths類處理文件路徑,Charset類處理字符編碼;3)實際項目中使用配置文件和多平台測試來優化和調試。

Java的平台獨立性與微服務體系結構之間有什麼關係?Java的平台獨立性與微服務體系結構之間有什麼關係?May 01, 2025 am 12:16 AM

java'splatformentenceenhancesenhancesmicroservicesharchitecture byferingDeploymentFlexible,一致性,可伸縮性和便攜性。 1)DeploymentFlexibilityAllowsibilityAllowsOllowsOllowSorlowsOllowsOllowsOllowSeStorunonAnyPlatformwithajvM.2)penterencyCrossServAccAcrossServAcrossServiCessImplifififiesDeevelopmentandeDe

GRAALVM與Java的平台獨立目標有何關係?GRAALVM與Java的平台獨立目標有何關係?May 01, 2025 am 12:14 AM

GraalVM通過三種方式增強了Java的平台獨立性:1.跨語言互操作,允許Java與其他語言無縫互操作;2.獨立的運行時環境,通過GraalVMNativeImage將Java程序編譯成本地可執行文件;3.性能優化,Graal編譯器生成高效的機器碼,提升Java程序的性能和一致性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。