BufferedImage像素值问题

时间:2012-01-25 13:54:16

标签: java colors pixel bufferedimage

我们尝试使用getRGB(x,y)获取缓冲图像中像素的rgb值。这是我们的代码:

public Color getColor(Point p, BufferedImage image){
    int rgb = image.getRGB(p.x,p.y);
    Color c = new Color(rgb);
    return c;
}

但我们得到的所有c都是(255,0,0),当我们检查RGB的值时,我们得到-65536。我们环顾四周,看到了许多有点变化的解决方案,但没有一个有效。 http://imgur.com/WMfeU是框架的一个示例。我们点击绿色。有什么想法吗?

private void initFrameGrabber(String videoDevice,int inWidth,int inHeight,int channel,             int videoStandard,int compressionQuality)抛出V4L4JException {         videoDev = new VideoDevice(videoDevice);

    DeviceInfo deviceInfo = videoDev.getDeviceInfo();

    if (deviceInfo.getFormatList().getNativeFormats().isEmpty()) {
      throw new ImageFormatException("Unable to detect any native formats for the device!");
    }
    ImageFormat imageFormat = deviceInfo.getFormatList().getNativeFormat(0);

    frameGrabber = videoDev.getJPEGFrameGrabber(inWidth, inHeight, channel, videoStandard,
            compressionQuality, imageFormat);

    frameGrabber.setCaptureCallback(new CaptureCallback() {
        public void exceptionReceived(V4L4JException e) {
            System.err.println("Unable to capture frame:");
            e.printStackTrace();
        }

        public void nextFrame(VideoFrame frame) {
            long before = System.currentTimeMillis();
            frameImage = frame.getBufferedImage();
            frame.recycle();
            processAndUpdateImage(frameImage, before);
        }
    });

    frameGrabber.startCapture();

    width = frameGrabber.getWidth();
    height = frameGrabber.getHeight();
}

1 个答案:

答案 0 :(得分:1)

我没有真正看到链接图像中的问题。 RGB值小于0,但形成有效颜色。

截图

Image Test

来源

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;

class ImageTest {

    public static int getRGB(int x, int y, BufferedImage image) {
        return image.getRGB(x,y);
    }

    public static Color getColor(int x, int y, BufferedImage image) {
        int rgb = image.getRGB(x,y);
        Color c = new Color(rgb);
        return c;
    }

    public static void main(String[] args) throws Exception {
        BufferedImage bi = ImageIO.read(
            new URL("http://i.imgur.com/WMfeU.png"));
        int w = bi.getWidth();
        int h = bi.getHeight();

        final BufferedImage bi2 = new BufferedImage(
            w,
            h,
            BufferedImage.TYPE_INT_RGB);

        for (int x=0; x<w; x++) {
            for (int y=0; y<h; y++) {
                int rgb = getRGB(x,y,bi);
                if (x%20==0 && y%20==0) {
                    System.out.println(getColor(x,y,bi));
                }
                bi2.setRGB(x,y,rgb);
            }
        }

        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(null,
                    new JLabel(new ImageIcon(bi2)));
            }
        });

    }
}

输出

java.awt.Color[r=115,g=148,b=77]
java.awt.Color[r=110,g=146,b=74]
java.awt.Color[r=111,g=147,b=77]
java.awt.Color[r=106,g=142,b=72]
java.awt.Color[r=106,g=141,b=73]
java.awt.Color[r=107,g=139,b=74]
java.awt.Color[r=75,g=95,b=46]
java.awt.Color[r=72,g=60,b=46]
java.awt.Color[r=108,g=92,b=79]
java.awt.Color[r=56,g=47,b=40]
java.awt.Color[r=32,g=28,b=27]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=104,g=79,b=59]
java.awt.Color[r=122,g=91,b=62]
java.awt.Color[r=60,g=61,b=47]
java.awt.Color[r=61,g=77,b=40]
java.awt.Color[r=103,g=134,b=66]
java.awt.Color[r=110,g=137,b=68]
java.awt.Color[r=111,g=138,b=69]
java.awt.Color[r=110,g=141,b=73]
java.awt.Color[r=114,g=146,b=73]
java.awt.Color[r=116,g=148,b=75]
java.awt.Color[r=115,g=147,b=74]
java.awt.Color[r=113,g=145,b=72]
java.awt.Color[r=113,g=147,b=73]
java.awt.Color[r=111,g=144,b=73]
java.awt.Color[r=114,g=147,b=76]
java.awt.Color[r=107,g=140,b=71]
java.awt.Color[r=110,g=142,b=75]
java.awt.Color[r=108,g=138,b=74]
java.awt.Color[r=73,g=92,b=46]
java.awt.Color[r=71,g=61,b=49]
java.awt.Color[r=111,g=93,b=81]
java.awt.Color[r=31,g=31,b=29]
java.awt.Color[r=36,g=36,b=36]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=103,g=78,b=56]
java.awt.Color[r=84,g=53,b=32]
java.awt.Color[r=58,g=58,b=46]
java.awt.Color[r=69,g=80,b=48]
java.awt.Color[r=106,g=136,b=66]
java.awt.Color[r=108,g=138,b=68]
java.awt.Color[r=106,g=136,b=66]
java.awt.Color[r=109,g=139,b=67]
java.awt.Color[r=117,g=147,b=75]
java.awt.Color[r=114,g=146,b=73]
java.awt.Color[r=113,g=145,b=72]
java.awt.Color[r=116,g=150,b=76]
java.awt.Color[r=112,g=146,b=72]
java.awt.Color[r=112,g=145,b=74]
java.awt.Color[r=111,g=144,b=73]
java.awt.Color[r=111,g=144,b=75]
java.awt.Color[r=109,g=142,b=73]
java.awt.Color[r=107,g=139,b=74]
java.awt.Color[r=75,g=94,b=48]
java.awt.Color[r=70,g=57,b=48]
java.awt.Color[r=96,g=79,b=69]
java.awt.Color[r=63,g=54,b=45]
java.awt.Color[r=61,g=52,b=45]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=100,g=79,b=62]
java.awt.Color[r=125,g=88,b=69]
java.awt.Color[r=59,g=59,b=47]
java.awt.Color[r=69,g=80,b=50]
java.awt.Color[r=107,g=137,b=67]
java.awt.Color[r=107,g=137,b=67]
java.awt.Color[r=107,g=134,b=63]
java.awt.Color[r=110,g=140,b=68]
java.awt.Color[r=114,g=144,b=70]
java.awt.Color[r=119,g=149,b=75]
java.awt.Color[r=113,g=145,b=72]
java.awt.Color[r=115,g=147,b=74]
java.awt.Color[r=112,g=144,b=71]
java.awt.Color[r=112,g=146,b=72]
java.awt.Color[r=111,g=144,b=73]
java.awt.Color[r=108,g=141,b=72]
java.awt.Color[r=111,g=142,b=74]
java.awt.Color[r=113,g=144,b=77]
java.awt.Color[r=73,g=92,b=46]
java.awt.Color[r=68,g=61,b=45]
java.awt.Color[r=180,g=153,b=136]
java.awt.Color[r=61,g=52,b=45]
java.awt.Color[r=59,g=52,b=44]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=96,g=75,b=54]
java.awt.Color[r=129,g=90,b=59]
java.awt.Color[r=63,g=63,b=51]
java.awt.Color[r=59,g=68,b=39]
java.awt.Color[r=102,g=132,b=62]
java.awt.Color[r=107,g=137,b=65]
java.awt.Color[r=107,g=137,b=65]
java.awt.Color[r=111,g=141,b=69]
java.awt.Color[r=115,g=145,b=73]
java.awt.Color[r=114,g=144,b=70]
java.awt.Color[r=112,g=144,b=71]
java.awt.Color[r=115,g=147,b=74]
java.awt.Color[r=113,g=145,b=72]
java.awt.Color[r=111,g=145,b=71]
java.awt.Color[r=110,g=143,b=72]
java.awt.Color[r=110,g=143,b=74]
java.awt.Color[r=110,g=143,b=74]
java.awt.Color[r=106,g=137,b=70]
java.awt.Color[r=76,g=89,b=45]
java.awt.Color[r=66,g=60,b=46]
java.awt.Color[r=104,g=82,b=71]
java.awt.Color[r=69,g=59,b=50]
java.awt.Color[r=70,g=60,b=51]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=102,g=77,b=55]
java.awt.Color[r=132,g=93,b=62]
java.awt.Color[r=52,g=48,b=36]
java.awt.Color[r=58,g=63,b=40]
java.awt.Color[r=103,g=130,b=61]
java.awt.Color[r=104,g=134,b=62]
java.awt.Color[r=108,g=135,b=64]
java.awt.Color[r=114,g=141,b=70]
java.awt.Color[r=114,g=144,b=72]
java.awt.Color[r=119,g=149,b=75]
java.awt.Color[r=115,g=145,b=73]
java.awt.Color[r=121,g=153,b=80]
java.awt.Color[r=111,g=143,b=70]
java.awt.Color[r=115,g=147,b=74]
java.awt.Color[r=116,g=148,b=75]
java.awt.Color[r=107,g=138,b=68]
java.awt.Color[r=107,g=138,b=70]
java.awt.Color[r=107,g=136,b=69]
java.awt.Color[r=73,g=89,b=44]
java.awt.Color[r=64,g=62,b=50]
java.awt.Color[r=83,g=65,b=53]
java.awt.Color[r=80,g=66,b=55]
java.awt.Color[r=73,g=61,b=49]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=103,g=79,b=55]
java.awt.Color[r=109,g=68,b=40]
java.awt.Color[r=69,g=66,b=51]
java.awt.Color[r=59,g=60,b=42]
java.awt.Color[r=101,g=131,b=61]
java.awt.Color[r=105,g=132,b=61]
java.awt.Color[r=106,g=133,b=62]
java.awt.Color[r=107,g=137,b=67]
java.awt.Color[r=115,g=143,b=69]
java.awt.Color[r=114,g=142,b=68]
java.awt.Color[r=115,g=143,b=69]
java.awt.Color[r=114,g=146,b=73]
java.awt.Color[r=110,g=142,b=69]
java.awt.Color[r=111,g=143,b=70]
java.awt.Color[r=109,g=141,b=68]
java.awt.Color[r=110,g=141,b=71]
java.awt.Color[r=107,g=138,b=70]
java.awt.Color[r=109,g=135,b=70]
java.awt.Color[r=75,g=86,b=43]
java.awt.Color[r=65,g=62,b=53]
java.awt.Color[r=78,g=59,b=42]
java.awt.Color[r=79,g=65,b=52]
java.awt.Color[r=76,g=64,b=52]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=105,g=79,b=54]
java.awt.Color[r=116,g=72,b=45]
java.awt.Color[r=84,g=72,b=58]
java.awt.Color[r=57,g=58,b=40]
java.awt.Color[r=97,g=126,b=59]
java.awt.Color[r=98,g=128,b=56]
java.awt.Color[r=103,g=133,b=61]
java.awt.Color[r=105,g=132,b=61]
java.awt.Color[r=108,g=136,b=62]
java.awt.Color[r=112,g=142,b=68]
java.awt.Color[r=109,g=139,b=67]
java.awt.Color[r=113,g=143,b=71]
java.awt.Color[r=112,g=142,b=70]
java.awt.Color[r=110,g=142,b=69]
java.awt.Color[r=109,g=141,b=68]
java.awt.Color[r=107,g=138,b=68]
java.awt.Color[r=104,g=135,b=67]
java.awt.Color[r=99,g=131,b=64]
java.awt.Color[r=77,g=85,b=46]
java.awt.Color[r=64,g=57,b=51]
java.awt.Color[r=83,g=66,b=46]
java.awt.Color[r=80,g=66,b=55]
java.awt.Color[r=76,g=62,b=49]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=106,g=82,b=58]
java.awt.Color[r=125,g=87,b=64]
java.awt.Color[r=106,g=88,b=74]
java.awt.Color[r=59,g=60,b=46]
java.awt.Color[r=92,g=123,b=55]
java.awt.Color[r=97,g=127,b=57]
java.awt.Color[r=101,g=128,b=59]
java.awt.Color[r=107,g=134,b=63]
java.awt.Color[r=109,g=136,b=65]
java.awt.Color[r=110,g=137,b=66]
java.awt.Color[r=105,g=135,b=63]
java.awt.Color[r=104,g=134,b=64]
java.awt.Color[r=106,g=138,b=65]
java.awt.Color[r=107,g=139,b=66]
java.awt.Color[r=107,g=138,b=68]
java.awt.Color[r=106,g=137,b=67]
java.awt.Color[r=105,g=136,b=68]
java.awt.Color[r=97,g=128,b=61]
java.awt.Color[r=73,g=80,b=39]
java.awt.Color[r=71,g=68,b=59]
java.awt.Color[r=84,g=67,b=57]
java.awt.Color[r=80,g=67,b=50]
java.awt.Color[r=82,g=69,b=53]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=106,g=81,b=59]
java.awt.Color[r=112,g=78,b=53]
java.awt.Color[r=116,g=94,b=71]
java.awt.Color[r=59,g=57,b=45]
java.awt.Color[r=88,g=117,b=51]
java.awt.Color[r=88,g=156,b=69]
java.awt.Color[r=95,g=127,b=54]
java.awt.Color[r=100,g=130,b=58]
java.awt.Color[r=102,g=132,b=60]
java.awt.Color[r=103,g=133,b=61]
java.awt.Color[r=92,g=116,b=54]
java.awt.Color[r=123,g=101,b=87]
java.awt.Color[r=65,g=67,b=53]
java.awt.Color[r=101,g=134,b=65]
java.awt.Color[r=97,g=128,b=58]
java.awt.Color[r=101,g=134,b=65]
java.awt.Color[r=100,g=131,b=63]
java.awt.Color[r=98,g=129,b=62]
java.awt.Color[r=69,g=72,b=41]
java.awt.Color[r=88,g=76,b=64]
java.awt.Color[r=89,g=72,b=56]
java.awt.Color[r=83,g=70,b=53]
java.awt.Color[r=82,g=68,b=55]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=103,g=78,b=56]
java.awt.Color[r=113,g=79,b=52]
java.awt.Color[r=123,g=93,b=67]
java.awt.Color[r=57,g=57,b=47]
java.awt.Color[r=48,g=145,b=50]
java.awt.Color[r=55,g=116,b=100]
java.awt.Color[r=87,g=124,b=55]
java.awt.Color[r=96,g=128,b=55]
java.awt.Color[r=104,g=134,b=62]
java.awt.Color[r=105,g=135,b=63]
java.awt.Color[r=94,g=104,b=51]
java.awt.Color[r=239,g=210,b=168]
java.awt.Color[r=74,g=63,b=45]
java.awt.Color[r=97,g=132,b=66]
java.awt.Color[r=97,g=130,b=61]
java.awt.Color[r=95,g=131,b=61]
java.awt.Color[r=91,g=126,b=58]
java.awt.Color[r=91,g=117,b=54]
java.awt.Color[r=56,g=54,b=29]
java.awt.Color[r=106,g=94,b=80]
java.awt.Color[r=81,g=65,b=50]
java.awt.Color[r=83,g=65,b=51]
java.awt.Color[r=79,g=66,b=50]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=103,g=79,b=55]
java.awt.Color[r=104,g=80,b=56]
java.awt.Color[r=99,g=79,b=55]
java.awt.Color[r=47,g=49,b=38]
java.awt.Color[r=70,g=109,b=46]
java.awt.Color[r=81,g=113,b=50]
java.awt.Color[r=80,g=111,b=44]
java.awt.Color[r=97,g=126,b=59]
java.awt.Color[r=101,g=131,b=59]
java.awt.Color[r=105,g=135,b=63]
java.awt.Color[r=105,g=126,b=59]
java.awt.Color[r=39,g=17,b=3]
java.awt.Color[r=140,g=127,b=93]
java.awt.Color[r=100,g=133,b=64]
java.awt.Color[r=88,g=125,b=56]
java.awt.Color[r=81,g=120,b=57]
java.awt.Color[r=77,g=119,b=55]
java.awt.Color[r=73,g=98,b=41]
java.awt.Color[r=53,g=52,b=34]
java.awt.Color[r=89,g=67,b=56]
java.awt.Color[r=84,g=67,b=51]
java.awt.Color[r=84,g=67,b=51]
java.awt.Color[r=77,g=64,b=48]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=110,g=86,b=62]
java.awt.Color[r=106,g=82,b=56]
java.awt.Color[r=106,g=82,b=58]
java.awt.Color[r=43,g=42,b=40]
java.awt.Color[r=53,g=53,b=41]
java.awt.Color[r=54,g=56,b=45]
java.awt.Color[r=53,g=53,b=41]
java.awt.Color[r=53,g=64,b=32]
java.awt.Color[r=84,g=101,b=47]
java.awt.Color[r=92,g=109,b=54]
java.awt.Color[r=89,g=109,b=50]
java.awt.Color[r=93,g=115,b=53]
java.awt.Color[r=94,g=114,b=53]
java.awt.Color[r=96,g=116,b=55]
java.awt.Color[r=78,g=83,b=42]
java.awt.Color[r=58,g=55,b=46]
java.awt.Color[r=56,g=56,b=44]
java.awt.Color[r=50,g=50,b=42]
java.awt.Color[r=40,g=35,b=29]
java.awt.Color[r=91,g=74,b=56]
java.awt.Color[r=90,g=71,b=54]
java.awt.Color[r=84,g=68,b=52]
java.awt.Color[r=74,g=60,b=49]