yankj12 / blog

技术研究、管理实践、其他的一些文章
MIT License
1 stars 2 forks source link

生成一个类似github头像的图片 #39

Open yankj12 opened 6 years ago

yankj12 commented 6 years ago

生成一个类似github头像的图片,目前考虑的是使用java

v1.0

1.0版本考虑的是生成一个基本的图片

v2.0

2.0版本考虑将图片转换成类似github头像的图片 读取一个照片,二值化后将二维数组拆分成多个二维数组,计算每个二位数组的值来决定使用什么颜色

yankj12 commented 6 years ago

v1.0 可以参考

package com.yan.image;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class TestImageLogo {

    public static void main(String[] args) {
        // 图片的宽度
        int imageWidth = 300;
        // 图片的高度
        int imageHeight = 300;

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

        Graphics2D main = image.createGraphics();
        //main.setColor(Color.white);
        main.fillRect(0, 0, imageWidth, imageHeight);

        //***********************页面头部
        Graphics title = image.createGraphics();
        //设置区域颜色
        // 海洋绿
        Color seaGreen = new Color(46, 139, 87);
        // 蜂蜜
        Color honeydew = new Color(240, 255, 240);
        // 淡绿色
        Color lightGreen = new Color(144, 238, 144);

        Color[] colors = new Color[]{seaGreen, lightGreen};

        Random random = new Random();

        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){

                int width = imageWidth/10;
                int height = imageHeight/10;

                int leftTopX = i * width;
                int leftTopY = j * height;

                int colorIndex = random.nextInt(colors.length);

                //填充区域并确定区域大小位置
                title.setColor(colors[colorIndex]);
                title.fillRect(leftTopX, leftTopY, width, height);
            }
        }

        createImage(image, "E:\\hehe.jpg");
    }

    public static void createImage(BufferedImage image, String fileLocation) {
        BufferedOutputStream bos = null;
        if(image != null){
            try {
                FileOutputStream fos = new FileOutputStream(fileLocation);
                bos = new BufferedOutputStream(fos);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                encoder.encode(image);
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(bos!=null){//关闭输出流
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
yankj12 commented 6 years ago

颜色取值可以参考 RGB颜色

yankj12 commented 6 years ago

读取图片,在转换成小方块类型的样式可能效果并不是很好,如下图示例 原图 docker 原图

拆分成大概10x10个小方块 docker_10x10

拆分成20x20个小方块 docker_20x20

拆分成40x40个小方块 docker_40x40

拆分成55x55个小方块 docker_55x55

可以看出效果不是很好

yankj12 commented 6 years ago

可以考虑设置小方块的宽和高,而不是设置将图片拆分成几乘几的方块