用java实现给你的头像) +n

首先, 其实应该把标题改为——给任意图片右上角套上红色消息数目框;
代码如下:

//主程序:

package cn.sourcecodes.main;

import cn.sourcecodes.utils.DecorationUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;

/**
 * Created by cn.sourcecodes on 2016/11/26.
 */
public class DecoratePicture {

    public static final String OUTPUT_DIRECTORY = "d://decoratedPictures/";

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //如果输出文件夹不存在, 创建它
        File dir = new File(DecoratePicture.OUTPUT_DIRECTORY);
        if(!dir.exists()) {
            System.out.println("not exist");
            dir.mkdir();
        }

        boolean isQuit = false;
        while(!isQuit) {
            String inputPath;
            String messageNum;

            System.out.println("请输入文件路径如(d://test/test.jpg, 开头盘符后必须接 // 否则生成文件文件名不规范)");
            inputPath = input.nextLine();
            System.out.println("请输入消息数: ");
            messageNum = input.nextLine();

            File file = new File(inputPath);
            try {
                BufferedImage sourceImage = ImageIO.read(file);
                DecorationUtil.getAfterCombine(Integer.valueOf(messageNum), sourceImage);

                String outputPath = DecoratePicture.OUTPUT_DIRECTORY + "decorated_" + getFileName(inputPath);//输出文件路径
                
                OutputStream output = new FileOutputStream(outputPath);

                String format = getFileName(inputPath).split("\\.")[1];  //获取文件格式名, 比如  jpg
                
                ImageIO.write(sourceImage, format, output);

                output.close();

                System.out.println("quit 退出, 任意字符继续");
                String action = input.nextLine();

                if(action.equals("quit")) {
                    isQuit = true;
                }
            } catch (IOException e) {
                System.out.println("文件不存在");
            } catch (NumberFormatException e) {
                System.out.println("输入的不是数字");
            } catch (Exception e) {
                System.out.println("未知异常");
            }
        }
    }

    //获取传入文件的文件名
    public static String getFileName(String path) {
        String temp = path.substring(4); //去掉前面盘符, 如: d://

        //分割获取最后一个,就是文件名
        String[] splitTemp = temp.split("/");
        String fileName = splitTemp[splitTemp.length-1];

        return fileName;
    }
}



//工具类 package cn.sourcecodes.utils;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 * Created by cn.sourcecodes on 2016/11/26.
 */
public class DecorationUtil {

    public static void getAfterCombine(int messageNum, BufferedImage bufferedImage) {
        int pictureWidth = bufferedImage.getWidth();
        int pictureHeight = bufferedImage.getHeight();

        Graphics2D graphics2D = bufferedImage.createGraphics();

        BufferedImage messageImage = getMessageCircle(messageNum, pictureWidth, pictureHeight);

        int diameter;
        //消息圆圈直径为宽或者高的 1/3
        if(pictureWidth < pictureHeight) {
            diameter = pictureWidth / 3;
        } else {
            diameter = pictureHeight / 3;
        }
        int hideSize = diameter / 20;  //右边和上边隐藏的像素

        graphics2D.drawImage(messageImage, null, (pictureWidth + hideSize - diameter), -hideSize);
    }

    //圆框的bufferedImage, 需要传入显示的消息数, 原图片宽高
    public static BufferedImage getMessageCircle(int messageNum, int pictureWidth, int pictureHeight) {
        String numberStr;

        if(messageNum > 99) {
            numberStr = "99+";
        } else if(messageNum < 0) {
            numberStr = "0";
        } else {
            numberStr = String.valueOf(messageNum);
        }

        int diameter;
        //消息圆圈直径为宽或者高的 1/3
        if(pictureWidth < pictureHeight) {
            diameter = pictureWidth / 3;
        } else {
            diameter = pictureHeight / 3;
        }

        //这样构造的bufferedImage背景是黑色的
        BufferedImage bufferedImage = new BufferedImage(diameter, diameter, BufferedImage.TYPE_3BYTE_BGR);

        Graphics2D g2d = bufferedImage.createGraphics();

        //背景透明的bufferedImage
        bufferedImage = g2d.getDeviceConfiguration().createCompatibleImage(diameter, diameter, Transparency.TRANSLUCENT);

        g2d.dispose();//释放

        //下面开始画消息框
        Graphics2D graphics2D = bufferedImage.createGraphics();

        //先画红色圆圈
        graphics2D.setPaint(Color.RED);
        Ellipse2D ellipse = new Ellipse2D.Double(0, 0, diameter, diameter);
        graphics2D.fill(ellipse);

        //画消息, 消息要画在正中间
        int fontSize = diameter / 2; //字体大小为直径 2/6
        Font font = new Font("SansSerif", Font.PLAIN, fontSize);
        graphics2D.setFont(font);

        //获取包含字符串 numberStr 的矩形, 通过它来获取字符串占的宽高
        FontRenderContext context = graphics2D.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(numberStr, context);

        double x = (diameter - bounds.getWidth()) / 2;
        double y = (diameter - bounds.getHeight()) / 2;
        double acsent = -bounds.getY(); //上坡度;

        double baseY = y + acsent;

        //画消息
        graphics2D.setPaint(Color.WHITE);
        graphics2D.drawString(numberStr, (int)x, (int)baseY);

        graphics2D.dispose();

        return bufferedImage;
    }
}


全部评论
点赞!
点赞 回复 分享
发布于 2016-11-28 17:26
给个赞!
点赞 回复 分享
发布于 2016-11-28 17:05
厉害,图像处理算法那一块是比较费时的
点赞 回复 分享
发布于 2016-11-28 14:25
棒!加了精华~~~
点赞 回复 分享
发布于 2016-11-28 09:51
99+怎么弄的。
点赞 回复 分享
发布于 2016-11-27 10:43
整体效果图.. 第一次发帖, 不清楚怎么搞, 发上来的代码好像很乱...
点赞 回复 分享
发布于 2016-11-26 13:42
首次, 程序操作方法: 接下来是结果图片 小图片: 宽高正常图片: 最后一张是苍老师..
点赞 回复 分享
发布于 2016-11-26 13:40

相关推荐

03-01 19:30
已编辑
南京大学 Java
点赞 评论 收藏
分享
xtu大迫杰:偶遇校友,祝校友offer打牌
点赞 评论 收藏
分享
评论
点赞
17
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8359次浏览 76人参与
# 你的实习产出是真实的还是包装的? #
1545次浏览 39人参与
# 米连集团26产品管培生项目 #
5419次浏览 213人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7300次浏览 40人参与
# 简历第一个项目做什么 #
31444次浏览 320人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186719次浏览 1118人参与
# MiniMax求职进展汇总 #
23601次浏览 305人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152195次浏览 887人参与
# 研究所笔面经互助 #
118827次浏览 577人参与
# 重来一次,我还会选择这个专业吗 #
433235次浏览 3926人参与
# 简历中的项目经历要怎么写? #
309862次浏览 4177人参与
# 面试紧张时你会有什么表现? #
30460次浏览 188人参与
# 你今年的平均薪资是多少? #
212899次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63173次浏览 784人参与
# 我的求职精神状态 #
447918次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76344次浏览 374人参与
# 正在春招的你,也参与了去年秋招吗? #
363045次浏览 2635人参与
# 你怎么看待AI面试 #
179691次浏览 1216人参与
# 牛客AI文生图 #
21390次浏览 237人参与
# 职能管理面试记录 #
10773次浏览 59人参与
# 网易游戏笔试 #
6422次浏览 83人参与
# 腾讯音乐求职进展汇总 #
160527次浏览 1109人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务