【问题标题】:Java How can I recognize a photo color?Java 如何识别照片颜色?
【发布时间】:2011-05-08 13:54:44
【问题描述】:

当我将鼠标悬停在照片的一个位置时,如何识别颜色?

BufferedImage image = new BufferedImage("blueball.jpg");
Project() {
    jFrame.setSize(new Dimension(500, 320));
    jFrame.getContentPane().setLayout(null);
    colorLabelText.setBounds(new Rectangle(310, 210, 50, 30));
    colorLabelText.setText("Color :");
    colorLabel.setBounds(new Rectangle(370, 210, 100, 30));
    photoLabel.setBounds(new Rectangle(20, 20, 220, 250));
    photoLabel.addMouseListener(new RecognizeColorActionListener());
    jFrame.getContentPane().add(photoLabel);
    jFrame.getContentPane().add(colorLabelText);
    jFrame.getContentPane().add(colorLabel);
    jFrame.setVisible(true);
}


     class RecognizeColorActionListener implements MouseListener {
    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX(); 
                       int y = e.getY(); 
                       int imgx = image.getMinX(); 
                       int imgy = image.getMinY(); 
                       int c = image.getRGB(x - imgx, y - imgy); 

发生错误 java.lang.ArrayIndexOutOfBoundsException: 坐标超出范围!

【问题讨论】:

  • 你如何展示你的图片?
  • 我将它显示在一个标签中,标签位置是 photoLabel.setBounds(new Rectangle(20, 20, 220, 250));
  • 它是否曾经工作过,例如在左上角?您是否从 photoLabel 的左上角绘制图像? image.getWidth(), image.getHeight() 给你什么?
  • 找不到以文件名为参数的 java.awt.image.BufferedImage 的构造函数,是不是看错了类?
  • 如果使用 getWidth 和 getHeight 给照片的宽度和高度 250 200,我将标签移动到左上角也会出错。协调员越界,顺便说一句,谢谢我用机器人解决了。

标签: java colors awt


【解决方案1】:

问题是鼠标的 X 和 Y 坐标与图像的 X 和 Y 坐标不对应。把它改成这样:

int x = e.getX();
int y = e.getY();
int imgx = image.getX();
int imgy = image.getY();
int c = image.getRGB(x - imgx, y - imgy);

不要完全引用我的语法,但这是基本思想。

【讨论】:

  • 谢谢,是 getMinX() 还是 getMinY() ?我仍然收到此错误 java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
  • 我不确定,因为我现在所在的机器上没有 Java 开发环境。我所描述的是问题所在,但您需要尝试解决问题才能完全正确。
  • 可能略有不同,因为我的图像是缓冲图像
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2012-08-31
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多