java使用BufferedImage和Graphics实现图片合成

时间:2023-02-06 05:09:39
package com.igoxin.weixin.custom;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ComposeImageTest { /**
* 图片合成
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { int i=1;
int j=2;
InputStream imagein = new FileInputStream(i + ".jpg");
InputStream imagein2 = new FileInputStream(j + ".png"); BufferedImage image = ImageIO.read(imagein);
BufferedImage image2 = ImageIO.read(imagein2);
Graphics g = image.getGraphics();
g.drawImage(image2, image.getWidth() - image2.getWidth() - 15, image.getHeight() - image2.getHeight() - 10,
image2.getWidth() + 10, image2.getHeight() + 5, null);
OutputStream outImage = new FileOutputStream("custom" + j + "-" + i + ".jpg");
JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
enc.encode(image);
imagein.close();
imagein2.close();
outImage.close(); }
}