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
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 に答える
2

@Andrewの例のこのバリエーションに抵抗できませんでした。バイトオーダーARGBを考えると、緑色の境界線と赤色の機能がほとんどわかります。xパラメータとyパラメータは、それぞれcolumnrowgetRGB()を表すことに注意してください。

間違い
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 に答える