1

当我尝试使用打开一个简单的 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;什么可能导致这个问题?这是图像: 我使用的图像

4

2 回答 2

2

我无法抗拒@Andrew 示例的这种变化。给定字节顺序ARGB,几乎可以看到绿色边框和红色特征。注意xy参数分别getRGB()代表columnrow

错误的
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();
        }
    }
}
于 2012-11-30T05:02:39.183 回答
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,
于 2012-11-24T11:34:20.550 回答