如何将图像数据复制到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);
}
}

但它不起作用,我如何修改上面的代码来复制图像?提前谢谢。

这是错误的:

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

最新更新