Java 如何使用“重缩放”设置图像大小

Java 如何使用“重缩放”设置图像大小,java,swing,user-interface,graphics,bufferedimage,Java,Swing,User Interface,Graphics,Bufferedimage,我正在编写一个测试应用程序。要为图像设置Alpha,我使用paintComponent方法。看下一段 public class TestImage extends JLabel{ public void paintComponent( Graphics g ) { super.paintComponent( g ); Graphics2D g2d=(Graphics2D)g;

我正在编写一个测试应用程序。要为图像设置Alpha,我使用paintComponent方法。看下一段

    public class TestImage extends JLabel{

        public void paintComponent( Graphics g ) {

                    super.paintComponent( g );

                    Graphics2D g2d=(Graphics2D)g;
                    g2d.drawImage(this.bImage, rop, 0, 0);


            }

    public void setRescaleOp(RescaleOp rop){this.rop=rop;}
}
如你所见

g2d.drawImage(this.bImage,rop,0,0)

不允许将宽度和高度设置为使用g.drawImage(bImage,0,0,width,height,null)

所以问题是。。。在这种情况下,如何设置双图像的宽度和高度

任何有用的意见都将不胜感激

Andrew

首先
filter()
,如图所示,然后使用
drawImage()
AffineTransformOp
进行缩放,如图所示

附录:或者,您可以先缩放图像(使用上述任一方法),然后在
drawImage()
中使用

另外,缩放图像的色带;它不会更改图像的尺寸。为了避免混淆,有时称为维度缩放

附录:下面是一个使用
drawImage()
重新采样和调整图像alpha的示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @see https://stackoverflow.com/questions/5838842
 * @see https://stackoverflow.com/questions/5864490
 */
public class AlphaTest {

    private static void display() {
        JFrame f = new JFrame("AlphaTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ImageIcon icon = new ImageIcon("image.jpg");
        final AlphaPanel ip = new AlphaPanel(icon, 0.75);
        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                int v = slider.getValue();
                ip.setAlpha((float) v / slider.getMaximum());
                ip.repaint();
            }
        });
        f.add(ip, BorderLayout.CENTER);
        f.add(slider, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                display();
            }
        });
    }
}

class AlphaPanel extends JPanel {

    private BufferedImage bi;
    private float[] scales = {1f, 1f, 1f, 0.5f};
    private float[] offsets = new float[4];
    private RescaleOp rop;

    public AlphaPanel(ImageIcon icon, double scale) {

        int width = (int) (scale * icon.getIconWidth());
        int height = (int) (scale * icon.getIconHeight());
        this.setPreferredSize(new Dimension(width, height));
        this.bi = new BufferedImage(
            width, height, BufferedImage.TYPE_INT_ARGB);
        this.bi.createGraphics().drawImage(
            icon.getImage(), 0, 0, width, height, null);
        rop = new RescaleOp(scales, offsets, null);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(bi, rop, 0, 0);
    }

    public void setAlpha(float alpha) {
        this.scales[3] = alpha;
        this.rop = new RescaleOp(scales, offsets, null);
    }
}


我尝试过使用filter方法,但这无助于像我在问题g2d.drawImage(this.bImage,rop,0,0)中提到的代码那样对图像进行前后过滤;问题是,我想通过拖动JSlider或单击JButton将同一图像的alpha设置为1到0。。。所以,若我使用过滤器,它似乎会返回一个初始化的BuffereImage,这使得不可逆的alpha值在下一次和下一次的动态变化。这就是为什么我想看到一些alt决策:)代码示例显示了如何将alpha设置为0旁边,并返回到1,但我不明白在这种情况下如何设置图像宽度和高度?在这种情况下,可以实际重新缩放类帮助吗?它的识别器看起来像“缩放”字,所以也许它可以控制图像缩放为w/h?不,它是缩放颜色模型,而不是尺寸。