简体   繁体   English

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

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

The aim of this little project is to break down an image (in this case a flag) into pieces like a jigsaw and store each piece in part of a 2D array. 这个小项目的目的是将图像(在这种情况下是一个标志)分解成像拼图一样的碎片,并将每个碎片存储在2D阵列的一部分中。 I then want to be able to view each piece individually so that I know it has worked. 然后,我希望能够逐个查看每件作品,以便知道它已经起作用。 I have created an object class which loads and stores the image. 我创建了一个加载和存储图像的对象类。 I created the object in my main class and then pass it to my splitImage method which divides the image into chunks and stores in the array. 我在主类中创建了对象,然后将其传递给我的splitImage方法,该方法将图像分为块并存储在数组中。 I would like to be able to view a section of the array to check that the splitImage method has worked correctly. 我希望能够查看数组的一部分以检查splitImage方法是否正常工作。 Long term however I do need to view the array as I will be determining the colour of the pixel in each image piece and counting how many of each colour is in the image. 从长远来看,我确实需要查看数组,因为我将确定每个图像中像素的颜色,并计算图像中每种颜色的数量。 When I run the current code I get the following in the console, 当我运行当前代码时,会在控制台中看到以下内容,

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 is the line - BufferedImage image = (BufferedImage)((Image) icon.getImage()); 28是这一行BufferedImage image = (BufferedImage)((Image) icon.getImage()); 59 is - ImageFrame.splitImage(imageSwing.label); 59是ImageFrame.splitImage(imageSwing.label);

I have played around with these for some time, changing the position trying other options and have been unsuccessful. 我玩这些游戏已经有一段时间了,尝试其他选择来改变位置,但都没有成功。 Help on this is much appreciated. 非常感谢您对此的帮助。 Code is below and thanks in advance. 代码在下面,在此先感谢。

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

Take a look at Java converting Image to BufferedImage 看一下Java将Image转换为BufferedImage
It provides a way to convert from Image to BufferedImage, which seems to be the problem. 它提供了一种从Image转换为BufferedImage的方法,这似乎是问题所在。

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

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