How to change the brightness of an Image

前端 未结 4 512
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 01:42

My Question: I want to be able to change the brightness of a resource image and have three instances of it as ImageIcons. One at 50% brightness (so darker), ano

4条回答
  •  误落风尘
    2021-02-06 01:57

    I would suggest just writing over the image with a semi-transparent black.

    Assuming you want to write directly on the image:

    Graphics g = img.getGraphics();
    float percentage = .5f; // 50% bright - change this (or set dynamically) as you feel fit
    int brightness = (int)(256 - 256 * percentage);
    g.setColor(new Color(0,0,0,brightness));
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    

    Or if you're just using the image for display purposes, do it in the paintComponent method. Here's an SSCCE:

    import java.awt.*;
    import java.awt.image.*;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    
    public class ImageBrightener extends JPanel{
    
        BufferedImage img;
        float percentage = 0.5f;
    
        public Dimension getPreferredSize(){
            return new Dimension(img.getWidth(), img.getHeight());
        }
    
        public ImageBrightener(){
            try {
                img = ImageIO.read(new URL("http://media.giantbomb.com/uploads/0/1176/230441-thehoff_super.jpeg"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(img, 0, 0, this);
            int brightness = (int)(256 - 256 * percentage);
            g.setColor(new Color(0,0,0,brightness));
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    
        public static void main(String[] args){
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new ImageBrightener());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    EDIT

    Assuming the same code as above, you can manipulate everything besides the Alpha by messing with the rasterizer. Here's an example (paint shadedImage instead of img if using this exmaple). Please note this doesn't catch edge cases of RGB values greater than 256 and less than 0.

            img = ImageIO.read(new URL("http://media.giantbomb.com/uploads/0/1176/230441-thehoff_super.jpeg"));
            shadedImage = new BufferedImage(img.getWidth(), img.getWidth(), BufferedImage.TYPE_INT_ARGB);
            shadedImage.getGraphics().drawImage(img, 0, 0, this);
    
            WritableRaster wr = shadedImage.getRaster();
            int[] pixel = new int[4];
    
            for(int i = 0; i < wr.getWidth(); i++){
                for(int j = 0; j < wr.getHeight(); j++){
                    wr.getPixel(i, j, pixel);
                    pixel[0] = (int) (pixel[0] * percentage);
                    pixel[1] = (int) (pixel[1] * percentage);
                    pixel[2] = (int) (pixel[2] * percentage);
                    wr.setPixel(i, j, pixel);
                }
            }
    

提交回复
热议问题