2

我正在使用 java swing 技术在 netbeans 平台上制作应用程序。我想对捕获的图像进行图像处理。此图像由 X-Ray Gun 捕获。之后我想使用 JSlider 增加/减少图像的亮度。我使用paintComponent 完成了这个(图形g)方法。但我想在不使用paintComponenet(Graphics g)方法的情况下直接增加/减少图像亮度的效果。那么我该怎么做呢?我的代码如下所示。在我的代码中,我使用 JAI 库的 PlanarImage 和 BufferedImage 类来加载 .tiff 图像,之后我使用 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();

 }
}
4

1 回答 1

4

目前尚不清楚您为什么要避免使用paintComponent(),但您始终可以更改BufferedImageaJLabelImageIconusing RescaleOp

附录:听起来您不想覆盖原始图像,因此更改副本自然是有意义的。不幸的是,您的示例仅复制了对原始示例的引用。相反,使用两个副本,传递原始 assrc和副本 as 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();
            }
        });
    }
}
于 2012-04-18T11:27:32.940 回答