当我尝试使用打开一个简单的smile.png图像时
package com.java3d.java3d.graphics;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Texture {
public static Render floor = loadBitMap("smile.png");
public Texture(){}
public static Render loadBitMap(String fileName) {
try {
BufferedImage image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource(fileName));
System.out.print(image==null);
int width = image.getWidth();
System.out.println(width);
int height = image.getHeight();
System.out.println(height);
System.out.println(image.getRGB(4, 4));
Render result = new Render(width, height);
image.getRGB(0, 0, width, height, result.pixels, 0, width);
return result;
} catch (Exception e) {
System.out.println("CRASH!");
throw new RuntimeException(e);
}
}
}
它返回每个像素为-1;是什么引起了这个问题?
这是图片:
最佳答案
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
public class QuickTest {
public static void main( String[] args ) throws Exception {
BufferedImage image = ImageIO.read(new URL(
"http://i.stack.imgur.com/BLRBU.png"));
System.out.println(image==null);
int width = image.getWidth();
int height = image.getHeight();
System.out.println(width + "x" + height);
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
System.out.print(image.getRGB(i, j) + "," );
}
System.out.println();
}
}
}
OP
false
8x8
-16711936,-16711936,-16711936,-16711936,-16711936,-16711936,-16711936,-16711936,
-16711936,-1,-1,-1,-1,-1,-1,-16711936,
-16711936,-1,-65536,-1,-65536,-1,-1,-16711936,
-16711936,-1,-1,-1,-1,-65536,-1,-16711936,
-16711936,-1,-1,-1,-1,-65536,-1,-16711936,
-16711936,-1,-65536,-1,-65536,-1,-1,-16711936,
-16711936,-1,-1,-1,-1,-1,-1,-16711936,
-16711936,-16711936,-16711936,-16711936,-16711936,-16711936,-16711936,-16711936,
关于java - 读取图像时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13540690/