Java Can';t按比例将BuffereImage绘制到另一个BuffereImage中

Java Can';t按比例将BuffereImage绘制到另一个BuffereImage中,java,awt,Java,Awt,请告知 我正在尝试将输入BuffereImage绘制成更大的输出BuffereImage(带有缩放)。请查看以下代码: 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++) {

请告知

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

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());
    }
}
似乎Graphics2D对象可以工作,因为我可以画一条调用drawLine函数的线。所以,我认为输入是问题的根源,但我不知道出了什么问题

更新: 我曾尝试使用
仿射变换
,但不幸的是,它没有帮助

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);
你的产生

-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());
    }
}
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入javax.swing.ImageIcon;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
公共班机{
公共空白打印(缓冲图像img、整数宽度、整数高度){
对于(int y=0;yint rgb=y*输入宽度*(1)你必须手动缩放图像吗?
Graphcs2D
AffineTransform
可以执行缩放操作文档说这个版本的drawImage应该动态缩放输入图像是的,那很好,你为什么不能使用
AffineTransform
?@MadProgrammer我试图使用
drawImage
v接受
AffineTransform
的版本,但不幸的是,它没有帮助。我将您的颜色算法替换为
color.RED.getRGB()
,它会生成一个结果-请原谅我对颜色数学的理解毫无用处,但是
BuffereImage
需要一个RGBA值-我会确保alpha值包含在您的计算中
0 1 
131072 131073 
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());
    }
}