java - Java 不创建图像文件(使用 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();
}
}
该文件执行没有问题。
但是,问题是在目录 ( void convertMatrixToImage()
) 中没有创建或写入图像文件。如果我取消注释comment snippet 1
(但是没有任何迹象表明曾经创建过任何图像文件。有人可以帮我吗?
注意:我尝试将数组转换为字节数组,然后写入图像文件,我也尝试了其他方法,但似乎没有任何效果。我什至在 Windows 上尝试过,但它也有同样的问题。没有任何地方正在创建/写入Output.jpg 。
解决方案
当我运行您修改后的代码来打印时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());
由于多种原因,这对我来说似乎很奇怪......
- 您用于
getRGB
获取颜色作为打包int
值 - 你试着用这个包装做一个新的颜色
int
- 您用于从基于打包的颜色
getRGB
返回打包的int
int
所有这些似乎都是错误和不必要的,你已经有一个打包的int
RGB 值,为什么不直接使用
int a = matrix3[i][j];
//Color newColor = new Color(a, a, a);
image.setRGB(j, i, a);
添加这个,错误消失并创建图像
推荐阅读
- c++ - 从 cpp 中的函数返回的 char*
- delphi - 去掉IDE代码编辑器中间的竖白线
- javascript - 如何解决“允许属性将优先于allowfullscreen”警告?
- ruby-on-rails - 当我在终端上运行 rails c 时,我得到了 pry 错误
- visual-studio - Visual Studio 未找到“vulkan-1.lib”
- javascript - 为什么我的 for 循环只返回 1 个值
- python - python:锁定但基于参数
- java - Java GUI - 缓冲阅读器仅在我按下按钮时读取最后一行
- java - 如何将按钮添加到单列 TableView 中的单行?
- python - PySpark Group 并逐行应用 UDF 操作