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

时间:2018-11-11 02:11:30

标签: java graphics copy bufferedimage

我有一个名为Bitmap的类,它是从BufferedImage扩展的,

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 个答案:

答案 0 :(得分: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将参数图像的宽度分配给this.height = image.getWidth(null);字段

其他问题:

  • 您不节省资源
  • 您要进行危险且不必要的演员

它应该是

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个JOptionPanes中看到3张图像显示为ImageIcons,第一个是原始的空白MyImage对象,然后在《第二次世界大战》中复制了2张图像后复制到原始图像中。