无法将 BufferedImage 绘制到另一个具有比例的 BufferedImage

Can't draw BufferedImage into another BufferedImage with scale

请指教

我正在尝试将输入 BufferedImage 绘制成更大的输出 BufferedImage(带缩放)。请看下面的代码:

public class Main {
    public void print(BufferedImage img, int width, int height) {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                System.out.print(img.getRGB(x, y) + " ");
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) {
        Main app = new Main();

        // create input image
        int inputWidth = 2;
        int inputHeight = 2;
        BufferedImage inputImg = new BufferedImage(inputWidth, inputHeight, BufferedImage.TYPE_INT_ARGB);

        // fill input image
        for (int y = 0; y < inputHeight; y++) {
            for (int x = 0; x < inputWidth; x++) {
                inputImg.setRGB(x, y, y * inputWidth * (1 << 16) + x);
            }
        }

        // print
        app.print(inputImg, inputWidth, inputHeight);

        // create output image
        int outputWidth = 4;
        int outputHeight = 4;
        BufferedImage outputImg = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_ARGB);

        // draw inputImg into outputImg
        Graphics2D g = outputImg.createGraphics();
        g.drawImage(inputImg, 0, 0, outputImg.getWidth(), outputImg.getHeight(), 0, 0, inputImg.getWidth(), inputImg.getHeight(), null);

        // print
        app.print(outputImg, outputImg.getWidth(), outputImg.getHeight());
    }
}

执行产生以下输出:

0 1 
131072 131073 
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 

似乎 Graphics2D 对象有效,因为我能够绘制,例如,一条调用 drawLine 函数的线。所以,我认为 inputImg 是问题的根源,但我不知道出了什么问题。

更新: 我试过使用 AffineTransform,但不幸的是没有用。

Graphics2D g = outputImg.createGraphics();
AffineTransform at = new AffineTransform();
at.setToIdentity();
at.scale(2, 2);
g.drawImage(inputImg, at, null);

对我来说,这似乎与您使用的颜色计算有关...

当我改变...

inputImg.setRGB(x, y, y * inputWidth * (1 << 16) + x);

到...

int rgb = y * inputWidth * (1 << 16) + x;
inputImg.setRGB(x, y, new Color(rgb).getRGB());

我得到了一个结果,尽管是一个黑点。这向我表明,默认情况下,您的计算生成的 alpha 值为 0

这可以在他们产生的输出中得到证实:

我的方法生成

-16777216 -16777215 
-16646144 -16646143

你的生成

0 1 
131072 131073 

现在,坦率地说,这就是为什么我不做这种计算,而不是在 API 可以为我做的时候 - 但我很笨;P

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Main {

    public void print(BufferedImage img, int width, int height) {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                System.out.print(img.getRGB(x, y) + " ");
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) {
        Main app = new Main();

        // create input image
        int inputWidth = 2;
        int inputHeight = 2;
        BufferedImage inputImg = new BufferedImage(inputWidth, inputHeight, BufferedImage.TYPE_INT_ARGB);

        // fill input image
        System.out.println(inputWidth + "x" + inputHeight);
        Color color = Color.RED;
        for (int y = 0; y < inputHeight; y++) {
            for (int x = 0; x < inputWidth; x++) {
                int rgb = y * inputWidth * (1 << 16) + x;
                inputImg.setRGB(x, y, new Color(rgb).getRGB());
            }
        }

        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(inputImg)));

        // print
        app.print(inputImg, inputWidth, inputHeight);

        // create output image
        int outputWidth = 4;
        int outputHeight = 4;
        BufferedImage outputImg = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_ARGB);

        // draw inputImg into outputImg
        Graphics2D g = outputImg.createGraphics();
        g.drawImage(inputImg, 0, 0, outputImg.getWidth(), outputImg.getHeight(), 0, 0, inputImg.getWidth(), inputImg.getHeight(), null);
        g.dispose();
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(outputImg)));

        // print
        app.print(outputImg, outputImg.getWidth(), outputImg.getHeight());
    }
}