使用java制作文字图片

使用java制作文字图片


学到一个好东西拿出来给大家一起分享一下

效果图

在这里插入图片描述

一步一步分析

这下能看到了吧,我对你的爱,在每一个字里行间哦~。有关程序员的强势浪漫。实现这个效果,代码非常简单,真实代码大约30行左右。完整代码在文章最后!依旧是,程序虽然很少,但是并不简单下面是用到的 api

java api学习手册:https://www.matools.com/api/java8

我们在代码中用到的类和方法。大家可以去上面链接学习一下哦

BufferedImage

构造方法
getGraphics()
getWidth()
getHeight()
getType()
getRGB()
ImageIO:

read()
write()
Graphics2D

setColor
drawString
我来依次讲解一下每行代码的作用。

首先先定义一个我们想要在图片中展示的内容

String base = "我爱你";

我们再读取我们想要转换的图片,这里呢,我们使用 ImageIO 中提供的静态方法 read(),传入一个文件,read() 方法回返回一个 BufferedImage 类型的图片缓存流。我们通过new File(path),来创建一个文件流。

BufferedImage image = ImageIO.read(new File(path));

我们再创建一个 BufferedImage 用于放置我们输出文字的图片。参数呢就是图片的宽度,图片的高度,和图片的类型。

BufferedImage newImage = new BufferedImage(image.getWidth(),image.getHeight(), image.getType());

创建一个2D座标转换及绘图相关的类 Graphics2D,用来设置每个像素点的颜色、字体大小和字体样式。

Graphics2D graphics2d = (Graphics2D) newImage.getGraphics();

设置字体风格、样式和大小

graphics2d.setFont(new Font("宋体", Font.BOLD, 12));

下面就是写一个for循环,循环遍历每一个像素点,将每隔12个像素点就替换为文字。

index
for (int y = 0; y < image.getHeight(); y += 12) {
    for (int x = 0; x < image.getWidth(); x += 12) {
      //循环每一个像素点
    }
}

for 循环中的的内容:获取图片当前位置像素的颜色

int pixel = image.getRGB(x, y);

for 循环中的的内容:分离出rgb三种颜色,分别进行灰度和二值化处理,想具体研究可以去找资料学习

int r = (pixel & 0xff0000) >> 16, 
    g = (pixel & 0xff00) >> 8, 
    b = pixel & 0xff;

for 循环中的的内容:通过graphics2d设置字体颜色

graphics2d.setColor(new Color(r, g, b));

for 循环中的的内容:在当前位置上绘上一个文字

graphics2d.drawString(String.valueOf(base.charAt(index % base.length)), x, y);

for 循环中的的内容:当前文字被绘上以后绘制下一个文字

index++;

最后,我们通过 ImageIO 方法,把图片重新绘制,并输出。

ImageIO.write(newImage, "JPG", new FileOutputStream("F:\\temp.jpg"));

在主方法中调用刚刚写的方法。

public static void main(final String[] args) {
    FontImage.createAsciiPic("C:\\Users\\huguiyun\\Pictures\\Camera Roll\\1.jpg");
    System.out.println("OK");
}

完整代码:

package com.hu;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class test {
    public static void createFongImg(String path){
        String base = "我爱你";
        try{
            BufferedImage image = ImageIO.read(new File(path));
            BufferedImage newImage = new BufferedImage(image.getWidth(),image.getHeight(),image.getType());
            Graphics2D graphics2D = (Graphics2D) newImage.getGraphics();
            graphics2D.setFont(new Font("宋体",Font.BOLD,12));
            int index = 0;
            for(int y = 0; y < image.getHeight(); y += 12){
                for (int x = 0; x < image.getWidth(); x += 12){
                    int pxcolor = image.getRGB(x,y);
                    int r = (pxcolor & 0xff0000) >> 16,
                            g = (pxcolor & 0xff00) >> 8,
                            b = pxcolor & 0xff;
                    graphics2D.setColor(new Color(r, g, b));
                    graphics2D.drawString(String.valueOf(base.charAt(index % base.length())), x, y);
                    index++;
                }
            }
            ImageIO.write(newImage, "JPG", new FileOutputStream("F:\\temp.jpg"));
        }catch (Exception e){
            e.printStackTrace();
        }
    };
    public static void main(String args[]){
        test.createFongImg("C:\\Users\\huguiyun\\Pictures\\Camera Roll\\1.jpg");
        System.out.println("OK");
    }
}

咋们有一说一哈!感觉小编帅气的请举手!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章