Java BufferedImage 类

Java BufferedImage 类是 Image 类的子类。它用于处理和操作图像数据。BufferedImage 由图像数据的 ColorModel 组成。所有 BufferedImage 对象的左上角坐标均为 (0, 0)。

构造函数

此类支持三种类型的构造函数。

第一个构造函数使用指定的 ColorModel 和 Raster 构造一个新的 BufferedImage

BufferedImage(ColorModel cm, WritableRaster raster,
boolean isRasterPremultiplied, Hashtable<?,?> properties)

第二个构造函数构造一个预定义图像类型之一的 BufferedImage

BufferedImage(int width, int height, int imageType)

第三个构造函数构造一个预定义图像类型之一的 BufferedImage:TYPE_BYTE_BINARY或 TYPE_BYTE_INDEXED。

BufferedImage(int width, int height, int imageType, IndexColorModel cm)

Sr.No 方法 &描述
1

copyData(WritableRaster outRaster)

计算 BufferedImage 的任意矩形区域并将其复制到指定的 WritableRaster 中。

2

getColorModel()

返回图像的 ColorModel 类对象。

3

getData()

将图像作为一个大图块返回。

4

getData(Rectangle rect)

它计算并返回 BufferedImage 的任意区域。

5

getGraphics()

此方法返回 Graphics2D,保留向后兼容性。

6

getHeight()

它返回 BufferedImage 的高度。

7

getMinX()

它返回此 BufferedImage 的最小 x 坐标。

8

getMinY()

它返回此 BufferedImage 的最小 y 坐标。

9

getRGB(int x, int y)

它返回默认 RGB 颜色模型 (TYPE_INT_ARGB) 和默认 sRGB 颜色空间中的整数像素。

10

getType()

它返回图像类型。

示例

以下示例演示了如何使用 java BufferedImage 类,该类使用 Graphics Object 在屏幕上绘制一些文本 −

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JPanel {

   public void paint(Graphics g) {
      Image img = createImageWithText();
      g.drawImage(img, 20,20,this);
   }

   private Image createImageWithText() {
      BufferedImage bufferedImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
      Graphics g = bufferedImage.getGraphics();

      g.drawString("www.tutorialspoint.com", 20,20);
      g.drawString("www.tutorialspoint.com", 20,40);
      g.drawString("www.tutorialspoint.com", 20,60);
      g.drawString("www.tutorialspoint.com", 20,80);
      g.drawString("www.tutorialspoint.com", 20,100);
      
      return bufferedImage;
   }
   
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.getContentPane().add(new Test());

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(200, 200);
      frame.setVisible(true);
   }
}

输出

执行给定的代码时,将看到以下输出 −

Java 缓冲图像教程