Java 在圖片上寫文字和圖片

寫文字之前.png

寫文字之后.png

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

import javax.imageio.ImageIO;
import javax.swing.;
import java.awt.
;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

public class MainIn {

static String picturePath;
static String pictureInName = "/bg.jpeg";
static String pictureOutName = "/bg_out.jpeg";


public static void main(String[] args) {
    String _dir = System.getProperty("user.dir");
    picturePath = _dir + File.separator + "src" + File.separator;

// exportImg1();
drawTextToPicture("我好,中國", picturePath + "/head_img.png");
}

///在圖片上畫文字
public static void drawTextToPicture(String username, String headImg) {
    try {
        //1.jpg是你的 主圖片的路徑
        InputStream is = new FileInputStream(picturePath + pictureInName);


        //通過JPEG圖象流創(chuàng)建JPEG數(shù)據(jù)流解碼器
        JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
        //解碼當前JPEG數(shù)據(jù)流,返回BufferedImage對象
        BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
        //得到畫筆對象
        Graphics g = buffImg.getGraphics();

        //創(chuàng)建你要附加的圖象。
        //小圖片的路徑
        ImageIcon imgIcon = new ImageIcon(headImg);

        //得到Image對象。
        Image img = imgIcon.getImage();

        //將小圖片繪到大圖片上。
        //5,300 .表示你的小圖片在大圖片上的位置。
        g.drawImage(img, 400, 15, null);

        //設(shè)置顏色。
        g.setColor(Color.BLACK);


        //最后一個參數(shù)用來設(shè)置字體的大小
        Font f = new Font("宋體", Font.PLAIN, 25);
        Color mycolor = Color.red;//new Color(0, 0, 255);
        g.setColor(mycolor);
        g.setFont(f);

        //10,20 表示這段文字在圖片上的位置(x,y) .第一個是你設(shè)置的內(nèi)容。
        g.drawString(username, 100, 135);

        g.dispose();
        OutputStream os;

        os = new FileOutputStream(picturePath + pictureOutName);
        //創(chuàng)鍵編碼器,用于編碼內(nèi)存中的圖象數(shù)據(jù)。
        JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
        en.encode(buffImg);

        is.close();
        os.close();
    } catch (ImageFormatException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

///創(chuàng)建一個文本畫布
public static void createTextCanvas() {
    int width = 100;
    int height = 100;
    String s = "你好";

    File file = new File(picturePath + pictureInName);
    Font font = new Font("Serif", Font.BOLD, 10);
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) bi.getGraphics();
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, width, height);
    g2.setPaint(Color.RED);

    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(s, context);
    double x = (width - bounds.getWidth()) / 2;
    double y = (height - bounds.getHeight()) / 2;
    double ascent = -bounds.getY();
    double baseY = y + ascent;

    g2.drawString(s, (int) x, (int) baseY);

    try {
        ImageIO.write(bi, "jpg", file);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}


2021年01月15日13:29:09更新
 文字增加抗鋸齒, 文本x  y 坐標校準
~~~



    ///在圖片上畫文字
    public static void drawTextToPicture(List<DetailCoordinate> _detailList) {
        try {

            BufferedImage _mainImage = getImage(picturePath + pictureInName);
            SunGraphics2D g = (SunGraphics2D) _mainImage.getGraphics();

            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

            OutputStream os = new FileOutputStream(picturePath + pictureOutName);
            for (DetailCoordinate itemCoordinate : _detailList) {
                if (itemCoordinate.type == 0) {
                    //最后一個參數(shù)用來設(shè)置字體的大小
                    Font f = new Font(Font.SERIF, Font.PLAIN, itemCoordinate.size);
                    Color myColor = fromStrToARGB(itemCoordinate.getColor());//new Color(0, 0, 255);
                    g.setColor(myColor);
                    g.setFont(f);

                    FontMetrics fm = g.getFontMetrics();
                    g.drawString(itemCoordinate.txt, itemCoordinate.x, fm.getMaxAscent() + itemCoordinate.y);
                } else if (itemCoordinate.type == 1) {
                    //創(chuàng)建你要附加的圖象。
                    //小圖片的路徑
                    ImageIcon imgIcon = new ImageIcon(itemCoordinate.txt);

                    //得到Image對象。
                    Image img = imgIcon.getImage();

                    //將小圖片繪到大圖片上。
                    //5,300 .表示你的小圖片在大圖片上的位置。
                    g.drawImage(img, itemCoordinate.x, itemCoordinate.y, null);

                    //設(shè)置顏色。
                    g.setColor(Color.BLACK);
                }
            }
            g.dispose();
            _mainImage.flush();

            ImageIO.write(_mainImage, "png", os);
        } catch (ImageFormatException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容