Java未创建图像文件(使用ImageIO)

时间:2018-06-24 21:24:00

标签: java image matrix javax.imageio

我被分配了一个任务,我必须从辅助存储器读取两个图像,然后将它们存储在两个单独的矩阵中。然后,我必须将这些矩阵相乘,并将结果矩阵转换回图像,并将其存储在HDD中。这是代码:

package ISI;

import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import javax.imageio.ImageIO;
import javax.swing.*;

class ImageMultiplication {

  BufferedImage img1, img2;
  File f1, f2;
  int matrix1[][], matrix2[][], matrix3[][];
  int w,h;

  ImageMultiplication() { 
      img1 = img2 = null; 
      f1 = f2 = null;
      w = 500;
      h = 400;
  }

  void readImages() throws IOException {
      f1 = new File("image1.jpg");
      f2 = new File("image2.jpg");
      img1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      img2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      img1 = ImageIO.read(f1);
      img2 = ImageIO.read(f2);
      System.out.println("\nReading of images complete");
  }

  void convertToMatrix() {
      int [] array1 = new int[w*h];
      matrix1 = new int[h][w];
      int [] array2 = new int[w*h];
      matrix2 = new int[w][h];
      matrix3 = new int[h][h];
      try {
          img1.getRGB(0, 0, w, h, array1, 0, w);
          img2.getRGB(0, 0, w, h, array2, 0, w);

      }
      catch(Exception e) {
          System.out.println("\nInterrupted");
      }

      int count=0;

      for(int i=0;i<h;i++) {
          for(int j=0;j<w;j++) {
              if(count == array1.length)
                  break;
              matrix1[i][j] = array1[count];
              count++;
          }
      }

      count=0;

      for(int i=0;i<w;i++) {
          for(int j=0;j<h;j++) {
              if(count == array2.length)
                  break;
              matrix2[i][j]=array2[count];
              count++;
          }
      }

      int sum = 0, c, d, k;
      for (c = 0; c < h; c++) {
          for (d = 0; d < h; d++) {
              for (k = 0; k < w; k++) 
                  sum = sum + matrix1[c][k] * matrix2[k][d]; 
          matrix3[c][d] = sum;
          sum = 0;
          }
      }  

      /* Comment snippet 1
      for(int i = 0; i<h; i++) {
          for(int j = 0; j<h; j++)
              System.out.print(" "+matrix3[i][j]);
          System.out.println();
      }
      */        
  }

  void convertMatrixToImage() {
      BufferedImage image = new BufferedImage(w, h,  BufferedImage.TYPE_INT_RGB);
      try {
          for(int i=0; i<h; i++) {
              for(int j=0; j<h; j++) {
                  int a = matrix3[i][j];
                  Color newColor = new Color(a,a,a);
                  image.setRGB(j,i,newColor.getRGB());
              }
          }
          ImageIO.write(image, "jpg", new File("Output.jpg"));
      }
      catch(Exception e) {}
      System.out.println(image.toString());
      System.out.println("\nThe output image has been generated!");         
  }

  public static void main(String [] args) throws IOException {
      ImageMultiplication i = new ImageMultiplication();
      i.readImages();
      i.convertToMatrix();
      i.convertMatrixToImage();
  }

}

文件执行没有问题。

See

问题是,但是,没有在目录(void convertMatrixToImage())中创建或写入任何图像文件。如果取消注释(comment snippet 1,则会在控制台窗口上得到2D矩阵作为输出,其中每个索引都显示一个数字值,我假设该数字值是RGB像素值。但是,没有任何迹象表明创建任何图像文件。有人可以帮我吗?

注意:我尝试将 array 转换为 byte array ,然后写入图像文件,并且也尝试了其他方法,但是似乎没有任何效果。我什至在Windows上也尝试过,但是也有同样的问题。 Output.jpg 在哪里都无法创建/编写。

1 个答案:

答案 0 :(得分:2)

运行修改后的代码以打印Exception时,我得到...

java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green Blue
    at java.awt.Color.testColorValueRange(Color.java:310)
    at java.awt.Color.<init>(Color.java:395)
    at java.awt.Color.<init>(Color.java:369)
    at javaapplication194.ImageMultiplication.convertMatrixToImage(JavaApplication194.java:102)
    at javaapplication194.ImageMultiplication.main(JavaApplication194.java:118)

现在,老实说,我不知道这个“真正的”是什么意思,但是我知道它与“颜色”有关

所以我回头看了一下对话代码...

try {
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < h; j++) {
            int a = matrix3[i][j];
            Color newColor = new Color(a, a, a);
            image.setRGB(j, i, newColor.getRGB());
        }
    }
    ImageIO.write(image, "jpg", new File("Output.jpg"));
} catch (Exception e) {
    e.printStackTrace();
}

并指出...

int a = matrix3[i][j];
Color newColor = new Color(a, a, a);
image.setRGB(j, i, newColor.getRGB());

由于多种原因,这对我来说似乎很奇怪...

  1. 您使用getRGB将颜色打包为int
  2. 您尝试使用这种包装的int
  3. 制作新的颜色
  4. 您使用getRGB从基于压缩的int的颜色返回压缩的int

所有这些似乎都是错误的和不必要的,您已经有了一个压缩的int RGB值,为什么不直接使用

int a = matrix3[i][j];
//Color newColor = new Color(a, a, a);
image.setRGB(j, i, a);

添加此内容,错误消失并且创建了图像

相关问题