读取图像时出错

时间:2012-11-24 11:19:37

标签: java image javax.imageio

当我尝试使用

打开一个简单的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;什么可能导致这个问题? 这是图像: The Image I use

2 个答案:

答案 0 :(得分:2)

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,

答案 1 :(得分:2)

我无法抗拒@Andrew的例子。给定字节顺序ARGB,人们几乎可以看到绿色边框和红色特征。请注意,getRGB() x y 参数分别代表

false
8x8
FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 
FF00FF00 FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FF00FF00 
FF00FF00 FFFFFFFF FFFF0000 FFFFFFFF FFFFFFFF FFFF0000 FFFFFFFF FF00FF00 
FF00FF00 FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FF00FF00 
FF00FF00 FFFFFFFF FFFF0000 FFFFFFFF FFFFFFFF FFFF0000 FFFFFFFF FF00FF00 
FF00FF00 FFFFFFFF FFFFFFFF FFFF0000 FFFF0000 FFFFFFFF FFFFFFFF FF00FF00 
FF00FF00 FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FF00FF00 
FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 FF00FF00 

代码:

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 row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                System.out.printf("%04X ", image.getRGB(col, row));
            }
            System.out.println();
        }
    }
}