有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个叫做位图的类,它是从BuffereImage扩展而来的

public class Bitmap extends BufferedImage {...

它的一个名为copyImage的方法将内容从源图像复制到类中,虽然有效,但该方法无法保持源图像的原始纵横比和尺寸

public void copyImage(Image image) {
    if (image != 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) 个答案

  1. # 1 楼答案

    这是错误的:

    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);将参数图像的宽度指定给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中显示为ImageIcon,第一个是原始的空白MyImage对象,然后在第一次世界大战中的2个图像被复制到原始图像中之后