绘制图像

原文: https://docs.oracle.com/javase/tutorial/https://docs.oracle.com/javase/tutorial/images/drawimage.html

如您所知,Graphics.drawImage方法在特定位置绘制图像:

  1. boolean Graphics.drawImage(Image img,
  2. int x, int y,
  3. ImageObserver observer);

x,y位置指定图像左上角的位置。 observer参数通知应用程序对异步加载的映像的更新。 observer参数不经常直接使用, BufferedImage 类不需要,因此它通常为空。

所描述的方法仅解决了要绘制整个图像的情况,将图像像素映射到用户空间坐标 1:1。有时,应用程序需要绘制图像的一部分(子图像),或缩放图像以覆盖绘图表面的特定区域,或者在绘制之前变换或过滤图像。

drawImage()方法的重载执行这些操作。例如,drawImage()方法的以下重载使您可以绘制与当前可用的指定图像的指定区域一样多的数量,将其缩放以适合目标可绘制表面的指定区域:

  1. boolean Graphics.drawImage(Image img,
  2. int dstx1, int dsty1, int dstx2, int dsty2,
  3. int srcx1, int srcy1, int srcx2, int srcy2,
  4. ImageObserver observer);

src参数表示要复制和绘制的图像区域。 dst参数显示源区域要覆盖的目标区域。 dstx1, dsty1坐标定义绘制图像的位置。目标区域的宽度和高度尺寸通过以下表达式计算:(dstx2-dstx1), (dsty2-dsty1)。如果源和目标区域的维度不同,则 Java 2D API 将根据需要向上扩展或缩小。

以下代码示例将图像划分为四个象限,并将源图像的每个象限随机绘制到目标的不同象限。

<applet alt=”Divides an image into four quadrants and randomly draws each quadrant of the source image into a different quadrant of the destination.” archive=”examples/lib/JumbledImageApplet.jar” code=”JumbledImageApplet” height=”250” width=”400”><param name=”permissions” value=”sandbox”></applet>


Note: If you don’t see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.


该 applet 的完整代码位于 JumbledImageApplet.java 中。

此示例使用以下代码绘制混乱的duke_skateboard.jpg图像。它迭代源的四个子图像,依次绘制成随机选择的目标象限。

  1. /* divide the image 'bi' into four rectangular
  2. * areas and draw each of these areas in to a
  3. * different part of the image, so as to jumble
  4. * up the image. 'cells' is an array which has
  5. * been populated with values which redirect
  6. * drawing of one subarea to another subarea.
  7. */
  8. int cellWidth = bi.getWidth(null)/2;
  9. int cellHeight = bi.getHeight(null)/2;
  10. for (int x=0; x<2; x++) {
  11. int sx = x*cellWidth;
  12. for (int y=0; y<2; y++) {
  13. int sy = y*cellHeight;
  14. int cell = cells[x*2+y];
  15. int dx = (cell / 2) * cellWidth;
  16. int dy = (cell % 2) * cellHeight;
  17. g.drawImage(bi,
  18. dx, dy,
  19. x+cellWidth, dy+cellHeight,
  20. sx, sy,
  21. sx+cellWidth, sy+cellHeight,
  22. null);
  23. }
  24. }

过滤图像

除了复制和缩放图像外,Java 2D API 还可以过滤图像。过滤通过将算法应用于源图像的像素来绘制或生成新图像。可以使用以下方法应用图像过滤器:

  1. void Graphics2D.drawImage(BufferedImage img,
  2. BufferedImageOp op,
  3. int x, int y)

BufferedImageOp参数实现过滤器。以下小程序表示在文本顶部绘制的图像。拖动滑块可在图像中显示更多或更少的文本,并使图像或多或少透明。

<applet alt=”Represents an image drawn on top of text” archive=”examples/lib/SeeThroughImageApplet.jar” code=”SeeThroughImageApplet” height=”250” width=”400”><param name=”permissions” value=”sandbox”></applet>


Note: If you don’t see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.


以下代码显示了如何通过使用 alpha 通道操作BufferedImage对象并使用RescaleOp对象重新调整该 Alpha 通道来完成滤镜操作。 alpha 通道决定每个像素的半透明度。它还指定此图像覆盖的程度。

  1. /* Create an ARGB BufferedImage */
  2. BufferedImage img = ImageIO.read(imageSrc);
  3. int w = img.getWidth(null);
  4. int h = img.getHeight(null);
  5. BufferedImage bi = new
  6. BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  7. Graphics g = bi.getGraphics();
  8. g.drawImage(img, 0, 0, null);
  9. /*
  10. * Create a rescale filter op that makes the image
  11. * 50% opaque.
  12. */
  13. float[] scales = { 1f, 1f, 1f, 0.5f };
  14. float[] offsets = new float[4];
  15. RescaleOp rop = new RescaleOp(scales, offsets, null);
  16. /* Draw the image, applying the filter */
  17. g2d.drawImage(bi, rop, 0, 0);

SeeThroughImageApplet.java 中表示的完整示例包括使用滑块从最初的 50%调整透明度的代码。此示例还需要 duke_skateboard.jpg 图像。

RescaleOp对象只是可以创建的许多过滤器之一。 Java 2D API 有几个内置的过滤器,包括:

  • ConvolveOp。从源图像中的周围像素计算每个输出像素。它可用于模糊或锐化图像。
  • AffineTransformOp。此滤镜通过对像素位置应用变换,将源中的像素映射到目标中的不同位置。
  • LookupOp。此过滤器使用应用程序提供的查找表来重新映射像素颜色。
  • RescaleOp。此滤镜将颜色乘以某个因子。可用于使图像变亮或变暗,增加或减少其不透明度等。

以下示例使用每个描述的过滤器以及缩放:

<applet alt=”Demonstrates filters and scaling” archive=”examples/lib/ImageDrawingApplet.jar” code=”ImageDrawingApplet” height=”250” width=”400”><param name=”permissions” value=”sandbox”></applet>


Note: If you don’t see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.


这个小程序的完整代码在 ImageDrawingApplet.java 中,这个小程序需要 bld.jpg 图像。

使用下拉菜单选择图像缩放或过滤操作。