2011-10-10 90 views
1

抛开其他任何错误,我需要一种将我的Color grayScale转换为int的方法。当我插入颜色时,出现错误。 setRGB方法将一个x,一个y和一个rgb int作为参数。如何将我的Color更改为int?将Color设置为用于setRGB(int x,int y,int rgb)方法的int值? - Java

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.image.*; 
import javax.imageio.ImageIO; 

public class Picture{ 
    Container content;  
    BufferedImage image, image2; 
    public Picture(String filename) { 
     File f = new File(filename); 
     //assume file is the image file 
     try { 
      image = ImageIO.read(f); 
     } 
     catch (IOException e) { 
      System.out.println("Invalid image file: " + filename); 
      System.exit(0); 
     } 
    } 

    public void show() { 
     final int width = image.getWidth(); 
     final int height = image.getHeight(); 

     JFrame frame = new JFrame("Edit Picture"); 

     //set frame title, set it visible, etc 
     content = frame.getContentPane(); 
     content.setPreferredSize(new Dimension(width, height)); 
     frame.pack(); 
     frame.setResizable(false); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //add a menubar on the frame with a single option: saving the image 
     JMenuBar menuBar = new JMenuBar(); 
     frame.setJMenuBar(menuBar); 
     JMenu fileMenu = new JMenu("File"); 
     menuBar.add(fileMenu); 
     JMenuItem saveAction = new JMenuItem("Save"); 
     fileMenu.add(saveAction); 
     JMenuItem grayScale = new JMenuItem("Grayscale"); 
     fileMenu.add(grayScale); 
     grayScale.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        grayscale(width, height); 
       } 
      }); 

     //add the image to the frame 
     ImageIcon icon = new ImageIcon(image); 
     frame.setContentPane(new JLabel(icon)); 

     //paint the frame 
     frame.setVisible(true); 
     frame.repaint(); 
    } 

    public void grayscale(int width, int height) { 
     for (int i = 0; i < width; i++) { 
      for (int j = 0; j < height; j++) { 
       Color color = new Color(image.getRGB(i, j)); 
       int red = color.getRed(); 
       int green = color.getGreen(); 
       int blue = color.getBlue(); 
       int rGray = red*(1/3); 
       int gGray = green*(2/3); 
       int bGray = blue*(1/10); 
       Color grayScale = new Color(rGray, gGray, bGray); 
       image.setRGB(i,j, grayScale); 
      } 
     } 
     show(); 
    } 

    public static void main(String[] args) { 
     Picture p = new Picture(args[0]); 
     p.show(); 
    } 
} 
+0

你能发布错误的堆栈跟踪吗? – Daniel

+0

错误信息? 找不到合适的方法setRGB(int,int,int,java.awt.Color) –

+0

另请参见[如何使用'TYPE_BYTE_GRAY'有效地创建使用AWT的灰度bufferedimage](http://stackoverflow.com/questions/ 3106269 /如何使用的型字节灰度到有效地创建-A-灰度-的BufferedImage-使用-A/3106550)。 – trashgod

回答

3

按照trashgod的评论。

Gray Scale image

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.image.*; 

public class Picture{ 
    Container content; 
    BufferedImage image; 
    BufferedImage image2; 
    public Picture(BufferedImage image) { 
     this.image = image; 
     grayscale(); 
    } 

    public void show() { 
     JFrame frame = new JFrame("Edit Picture"); 

     //set frame title, set it visible, etc 
     content = frame.getContentPane(); 
     frame.setResizable(false); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //add the image to the frame 
     ImageIcon icon = new ImageIcon(image2); 
     frame.setContentPane(new JLabel(icon)); 
     frame.pack(); 

     //paint the frame 
     frame.setVisible(true); 
    } 

    public void grayscale() { 
     // create a grayscale image the same size 
     image2 = new BufferedImage(
      image.getWidth(), 
      image.getHeight(), 
      BufferedImage.TYPE_BYTE_GRAY); 

     // convert the original colored image to grayscale 
     ColorConvertOp op = new ColorConvertOp(
      image.getColorModel().getColorSpace(), 
     image2.getColorModel().getColorSpace(),null); 
     op.filter(image,image2); 
     show(); 
    } 

    public static void main(String[] args) { 
     int size = 120; 
     int pad = 10; 
     BufferedImage bi = new BufferedImage(
      size, 
      size, 
      BufferedImage.TYPE_INT_RGB); 
     Graphics g = bi.createGraphics(); 
     g.setColor(Color.WHITE); 
     g.fillRect(0,0,size,size); 
     g.setColor(Color.YELLOW); 
     g.fillOval(pad,pad,size-(2*pad),size-(2*pad)); 
     g.dispose(); 

     Picture p = new Picture(bi); 
     p.show(); 
    } 
} 
2
image.setRGB(i, j, grayScale.getRGB()); 
+0

这只会返回一个黑屏与我的菜单栏。 :/ –

+0

@RMartin:你用这些得到了什么 - 'int rGray = red *(1/3); int gGray = green *(2/3); int bGray = blue *(1/10);'? –

+0

@BheshGurung我正在计算灰度值。从技术上来说,它应该更像* .29,* .58和* .11我相信,但是之后我需要使用双打,而且我不能在此方法中使用双打:/不知道如何使用它们,否则。 –

1

看看什么样的价值观(1/3)(2/3)(1/10)居然有,那么请注意,您是通过这些数字乘以你的red,​​和blue值。

0

颜色是一个对象。 rgb是一个int。使用colorName.getRGB()获取该颜色的整数RGB值。

相关问题