【福利】将图片转成文字组成的的图片,比如将苍老师图片转成“苍老师”的图片!
程序员文章站
2022-03-07 14:13:00
...
转化前:
转化后:
细节:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageDemo {
public static void main(String[] args) throws Exception {
String path ="E:\\workspace\\OCR_EXAMPLE\\poiUntil\\src\\main\\resources\\timg.jpg";
String tarImgPath="E:\\workspace\\OCR_EXAMPLE\\poiUntil\\src\\main\\resources\\timg-1.jpg";
File file = new File(path);
File file1 = new File(tarImgPath);
BufferedImage read = ImageIO.read(file);
BufferedImage bufferedImage = new BufferedImage(read.getWidth()*10, read.getHeight()*10, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = bufferedImage.createGraphics();
Font font = new Font("微软雅黑", Font.PLAIN, 10);
graphics.setFont(font);
for(int i=0;i<read.getWidth();i++){
for(int j=0;j<read.getHeight();j++){
graphics.setColor(new Color(read.getRGB(i,j)));
if(i%3==0){
graphics.drawString("仓",i*10,j*10);
}
if(i%3==1){
graphics.drawString("老",i*10,j*10);
}
if(i%3==2){
graphics.drawString("师",i*10,j*10);
}
}
}
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufferedImage, "jpg", outImgStream);
outImgStream.close();
}
}