Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

swing - How to increase/decrease brightness of image using JSlider in java?

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

 }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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://stackoverflow.com/q/10208255/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();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...