当前位置: 首页 > 工具软件 > imagewriter > 使用案例 >

Java 实现无损画质给图片添加文字(ImageWriter)

胡沈义
2023-12-01
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) throws IOException {
        // 获取源图片路径
        URL source = Test.class.getResource("1.jpg");
        // 用imageIO读取图片
        Image image = ImageIO.read(source);
        // 利用BufferedImage,将图片加载到内存中
        int height = image.getHeight(null);
        int width = image.getWidth(null);
        BufferedImage bufimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        // 获取图像对象,来对图片进行处理
        Graphics2D g = bufimg.createGraphics();

        // 设置RenderingHints(渲染提示),以达到文字抗锯齿的功效,(key,value)形式赋值
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        rh.put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        g.setRenderingHints(rh);


        // 开始处理图片
        g.drawImage(image,0,0,width,height,null);
        // 设置颜色
        Color color = new Color(66,66,66);
        g.setColor(color);
        // 设置字体
        Font font = new Font("楷体",Font.PLAIN,25);
        g.setFont(font);
        // 添加文字
        g.drawString("不偏不倚,刚好是你",width/2-25*9/2,height/2-25/2);

        // 为了保证原质量输出,我们用ImageWriter输出图片
        // 获取ImageWriter对象
        Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
        ImageWriter writer = (ImageWriter)writers.next();
        // 指定输出路径
        File f = new File("out.jpg");
        ImageOutputStream ios = ImageIO.createImageOutputStream(f);
        // 修改ImageWriteParam,原质量输出图片
        ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
        imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        imageWriteParam.setCompressionQuality(1);
        // 将BufferedImage转换为IIOImage,进而输出图片
        IIOImage iio_image = new IIOImage(bufimg, null, null);
        // 输出
        writer.setOutput(ios);
        writer.write(null,iio_image,imageWriteParam);
    }
}

问题总结:

1. 获取图片路径问题

URL source = Test.class.getResource("1.jpg");

参考:https://blog.csdn.net/zhangshk_/article/details/82704010

2. 文字锯齿问题

https://www.cnblogs.com/Seamanm/archive/2011/03/27/1997044.html

https://blog.csdn.net/a664347225/article/details/79096137

3.图片被压缩的解决方案

https://www.cnblogs.com/rubekid/p/3955050.html

4.Linux报错Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable

https://blog.csdn.net/yfx000/article/details/86591945

5.ImageWriter和ImageReader详解

https://blog.csdn.net/u012454773/article/details/50735266

 类似资料: