Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在另一个上绘制缓冲图像?_Java_Image Processing_Bufferedimage - Fatal编程技术网

Java 在另一个上绘制缓冲图像?

Java 在另一个上绘制缓冲图像?,java,image-processing,bufferedimage,Java,Image Processing,Bufferedimage,我正在尝试使用java将两个图像放在一起。所以我试着在另一个缓冲图像上画一个缓冲图像它工作了,但是它破坏了图像的颜色最终的图像是绿色的 这是我的密码: try { BufferedImage source = ImageIO.read(new File("marker.png")); BufferedImage logo = ImageIO.read(new File("pic.png")); Graphics2D g = (Graphics2D) source.getGraphics()

我正在尝试使用java将两个图像放在一起。所以我试着在另一个缓冲图像上画一个缓冲图像它工作了,但是它破坏了图像的颜色最终的图像是绿色的 这是我的密码:

  try
{
BufferedImage source = ImageIO.read(new File("marker.png"));
BufferedImage logo = ImageIO.read(new File("pic.png"));

Graphics2D g = (Graphics2D) source.getGraphics();
g.drawImage(logo, 20, 50, null);
File outputfile = new File("image.jpg");
ImageIO.write(source, "jpg", outputfile);
}
catch (Exception e)
{
e.printStackTrace();
}

jpg可能会在压缩过程中弄乱您的数据-您可以尝试将png作为输出格式

为了确保您拥有所需的所有颜色,我建议使用具有所需颜色深度的专用目标图像,而不是覆盖源图像的颜色。像这样:

BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) target.getGraphics();
g.drawImage(source, 0, 0, null);
g.drawImage(logo, 20, 50, null);
File outputfile = new File("targetimage.png");
ImageIO.write(target, "png", outputfile);