更新時間:2022-12-14 11:41:21 來源:動力節點 瀏覽1494次
Java實現一個特定類型的對象稱為BufferedImage圖像在Java中。可以從幾個不同的圖像讀取BufferedImage類型(即BMP HEIC,等等)。不是所有這些都是由ImageIO本身,但也有插件擴展ImageIO例如Apache成像和JDeli和其他庫。
在Java本身,所有各種圖像類型隱藏的復雜性,我們只有在BufferedImage工作。Java提供了直接訪問圖像像素和顏色信息,并允許轉換和圖像處理。
1. io .讀寫文件:一個圖像文件,我們必須導入文件類。這個類代表文件和目錄路徑名。
2. io .IOException:處理錯誤,我們使用IOException類。
3. java.awt.image。BufferedImage:保存圖像,我們創建BufferedImage對象;我們使用BufferedImage類。該對象用于存儲在RAM中。
4. javax.imageio。ImageIO: p
// Java program to demonstrate read and write of image
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MyImage {
public static void main(String args[])
throws IOException
{
// width of the image
int width = 963;
// height of the image
int height = 640;
// For storing image in RAM
BufferedImage image = null;
// READ IMAGE
try {
File input_file = new File(
"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
// image file path create an object of
// BufferedImage type and pass as parameter the
// width, height and image int
// type. TYPE_INT_ARGB means that we are
// representing the Alpha , Red, Green and Blue
// component of the image pixel using 8 bit
// integer value.
image = new BufferedImage(
width, height, BufferedImage.TYPE_INT_ARGB);
// Reading input file
image = ImageIO.read(input_file);
System.out.println("Reading complete.");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
// WRITE IMAGE
try {
// Output file path
File output_file = new File(
"C:/Users/hp/Desktop/Image Processing in Java/gfg.png");
// Writing to file taking type and path as
ImageIO.write(image, "png", output_file);
System.out.println("Writing complete.");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
} // main() ends here
} // class ends here
輸出
注意:這段代碼不會在網上IDE運行,因為它需要一個磁盤上的形象。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習