简体   繁体   English

Java如何识别照片颜色?

[英]Java How can I recognize a photo color?

How can I recognise the color when i mouseover point to one postition of the photo? 当鼠标悬停指向照片的一个位置时,如何识别颜色?

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); 

Error occurs java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! 发生错误java.lang.ArrayIndexOutOfBoundsException:坐标超出范围!

The problem is the the X and Y coordinates of the mouse do not correspond to the X and Y coordinates of the image. 问题是鼠标的X和Y坐标与图像的X和Y坐标不对应。 Change it to something like this: 将其更改为如下所示:

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

Don't quote me exactly on the syntax, but that is the basic idea. 不要在语法上精确地引用我,但这是基本思想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM