[英]Java - Why does ImageIO.read(filename) return null and the input image gets overwritten with 0 bytes?
I have several methods that manipulate a .jpg image: mirroring on x and y axis, tiling it, converting to ASCII text, and adjusting its brightness. 我有几种处理.jpg图像的方法:在x和y轴上镜像,平铺,转换为ASCII文本以及调整其亮度。 All the methods work properly except the brightness one. 除亮度之一外,所有方法均正常工作。 When I run the brightness method, there is a NullPointerException in readGrayscaleImage()
(see below - instructor written code). 当我运行亮度方法时, readGrayscaleImage()
有一个NullPointerException(请参见下文-教师编写的代码)。 The BufferedImage
is null, however it isn't when the method is called from any other of my methods (mirrors, tiling, ascii all read the file correctly). BufferedImage
为null,但是从我的任何其他方法(镜像,平铺,ascii都正确读取文件)调用该方法时不是这种情况。 Not only is the BufferedImage
null, the input image it is trying to read gets overwritten with 0 bytes and cannot be viewed in an image viewer, which would probably explain why the BufferedImage
is null. 不仅BufferedImage
null,而且它试图读取的输入图像也会被0字节覆盖,并且无法在图像查看器中查看,这可能可以解释为什么BufferedImage
为null。
Here is a working method that calls readGrayscaleImage()
: 这是一个调用readGrayscaleImage()
的工作方法:
public static void tileImage(int h, int v, String infile, String outfile) {
int[][] result = readGrayscaleImage(infile);
int[][] tileResult = new int[result.length * h][result[0].length * v];
for (int hh = 0; hh < h; hh++) {
for (int vv = 0; vv < v; vv++) {
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
tileResult[i + hh * result.length][j + vv * result[i].length] = result[i][j];
}
}
}
}
writeGrayscaleImage(outfile, tileResult);
}
Here is the brightness method that results in the problem: 这是导致问题的亮度方法:
public static void adjustBrightness(int amount, String infile, String outfile) {
int[][] result = readGrayscaleImage(infile); // NullPointerException trace points here
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
if (result[i][j] + amount > 255)
result[i][j] = 255;
else if (result[i][j] + amount < 0)
result[i][j] = 0;
else
result[i][j] += amount;
}
}
writeGrayscaleImage(outfile, result);
}
Here is the instructor-written code that reads a greyscale .jpg file and returns an array of integers: 这是由教师编写的代码,该代码读取灰度.jpg文件并返回整数数组:
public static int[][] readGrayscaleImage(String filename) {
int [][] result = null; //create the array
try {
File imageFile = new File(filename); //create the file
BufferedImage image = ImageIO.read(imageFile);
int height = image.getHeight(); // NullPointerException POINTS HERE - image IS NULL
int width = image.getWidth();
result = new int[height][width]; //read each pixel value
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = image.getRGB(x, y);
result[y][x] = rgb & 0xff;
}
}
}
catch (Exception ioe) {
System.err.println("Problems reading file named " + filename);
ioe.printStackTrace();
System.exit(-1);
}
return result; //once we're done filling it, return the new array
}
Here is the instructor written method to write a .jpg: 这是教师编写.jpg的书面方法:
public static void writeGrayscaleImage(String filename, int[][] array) {
int width = array[0].length;
int height = array.length;
try {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); //create the image
//set all its pixel values based on values in the input array
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = array[y][x];
rgb |= rgb << 8;
rgb |= rgb << 16;
image.setRGB(x, y, rgb);
}
}
//write the image to a file
File imageFile = new File(filename);
ImageIO.write(image, "jpg", imageFile);
}
catch (IOException ioe) {
System.err.println("Problems writing file named " + filename);
System.exit(-1);
}
}
(Wanted to comment but am not able to.) (想发表评论,但无法发表评论。)
BufferedImage image = ImageIO.read(filename);
Should this be 应该是
BufferedImage image = ImageIO.read(imageFile);
? ? I don't even see an override of read that takes a string. 我什至看不到带有字符串的read重写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.