java使用Graphics2D进行图片文字合成示例

功能介绍:通过Graphics2D在一张背景图上添加文字描述和图片。
合成效果示意图:
在这里插入图片描述

代码示例:
在代码中对每步操作做了详细的注释

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JLabel;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sinolife.sf.store.SFFile;
import com.sinolife.sf.store.TempFileFactory;

public class DrawImgtest{
	
	private static final Logger logger = LoggerFactory.getLogger(DrawImgtest.class);
	
	private static TempFileFactory temFileFactory = TempFileFactory.getInstance(DrawImgtest.class);
	
	public static final String realPath = "C:\\Users\\yyyL\\Desktop\\合成\\";
	public static final String backgroundImage = "background.png"; // 背景图片
	public static final String qrCode = "qrCode.png"; // 二维码图片
	public static final String card = "card-big-8.png"; // 插入的小卡片

	
	private static String userId="121212122";
	
	public static void main(String[] args) throws Exception {
		
		Map<String, String> imageInfo1 = new HashMap<String, String>();
		imageInfo1.put("img", card);
		imageInfo1.put("desc", "小图上面的描述");
		ByteArrayOutputStream outStream = compose(imageInfo1);
		
		// TODO 保存生成的新图片
		File outputfile = new File(realPath+"合成.png");
        if (!outputfile.getParentFile().exists()) {
            outputfile.getParentFile().mkdirs();
        }
        FileOutputStream fs = new FileOutputStream(outputfile);
        fs.write(outStream.toByteArray());
        fs.flush();
        fs.close();

		FileOutputStream out = null;
		try {
			// 将文件临时保存
			SFFile tempFile = temFileFactory.createTempFile();
			out = tempFile.openOutputStream();
			byte[] b = outStream.toByteArray();
			out.write(b);
			out.flush();
			
			// TODO 后面可以通过 tempFile进行文件上传啥的
			
		} catch (Exception e) {
			try {
				if (outStream != null) {
					outStream.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
			
	}
	
	private static ByteArrayOutputStream compose(Map<String,String> imageInfo1) {
        OutputStream out = null;
        try {
            // 加载背景图片
            BufferedImage imageLocal = ImageIO.read(new File(realPath+backgroundImage));
            int backgroundWidth = imageLocal.getWidth();
            int backgroundHeight = imageLocal.getHeight();
            addText(imageLocal, "1张贺岁卡", new Font("微软雅黑", Font.PLAIN, 35),new Color(238, 65, 53),backgroundWidth,250);

            // 待插入卡片图片
            BufferedImage image1 = ImageIO.read(new File(realPath+imageInfo1.get("img")));
            addText(image1, imageInfo1.get("desc"), new Font("微软雅黑", Font.PLAIN, 48),new Color(252, 219, 179),image1.getWidth(),70);
            
            // 待插入二维码  190 200
            BufferedImage qrCodeImg = ImageIO.read(new File(realPath+qrCode));
            
            // 以背景图片为模板
            Graphics2D g2d = imageLocal.createGraphics();
            
            // 这里必须要先压缩小图大小,然后再合成,不压缩直接合成会造成小图不清晰
            image1 = disposeImage(image1, 410, 550);
            
            // TODO 输出小图
    		File outputfile1 = new File(realPath+"小图.png");
            if (!outputfile1.getParentFile().exists()) {
                outputfile1.getParentFile().mkdirs();
            }
            ImageIO.write(image1, "png", outputfile1);
            
            // 将小图绘画在背景图片上,生成新图片, drawImage方法参数的x和y:绘制点距离左边距和上边距的像素。 width和height:表示绘制的小图的大小
            g2d.drawImage(image1, (backgroundWidth - 410)/2, (backgroundHeight - 550)/2-90, 410, 550, null);
            g2d.drawImage(qrCodeImg, (backgroundWidth - 150)/2, backgroundHeight-260, 150, 150, null);
            g2d.dispose();
            
            // 生成的新图片写入文件流
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            ImageIO.write(imageLocal, "png", outStream);
			return outStream;
        } catch (Exception e) {
        	logger.error("DrawImgtest compose Exception: userId={}", userId, e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }
	
	/**
	 * 压缩图片大小
	 * @param src
	 * @param newWidth 新图片的宽度
	 * @param newHeight 新图片的高度
	 * @return
	 * @date 2021年11月23日
	 */
	private static BufferedImage disposeImage(BufferedImage image, int newWidth, int newHeight) {
		// 得到源图长
		BufferedImage newImg = null;
		// 判断输入图片的类型
		switch (image.getType()) {
		case 13:
			newImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);
			break;
		default:
			newImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
			break;
		}
		Graphics2D g = newImg.createGraphics();
		g.drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
		g.dispose();
		return newImg;
	}
	
	/**
	 * 渲染文字
	 * @param image1
	 * @param text
	 * @param font
	 * @param color
	 * @param width
	 * @param height
	 * @date 2021年12月15日
	 */
	private static void addText(BufferedImage image1,String text,Font font,Color color, int width, int height) {
		if(StringUtils.isBlank(text)) {
			return;
		}
		Graphics2D g2d = image1.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        JLabel jLabel = new JLabel();
        FontMetrics fm = jLabel.getFontMetrics(font);
        int textLength = fm.stringWidth(text);
        g2d.setFont(font);
        g2d.setColor(color);
        g2d.drawString(text, (width - textLength) / 2,height); //  设置文字的内容居中,渲染位置
        g2d.dispose();// 主动释放Graphics
	}
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JavaGraphics2D类压缩图片,你可以按照以下步骤: 1. 加载要压缩的图片,可以使用ImageIO类: ```java BufferedImage originalImage = ImageIO.read(new File("path/to/image")); ``` 2. 创建一个的BufferedImage对象,设置它的宽度和高度为原始图像的一半,或者其他你想要的压缩比例: ```java int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); int newWidth = originalWidth / 2; // 压缩一半 int newHeight = originalHeight / 2; // 压缩一半 BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); ``` 3. 获取Graphics2D对象,用它来绘制压缩后的图像: ```java Graphics2D g2d = compressedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); ``` 4. 最后,将压缩后的图像保存到文件或输出流中,可以使用ImageIO类: ```java ImageIO.write(compressedImage, "jpg", new File("path/to/compressed/image.jpg")); ``` 完整的代码: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageCompressor { public static void main(String[] args) throws Exception { BufferedImage originalImage = ImageIO.read(new File("path/to/image")); int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); int newWidth = originalWidth / 2; // 压缩一半 int newHeight = originalHeight / 2; // 压缩一半 BufferedImage compressedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); Graphics2D g2d = compressedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); ImageIO.write(compressedImage, "jpg", new File("path/to/compressed/image.jpg")); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值