如何改变图像的亮度?

时间:2013-08-15 10:41:22

标签: java swing

我已经创建了一个应用程序,它从特定目录加载图像,加载堆栈中的所有图像,当我点击下一个按钮时,下一个图像加载。 我还添加了JSlider来改变加载图像的亮度,但它不起作用。 我不知道为什么,但我没有得到确切的问题。

我的代码:

public class PictureEditor extends JFrame 
{ 
    private static final long serialVersionUID = 6676383931562999417L; 

    String[] validpicturetypes = {"png", "jpg", "jpeg", "gif"}; 
    Stack<File> pictures ;
    JLabel label = new JLabel(); 
    BufferedImage a = null; 
    float fval=1;

    public PictureEditor() 
    { 
        JPanel panel = new JPanel(); 
        JMenuBar menubar = new JMenuBar(); 
        JMenu toolsmenu = new JMenu("Tools"); 


        final JSlider slider1;
            slider1 = new JSlider(JSlider.HORIZONTAL,0,4,1);
            slider1.setToolTipText("Slide To Change Brightness");
            slider1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
            slider1.setMajorTickSpacing(1);
            slider1.setPaintLabels(true);
            slider1.setPaintTicks(true);
            slider1.setPaintTrack(true);
            slider1.setAutoscrolls(true);
            slider1.setBounds(50, 55, 200, 50);

            slider1.addChangeListener(new ChangeListener() 
            {

                @Override
                public void stateChanged(ChangeEvent e) 
                {
                    System.out.println("Before");
                    fval=slider1.getValue();
                    chgBrt(fval);
                    repaint();
                }
            });

        TitledBorder title;
        title = BorderFactory.createTitledBorder("Operations");
        title.setTitleJustification(TitledBorder.LEFT);

        JButton RT90 = new JButton("");
        JButton RT180 = new JButton("");
        JButton RTM90 = new JButton("");
        JButton RTM180 = new JButton("");
        JButton NEXT = new JButton("");
        Image img = null;
        Image imgn = null;

        try 
        {
            img = ImageIO.read(getClass().getResource("/images/images_Horizontal.png"));
            imgn = ImageIO.read(getClass().getResource("/images/next12.png"));
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        RT90.setIcon(new ImageIcon(img));
        RT180.setIcon(new ImageIcon(img));
        RTM90.setIcon(new ImageIcon(img));
        RTM180.setIcon(new ImageIcon(img));
        NEXT.setIcon(new ImageIcon(imgn));

        JPanel buttonPane = new JPanel();

        buttonPane.add(slider1);

        buttonPane.add(Box.createRigidArea(new Dimension(250,0)));

        buttonPane.setBorder(title);
        buttonPane.add(RT90);
        buttonPane.add(RT180);
        buttonPane.add(RTM90);
        buttonPane.add(RTM180);


        buttonPane.add(Box.createRigidArea(new Dimension(250,0)));


        NEXT.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) 
            {           
                nextImage();
            }
        });
        buttonPane.add(NEXT);
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

        final File dir = new File("");
        final JFileChooser file;
        file = new JFileChooser();
        file.setCurrentDirectory(dir);
        file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        file.showOpenDialog(panel);
        String path = file.getSelectedFile().getAbsolutePath();
        System.out.println(path);
        pictures= getFilesInFolder(path.toString()); 

        Action nextpictureaction = new AbstractAction("Next Picture") 
        { 
            private static final long serialVersionUID = 2421742449531785343L; 

            @Override 
            public void actionPerformed(ActionEvent e) 
            {   
                nextImage();    
            }

        }; 
        setJMenuBar(menubar); 
        menubar.add(toolsmenu); 
        toolsmenu.add(nextpictureaction); 

        panel.add(label,BorderLayout.CENTER); 
        add(panel);     

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLocationByPlatform(true); 
        setSize(1000, 700); 
        setTitle("PictureEditor"); 
        setVisible(true); 
    } 

    public Stack<File> getFilesInFolder(String startPath) 
    { 
        File startFolder = new File(startPath); 
        Stack<File> picturestack = new Stack<File>(); 

        String extension; 
        int dotindex; 

        // Go through the folder 
        for (File file : startFolder.listFiles()) 
        { 
            extension = ""; 
            dotindex = file.getName().lastIndexOf('.'); // Get the index of the dot in the filename 

            if (dotindex > 0) 
            { 
                extension = file.getName().substring(dotindex + 1);             
                // Iterate all valid file types and check it 
                for (String filetype : validpicturetypes) 
                { 
                    if (extension.equals(filetype))
                    { 
                        picturestack.add(file); 
                    } 
                } 
            } 
        } 
    return picturestack; 
    } 

    public void nextImage()
    {       

        try 
        {
            a=ImageIO.read(pictures.pop().getAbsoluteFile());       
        } 
        catch (IOException e1) 
        {
            e1.printStackTrace();
        }
        final ImageIcon image = new ImageIcon(a);

        label.setIcon(image);
        repaint();
    }
    @SuppressWarnings("null")
    public void chgBrt(float f)
    {
          Graphics g = null;
          Graphics2D g2d=(Graphics2D)g;
          try
          {  
              BufferedImage dest=changeBrightness(a,(float)fval);          
              System.out.println("Change Bright");

              int w = a.getWidth();
              int h = a.getHeight();              
              g2d.drawImage(dest,w,h,this);      

              ImageIO.write(dest,"jpeg",new File("/images/dest.jpg"));
              System.out.println("Image Write");
          }
          catch(Exception e)
          {
                e.printStackTrace();
          }
    }

    public BufferedImage changeBrightness(BufferedImage src,float val)
    {
         RescaleOp brighterOp = new RescaleOp(val, 0, null);
         return brighterOp.filter(src,null); //filtering
    }

    public static void main(String[] args) 
    { 
        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        { 
            public void run() 
            { 
                new PictureEditor(); 
            } 
        }); 
    } 
}

任何能指导我并告诉我错误的人

1 个答案:

答案 0 :(得分:1)

您可以调整此RescaleTest中显示的方法,这会将传递给scaleFactor的{​​{1}}变为零到滑块最大值的两倍之间的值。