如何将图像数据复制到 BufferedImage 的子类中?

How to copy image data into a subclass of BufferedImage?

我有一个名为 Bitmap 的 class 扩展自 BufferedImage,

public class Bitmap extends BufferedImage {...

其方法之一称为 copyImage,可将源图像中的内容复制到 class,该方法有效,但此方法无法保持源图像的原始纵横比和尺寸。

public void copyImage(Image image) {
    if (image != null) {
        BufferedImage bi = (BufferedImage) image;
        Graphics g = getGraphics();

        g.drawImage(bi, 0, 0, width, height, null);
    }
}

我希望此方法将源图像复制到 class 并保持其原始纵横比和尺寸,我想调整宽度和高度我将上面的代码修改为:

public void copyImage(Image image) {
    if (image != null) {
        this.width = image.getWidth(null);
        this.height = image.getWidth(null);

        BufferedImage bi = (BufferedImage) image;
        Graphics g = getGraphics();

        g.drawImage(bi, 0, 0, width, height, null);
    }
}

但是还是不行,请问如何修改上面的代码来复制图片呢? 提前致谢。

这是错误的:

public void copyImage(Image image) {
    if (image != null) {
        this.width = image.getWidth(null);
        this.height = image.getWidth(null);

        BufferedImage bi = (BufferedImage) image;
        Graphics g = getGraphics();

        g.drawImage(bi, 0, 0, width, height, null);
    }
}

您的主要问题是:

  1. 您似乎在尝试更改原始图像 this 图像的固有宽度和高度,您不应该这样做,而不是这样
  2. 您正在使用 this.height = image.getWidth(null);
  3. 将参数图像的宽度分配给 this.height 字段

其他问题:

  • 你没有节约资源
  • 你在进行危险且不必要的转换

应该是

public void copyImage(Image image) {
    if (image != null) {
        // don't change the width/height of your original image
        int width = image.getWidth(null);
        // int height = image.getWidth(null);
        int height = image.getHeight(null); // *** Note change ***

        // BufferedImage bi = (BufferedImage) image;  // *** no need ***
        Graphics g = getGraphics();

        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();  // save resources
    }
}   

使用 MCVE 显示概念证明的测试代码:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImage {
    public static final String SOMME_PATH = "https://upload.wikimedia.org/"
            + "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
            + "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
    public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
            + "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";

    public static void main(String[] args) {
        int imgW = 1000;
        int imgH = 700;
        MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
        BufferedImage sommeTrench = null;
        BufferedImage battleOfDoberdò = null;

        try {
            URL url = new URL(SOMME_PATH);
            sommeTrench = ImageIO.read(url);

            url = new URL(BATTLE_PATH);
            battleOfDoberdò = ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        Icon icon = new ImageIcon(myImage);
        JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);

        myImage.copyImage(sommeTrench);
        icon = new ImageIcon(myImage);
        JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);

        myImage.copyImage(battleOfDoberdò);        
        icon = new ImageIcon(myImage);
        JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);

    }
}

class MyImage extends BufferedImage {

    public MyImage(int width, int height, int imageType) {
        super(width, height, imageType);
    }

    public void copyImage(Image image) {
        if (image != null) {
            int width = image.getWidth(null);

            int height = image.getHeight(null); // *** Note change ***

            Graphics g = getGraphics();

            g.drawImage(image, 0, 0, width, height, null);
            g.dispose(); // save resources
        }
    }    
}

如果你 运行 这段代码你会看到 3 张图像在 3 个 JOptionPanes 中显示为 ImageIcons,第一个是原始的空白 MyImage 对象,然后是来自 World War 的 2 张图像,我已被复制到原始图像中。