如何在java中使用JSlider增加/减少图像的亮度?
作者:互联网
我正在使用java swing技术在netbeans平台上制作应用程序.我想对捕获的图像进行图像处理.这个图像是由X-Ray Gun捕获的.之后我想使用JSlider增加/减少图像的亮度.我使用paintComponent完成了这个(图形g)方法.但我想直接影响增加/减少图像的亮度,而不使用paintComponenet(图形g)方法.那我怎么能这样做?我的代码如下所示.在我的代码中,我使用了JAI库的PlanarImage和BufferedImage类来加载.tiff imge,之后我使用JSlider对象的statechange事件来增加/减少图像的亮度.
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();
}
}
解决方法:
目前尚不清楚为什么要避免使用paintComponent(),但是你总是可以使用RescaleOp改变JLabel的ImageIcon中的BufferedImage.
附录:我听起来你不想覆盖原始图像,所以自然更改副本是有意义的.不幸的是,您的示例仅复制对原始的引用.相反,使用两个副本,将原始文件作为src传递,将副本传递为dst,如下例所示.另见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();
}
});
}
}
标签:brightness,jslider,java,swing 来源: https://codeday.me/bug/20190917/1810287.html