java生成图片文字及在相应图片上添加文字

/**
 * 
 */
package com.nuotai.scm.business;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.junit.Test;

/** @version:
 * @Description:  
 * @author: LiGuoWei
 * @date: 2019年2月26日上午9:00:28
 */

public class test {
        //测试生成图片   无图片 时创建图片
        @Test
        public void test01(){  
             int width = 100;      
             int height = 100;      
             String s = "你好";      
               
             File file = new File("D:/image.jpg");      
             
             Font font = new Font("Serif", Font.BOLD, 10);      
            //创建一个画布  
             BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);      
            //获取画布的画笔  
             Graphics2D g2 = (Graphics2D)bi.getGraphics();      
               
            //开始绘图  
            g2.setBackground(Color.WHITE);      
             g2.clearRect(0, 0, width, height);      
             g2.setPaint(new Color(0,0,255));      
             g2.fillRect(0, 0, 100, 10);  
             g2.setPaint(new Color(253,2,0));  
             g2.fillRect(0, 10, 100, 10);  
             g2.setPaint(Color.red);  
     
              
             FontRenderContext context = g2.getFontRenderContext();      
             Rectangle2D bounds = font.getStringBounds(s, context);      
             double x = (width - bounds.getWidth()) / 2;      
             double y = (height - bounds.getHeight()) / 2;      
             double ascent = -bounds.getY();      
             double baseY = y + ascent;      
     
            //绘制字符串  
             g2.drawString(s, (int)x, (int)baseY);   
     
             try {  
                //将生成的图片保存为jpg格式的文件。ImageIO支持jpg、png、gif等格式  
                ImageIO.write(bi, "jpg", file);  
            } catch (IOException e) {  
                System.out.println("生成图片出错........");  
                e.printStackTrace();  
            }      
     
        }  
        //在图片中 添加
        public static void main(String[] args) throws IOException {
            String sourceImg="D:\\123.jpg"; //源图片地址
            String targetImg="D:\\1234.jpg"; //新存储的地址
            markImgMark(sourceImg,targetImg);
        }
        
        /**
         * 为图片添加图片文字
         * @param sourceImg 原图
         * @param targetImg 制作完成的图片
         * @return
         * @throws IOException
         */
        public static String markImgMark(String sourceImg, String targetImg) throws IOException {
            String result = "添加图片水印出错";
            File file = new File(sourceImg);
            Image img = ImageIO.read(file);
            int width = img.getWidth(null);//水印宽度
            int height = img.getHeight(null);//水印高
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bi.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            String s = "你好,我是图片上的合格证文字1222222222222222222222222222222222";      
             //字体
             Font font = new Font("Serif", Font.BOLD, 10); 
             g.setBackground(Color.WHITE);      
             g.fillRect(0, 10, 100, 10);  
             g.setPaint(Color.red);  
     
              
             FontRenderContext context = g.getFontRenderContext();      
             Rectangle2D bounds = font.getStringBounds(s, context);      
             double x = (width - bounds.getWidth()) / 2;      
             double y = (height - bounds.getHeight()) / 2;      
             double ascent = -bounds.getY();      
             double baseY = y + ascent;      
     
            //绘制字符串  
             g.drawString(s, (int)x, (int)baseY);  
             g.drawString(s, (int)x, (int)baseY+100);  
            File sf = new File(targetImg);
            ImageIO.write(bi, "jpg", sf); // 保存图片
            System.out.println("添加图片水印成功");
            return result;
        }
        
    
}
 

你可能感兴趣的:(java生成图片文字及在相应图片上添加文字)