How to increase/decrease brightness of image using JSlider in java?
Asked Answered
S

1

2

I m making App in netbeans platform using java swing Technology.I want to do image processing oncaptured image.This image is capture by X-Ray Gun.after that i want to increase/decrease brightness of image using JSlider.I done this using paintComponent(Graphics g) method. but i want to do direct effect of increased/decreased brightnees of image without using paintComponenet(Graphics g) method.so how can i do that? My code is shown below. In my code i use PlanarImage and BufferedImage class of JAI library for load .tiff imge and after that i use statechange event of JSlider object for increase/decrease brightness of image.

enter code here

 import java.awt.BorderLayout;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
 import java.awt.image.RescaleOp;
 import javax.media.jai.JAI;
 import javax.media.jai.PlanarImage;
 import javax.swing.*;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;

 public class Main extends JPanel implements ChangeListener {

 RescaleOp op;
 PlanarImage image = JAI.create("fileload", "F:\\java\\aimages\\teeth1.tiff");
 BufferedImage bufferedImage = image.getAsBufferedImage();
 BufferedImage bImage;

 int x1 = bufferedImage.getWidth();
 int y1 = bufferedImage.getHeight();

 JSlider slider = new JSlider(-10, 10);

 public Main() {
     bImage=bufferedImage; 
     slider.addChangeListener(this);
    this.add(slider, BorderLayout.class);
    // this.add(new JLabel(new ImageIcon(bufferedImage)), BorderLayout.NORTH);

 }

 @Override
 protected void paintComponent(Graphics g) {
    g.drawImage(bufferedImage, 0, 0, x1, y1, null);
 }

 public static void main(String[] argv) throws Exception {

    JFrame jFrame = new JFrame();
    jFrame.add(new Main());
    jFrame.setVisible(true);
    jFrame.setSize(200, 200);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

 @Override
 public void stateChanged(ChangeEvent e) {
    JSlider slid = (JSlider) e.getSource();
    float value = (float) slid.getValue();
    setValue(value);
 }

 private void setValue(float value) {

    bufferedImage=bImage;
    float scaleFactor = (float) (1.0 + (value / 10.0));
    op = new RescaleOp(scaleFactor, 0, null);
    bufferedImage = op.filter(bufferedImage, null);
    repaint();

 }
}
Sudden answered 18/4, 2012 at 11:19 Comment(3)
"for load .tiff imge" .. "F:\\java\\aimages\\batman.jpg" Umm.. that is a JPEG.Palaeo
ohhh. sorry sir its my mistake. i update my code and check it.and if u have any idea about that then share with me.Sudden
@AndrewThompson please refer this. #10208755Sudden
L
4

It's not clear why you want to avoid paintComponent(), but you can always alter the BufferedImage in a JLabel's ImageIcon using RescaleOp.

Addendum: I sounds like you don't want to overwrite the original image, so naturally it makes sense to alter a copy. Unfortunately, your example merely copies a reference to the original. Instead, use two copies, passing the original as src and the copy as dst, as shown the example below. See also AlphaTest.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @see https://mcmap.net/q/1019068/-how-to-increase-decrease-brightness-of-image-using-jslider-in-java/230513
 * @see https://stackoverflow.com/questions/5838842
 * @see https://stackoverflow.com/questions/5864490
 */
public class RescaleTest extends JPanel implements ChangeListener {

    private static final String NAME = "image.jpg";
    private BufferedImage image, copy;
    private JSlider slider = new JSlider();

    public RescaleTest() {
        PlanarImage pi = JAI.create("fileload", NAME);
        image = pi.getAsBufferedImage();
        copy = pi.getAsBufferedImage();
        this.setLayout(new BorderLayout());
        this.add(new JLabel(new ImageIcon(copy)));
        this.add(slider, BorderLayout.SOUTH);
        slider.setValue(slider.getMaximum() / 2);
        slider.addChangeListener(this);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        float value = (float) slider.getValue();
        float scaleFactor = 2 * value / slider.getMaximum();
        RescaleOp op = new RescaleOp(scaleFactor, 0, null);
        copy = op.filter(image, copy);
        repaint();
    }

    private void display() {
        JFrame f = new JFrame("RescaleTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new RescaleTest().display();
            }
        });
    }
}
Lutherlutheran answered 18/4, 2012 at 11:27 Comment(1)
i want to avoid paintComponent() because in my original program i get image path from JTree's node. means i take path of tree node then i open that image and then i want to increase/decrease brightness of image.if i use paintComponent then it will overwrite on my app image and customer see double conflict image.so. i want to avoid that method.if u have any solution of my problem then please share with me.Sudden

© 2022 - 2024 — McMap. All rights reserved.