光栅之外的 Java getSubimage()

标签 java bufferedimage raster

我正在尝试拍摄一张图像并将其存储在 16x16 子图像数组中。我使用的图像是 512x512 像素。但是,在循环迭代时,getSubimage() 因 Raster 异常而停止。

这是代码:

public class TileList extends JPanel {

  private static final int width = 16;          //width of a tile
  private static final int height = width;
  private int col = 1;
  private int row = 1;

  private BufferedImage image;
  File tilesetImage = new File("image.png");
  BufferedImage tileset[];

  public void loadAndSplitImage (File loadImage) {
    try{
        image = ImageIO.read(loadImage);
    }catch(Exception error) {
        System.out.println("Error: cannot read tileset image.");
    }// end try/catch
    col = image.getWidth()/width;
    row = image.getHeight()/height;
    tileset = new BufferedImage[col*row];
  }// end loadAndSplitImage

  public TileList() {
    loadAndSplitImage(tilesetImage);
    setLayout(new GridLayout(row,col,1,1));
    setBackground(Color.black);

    int x=0;
    int y=0;
    int q=0;                                    //keeps track of tile #
    for (int i = 0; i < row; i++) {

        for (int j = 0; j < col; j++) {
            JPanel panel = new JPanel();
            tileset[q] = new BufferedImage(width, height, image.getType());
            tileset[q] = image.getSubimage(x,y,x + width,y + height);
            panel.add(new JLabel(new ImageIcon(tileset[q])));
            add(panel);
            x += width;
            q++;
        }// end for loop
        y += height;
        x = 0;
    }// end for loop
  }// end constructor
}// end class

这是错误:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (x
+ width) is outside of Raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleav
edRaster.java:1245)
    at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1173)
    at TileList.<init>(TileList.java:59)
    at TileList.createAndShowGui(TileList.java:79)
    at TileList$1.run(TileList.java:88)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

最佳答案

您向 getSubimage 传递了错误的参数。文档说...

Parameters:
x - the X coordinate of the upper-left corner of the specified rectangular region
y - the Y coordinate of the upper-left corner of the specified rectangular region
w - the width of the specified rectangular region
h - the height of the specified rectangular region

您传入的是 x, y, x + width, y + width,这意味着如果 x = 256,width 实际上等于 256 + 16 = 272

所以你的新图像将是...x + width = 256 + 272 = 528,它比你的图像区域更宽。

您应该传递x、y、宽度、高度

tileset[q] = image.getSubimage(x, y, width, height);

关于光栅之外的 Java getSubimage(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54613196/

相关文章:

jsp - 将 BufferedImage 转换为 img 的问题

java - 从 Java 图像中检索像素数据

java - 我是否必须为连接查询创建单独的 @Entity 类才能提取结果? Jpa存储库

java - Spring Boot 和 Spring Security,无法在 AuthenticationEntryPoint 中使用自定义消息发送错误

java - 从SQL Server 2005升级到SQL Server 2008,JAVA应用程序会出现错误吗?

java - 在 Java 中重新绘制 BufferedImage 不会更改面板的内容

r - R中光栅和多边形的坐标引用系

r - Eloquent 地更改 R 中的许多栅格单元值

python - 使用 Python 将纬度、经度、值 CSV 转换为栅格 map

java - 在java应用程序中使用黑莓相机选择已存档的照片