如何使用 RescaleOp 设置图像的大小 [英] How to setSize of image using RescaleOp

查看:23
本文介绍了如何使用 RescaleOp 设置图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a test app. To set Alpha for image I use paintComponent method. Watch next snippet...

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

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

不允许像使用 g.drawImage(bImage, 0, 0,width,height, null) 一样设置宽度和高度;

does not allow to set width and height as if I use g.drawImage(bImage, 0, 0,width,height, null);

那么问题是...在这种情况下如何为 bImage 设置宽度和高度?

So the question is... How to set width and height for bImage in this case?

感谢任何有用的评论

安德鲁

推荐答案

filter(),如图此处,然后使用 drawImage()AffineTransformOp 缩放,如此处所示.

First filter(), as shown here, and then scale using drawImage() or AffineTransformOp, as shown here.

附录:或者,您可以缩放图像(使用上述任一方法),然后然后使用您的RescaleOpdrawImage().

Addendum: Alternatively, you can scale the image first (using either approach above) and then use your RescaleOp in drawImage().

顺便说一句,RescaleOp 缩放图像的色带;它不会改变图像的尺寸.为避免混淆,维度缩放有时称为重新采样.

As an aside, RescaleOp scales the image's color bands; it does not change the image's dimensions. To avoid confusion, dimensional scaling is sometimes called resampling.

附录:这是使用 drawImage() 重新采样和 RescaleOp 调整图像的 alpha.

Addendum: Here's an example of using drawImage() to resample and RescaleOp to adjust the alpha of an image.

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

这篇关于如何使用 RescaleOp 设置图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆