繁体   English   中英

用Java将图像对象拆分为2D数组

[英]Splitting an Image Object into a 2D Array in Java

这个小项目的目的是将图像(在这种情况下是一个标志)分解成像拼图一样的碎片,并将每个碎片存储在2D阵列的一部分中。 然后,我希望能够逐个查看每件作品,以便知道它已经起作用。 我创建了一个加载和存储图像的对象类。 我在主类中创建了对象,然后将其传递给我的splitImage方法,该方法将图像分为块并存储在数组中。 我希望能够查看数组的一部分以检查splitImage方法是否正常工作。 从长远来看,我确实需要查看数组,因为我将确定每个图像中像素的颜色,并计算图像中每种颜色的数量。 当我运行当前代码时,会在控制台中看到以下内容,

Exception in thread "main" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
    at sample.ImageFrame.splitImage(ImageFrame.java:28)
    at sample.ImageFrame.main(ImageFrame.java:59)

28是这一行BufferedImage image = (BufferedImage)((Image) icon.getImage()); 59是ImageFrame.splitImage(imageSwing.label);

我玩这些游戏已经有一段时间了,尝试其他选择来改变位置,但都没有成功。 非常感谢您对此的帮助。 代码在下面,在此先感谢。

public class ImageSwing extends JFrame
{
   public JLabel label;

   public ImageSwing()
   {
      super("Test Image Array");
      setLayout(new FlowLayout());

      Icon flag = new ImageIcon(getClass().getResource("Italy_flag.jpg"));
      label = new JLabel(flag);
      label.setToolTipText("Italian Flag");
      add(label);
   }//main
}//class

public class ImageFrame
{
   //public ImageSwing imageSwing = new ImageSwing();
   //ImageSwing imageSwing = new ImageSwing();


   public static BufferedImage splitImage(JLabel i) throws IOException
   {
      //Holds the dimensions of image
      int rows = 4;
      int cols = 4;

      ImageIcon icon = (ImageIcon)i.getIcon();
      BufferedImage image = (BufferedImage)((Image) icon.getImage());

      int chunks = rows * cols;  //Total amount of image pieces

      int partWidth = i.getWidth() / cols; // determines the piece width
      int partHeight = i.getHeight() / rows; //determines the piece height
      int count = 0;

      BufferedImage[][] flagArray = new BufferedImage[rows][cols]; //2D Array to hold each image piece
      for (int x = 0; x < rows; x++)
      {
         for (int y = 0; y < cols; y++)
         {
            //Initialize the image array with image chunks
            flagArray[x][y] = new BufferedImage(partWidth, partHeight, image.getType());

            // draws the image chunk
            Graphics2D gr = flagArray[x][y].createGraphics();
            gr.drawImage(image, 0, 0, partWidth, partHeight, partWidth * y, partHeight * x, partWidth * y + partWidth, partHeight * x + partHeight, null);
            gr.dispose();
         }
      }
         return flagArray[rows][cols];
   }
   public static void main(String[] args)
   {

      ImageSwing imageSwing = new ImageSwing();

      try
      {
         ImageFrame.splitImage(imageSwing.label);
      } catch (IOException e)
      {
         e.printStackTrace();
      }

      imageSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      imageSwing.setSize(260,180);
      imageSwing.setVisible(true);



   }//main
}//class

看一下Java将Image转换为BufferedImage
它提供了一种从Image转换为BufferedImage的方法,这似乎是问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM