java 生成相亲信息海报

 

package com.company;

import sun.font.FontDesignMetrics;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * Created by zj on 2018/10/18.
 */
public class haibao2 {

    public static void main(String[] args) {
//        String backgroundPath = "http://xqapi.haoyingsheng.top/profile/upload/2022/09/06/458db93c05d3156a6c3381823bf317dc%20-%20%E5%89%AF%E6%9C%AC_20220906115542A002.jpg";
//        String qrCodePath = "http://xqapi.haoyingsheng.top/profile/upload/2022/09/06/222878_20220906115731A003.jpg";
//        String people_img="http://xqapi.haoyingsheng.top/profile/upload/2022/08/22/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220822102619_20220822102729A007.png";
//        String people_img1="http://xqapi.haoyingsheng.top/profile/upload/2022/08/22/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220822102619_20220822102729A007.png";


        String backgroundPath = "C:\\Users\\Administrator\\Desktop\\海报\\haibaobeijing.jpg";
        String qrCodePath = "C:\\Users\\Administrator\\Desktop\\海报\\xiaochnegxuma.jpg";
        String people_img="C:\\Users\\Administrator\\Desktop\\海报\\renwu1.png";
        String people_img1="C:\\Users\\Administrator\\Desktop\\海报\\renwu1.png";


        String message01 ="山东地方个地方官地方官,方大哥大地飞歌电饭锅电饭锅电饭,锅电饭锅地方官方的,个地方官的山东地方个地方官地!!";
        String message02 = "";
        String outPutPath="D:\\ruoyi\\qr1.jpg";
        String age="23";
        String mHeight="180";
        String aihao="旅行";
        String xueli="博士";
        String zhiye="行政";
        String xianju="北京";
        overlapImage(backgroundPath,qrCodePath,people_img,people_img1,message01,message02,outPutPath,
                age,mHeight,aihao,xueli,zhiye,xianju
                );
    }
    public static String overlapImage(String backgroundPath,String qrCodePath,
                                      String people_img,String people_img1,
                                      String message01,String message02,
                                      String outPutPath,String age,
                                      String mHeight,String aihao,
                                      String xueli,String zhiye,
                                      String xianju
                                      ){
        int width=500;
        int height=888;
        try {
            //设置图片大小


            InputStream inputStream = new FileInputStream(backgroundPath);
            BufferedImage bi = ImageIO.read(inputStream);
            BufferedImage background = resizeImage(width,height, bi);

            InputStream inputStream1 = new FileInputStream(qrCodePath);
            BufferedImage bi1 = ImageIO.read(inputStream1);
            BufferedImage qrCode = resizeImage(60,60, bi1);

            InputStream inputStream2 = new FileInputStream(people_img);
            BufferedImage bi2 = ImageIO.read(inputStream2);
            BufferedImage peopleImg = resizeImage(261,381, bi2);

            InputStream inputStream3 = new FileInputStream(people_img1);
            BufferedImage bi3 = ImageIO.read(inputStream3);
            BufferedImage peopleImg1 = resizeImage(309,216, bi3);



            //在背景图片中添加入需要写入的信息,例如:扫描下方二维码,欢迎大家添加我的淘宝返利机器人,居家必备,省钱购物专属小秘书!
            Graphics2D g = background.createGraphics();
            Color color1=new Color(142,127,132);
            g.setColor(color1);

            g.setFont(new Font("",Font.BOLD,35));
            // 计算文字长度,计算居中的x点坐标
            Font font=new Font("",Font.BOLD,35);
            FontMetrics fm = g.getFontMetrics(font);
            int textWidth = fm.stringWidth(message02);
            int widthX = (width - textWidth) / 2;
            g.drawString(message02,widthX ,50);




            int fontsize2=16;
            g.setFont(new Font("",Font.PLAIN,fontsize2));
            Font font1=new Font("",Font.PLAIN,fontsize2);




//          画简介
            drawWordAndLineFeed(g,font1,message01,70,755,255);
//            构建年龄
            g.setFont(new Font("",Font.BOLD,fontsize2));
            Color color2=new Color(241,77,128);
            g.setColor(color2);

            FontMetrics fm1 = g.getFontMetrics(font1);
            textWidth =fm1.stringWidth(age);
            drawWordAndLineFeed(g,font1,age,126+(52-textWidth)/2,673,52);

//            身高
            textWidth =fm1.stringWidth(mHeight);
            drawWordAndLineFeed(g,font1,mHeight,250+(52-textWidth)/2,673,52);

//            爱好
            textWidth =fm1.stringWidth(aihao);
            drawWordAndLineFeed(g,font1,aihao,375+(52-textWidth)/2,673,52);


//          学历
            textWidth =fm1.stringWidth(xueli);
            drawWordAndLineFeed(g,font1,xueli,126+(52-textWidth)/2,718,52);
//          职业
            textWidth =fm1.stringWidth(mHeight);
            drawWordAndLineFeed(g,font1,zhiye,250+(52-textWidth)/2,718,52);


//            现居
            textWidth =fm1.stringWidth(xueli);
            drawWordAndLineFeed(g,font1,xianju,375+(52-textWidth)/2,718,52);


            g.drawImage(peopleImg, 71, 65, peopleImg.getWidth(), peopleImg.getHeight(), null);
            g.drawImage(peopleImg1, 121, 361, peopleImg1.getWidth(), peopleImg1.getHeight(), null);


            //在背景图片上添加二维码图片
            g.drawImage(qrCode, 416, 804, qrCode.getWidth(), qrCode.getHeight(), null);
            g.dispose();
            System.out.printf("outPutPath"+outPutPath);
            ImageIO.write(background, "jpg", new File(outPutPath));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static int drawWordAndLineFeed(Graphics2D g2d, Font font, String words, int wordsX, int wordsY, int wordsWidth) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        // 获取字符的最高的高度
        int height = metrics.getHeight();

        int width = 0;
        int count = 0;
        int total = words.length();
        System.out.println("几行字--"+total);
        String subWords = words;
        int b = 0;

        int zuidi=0;

        for (int i = 0; i < total; i++) {
            // 统计字符串宽度 并与 预设好的宽度 作比较
            if (width <= wordsWidth) {
                width += metrics.charWidth(words.charAt(i)); // 获取每个字符的宽度
                count++;
            } else {
                // 画 除了最后一行的前几行
                String substring = subWords.substring(0, count);
//                g2d.drawString(substring, wordsX, wordsY + (b * height));

                MyDrawString(substring, wordsX, wordsY + (b * height),1.2,g2d);

                subWords = subWords.substring(count);
                b++;
                width = 0;
                count = 0;
            }
            // 画 最后一行字符串
            if (i == total - 1) {
                g2d.drawString(subWords, wordsX, wordsY + (b * height));
            }
            zuidi=wordsY + (b * height);
        }
        System.out.println("高度--"+zuidi+25);
        return zuidi+25;
    }

    public static void MyDrawString(String str,int x,int y,double rate,Graphics g){
        String tempStr=new String();
        int orgStringWight=g.getFontMetrics().stringWidth(str);
        int orgStringLength=str.length();
        int tempx=x;
        int tempy=y;
        while(str.length()>0)
        {
            tempStr=str.substring(0, 1);
            str=str.substring(1, str.length());
            g.drawString(tempStr, tempx, tempy);
            tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate);
        }
    }


    private static File getFile(String url) throws Exception {
        //对本地文件命名
        String fileName = url.substring(url.lastIndexOf("."),url.length());
        File file = null;

        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {
            file = File.createTempFile("net_url", fileName);
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return file;
    }

    public static BufferedImage resizeImage(int x, int y, BufferedImage bfi){
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(
                bfi.getScaledInstance(x, y, Image.SCALE_DEFAULT), 0, 0, null);
        return bufferedImage;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值