如何在java中使用JSlider增加/减less图像的亮度?
我使用java swing技术在netbeans平台上制作应用程序。我想对图像进行image processing。这个图像是由X射线枪捕获的。之后,我想使用JSlider来增加/减less图像的亮度。我使用paintComponent (Graphics g)方法。 但我想做的直接影响增加/减less图像brightnees而不使用paintComponenet(Graphics g)method.so我怎么能做到这一点? 我的代码如下所示。 在我的代码中,我使用JAI库的PlanarImage和BufferedImage类来加载.tiff图像,然后使用JSlider对象的statechange事件来增加/减less图像的亮度。
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(); } }); } }