And*_*son 6 java image javax.imageio
为什么组合BG为JPEG的图像会导致意外结果?
这是我在2个图像的覆盖中无法正常工作的答案的后续行动.在那里发布的源(使用在内存中创建的BG图像)看起来像这样:

到现在为止还挺好.但是那个问这个问题的人评论说如果BG是JPEG,那就失败了.认为他们错了,我改变了我的例子,将BG图像编码为JPEG.现在,如果我使用BufferedImage.TYPE_INT_ARGB或BufferedImage.TYPE_INT_RGB为最终图像我得到他们所指的:
TYPE_INT_ARGB
TYPE_INT_RGB
我期望结果与至少其中一个的原始结果相同(更多的是ARGB变体).
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import javax.imageio.ImageIO;
class CombineImages {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
URL urlImage1 =
new URL("http://i.stack.imgur.com/T5uTa.png");
// Load the FG image
Image fgImage = ImageIO.read(urlImage1);
int w = fgImage.getWidth(null);
int h = fgImage.getHeight(null);
// Create a non-trasparent BG image
BufferedImage bgImageTemp =
new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
ImageIO.write(bgImageTemp, "jpg", baos);
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
BufferedImage bgImageJpeg = ImageIO.read(bais);
int result = JOptionPane.showConfirmDialog(
null,
"Use a final image with transparency?",
"Transparency",
JOptionPane.YES_NO_OPTION);
int type = (result==JOptionPane.OK_OPTION ?
BufferedImage.TYPE_INT_ARGB :
BufferedImage.TYPE_INT_RGB);
// Create the final image
BufferedImage finalImage =
new BufferedImage(w,h,type);
Graphics2D g = finalImage.createGraphics();
g.drawImage(bgImageJpeg, w, h, null);
g.drawImage(fgImage, w, h, null);
g.dispose();
JPanel gui = new JPanel(new GridLayout(1,0,5,5));
gui.add(new JLabel(new ImageIcon(bgImageJpeg)));
gui.add(new JLabel(new ImageIcon(fgImage)));
gui.add(new JLabel(new ImageIcon(finalImage)));
JOptionPane.showMessageDialog(null, gui);
} catch (Exception e) {
e.printStackTrace();
}
}
};
SwingUtilities.invokeLater(r);
}
}
Run Code Online (Sandbox Code Playgroud)
看来这是由于拼写错误造成的。
在您引用的答案中,形成组合图像的代码是
Graphics2D g = finalImage.createGraphics();
g.drawImage(bgImage, 0, 0, null);
g.drawImage(fgImage, 0, 0, null);
Run Code Online (Sandbox Code Playgroud)
但在这个问题中,它被改为,
Graphics2D g = finalImage.createGraphics();
g.drawImage(bgImageJpeg, w, h, null);
g.drawImage(fgImage, w, h, null);
Run Code Online (Sandbox Code Playgroud)
后者从“左上角”开始绘制,这恰好是图像的右下角,因此没有真正绘制任何内容。然而,前者按照预期绘制了整个图像。