java 使用Graphics2D在圖片上寫字
使用Graphics2D在圖片上寫字
首先jsp頁面:src是一個方法地址
然后在Controller如下:寫一個生成方法,用流進(jìn)行讀寫
生成方法,(關(guān)鍵):
注意:這種方法可行,但是需要計算文字的位置,感覺不太靈活,如果誰有更好的方法請分享一下。
Graphics2D 的使用
1、Graphics2D和Graphics的區(qū)別
此 Graphics2D 類擴(kuò)展Graphics 類,以提供對幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制。它是用于在Java(tm) 平臺上呈現(xiàn)二維形狀、文本和圖像的基礎(chǔ)類。
tips:總而言之Graphics2D是Graphics的加強(qiáng)版,增強(qiáng)了一些高級操作。
2、setRenderingHint的具體用處
為呈現(xiàn)算法設(shè)置單個首選項(xiàng)的值。提示類別包括對呈現(xiàn)過程中的呈現(xiàn)質(zhì)量和總時間/質(zhì)量折衷的控制。
tips:對呈現(xiàn)圖像效果處理方式進(jìn)行設(shè)置,例如抗鋸齒、顏色、差值等各種效果的處理算法。(我現(xiàn)在主要覺得抗鋸齒有用,以后會有不同的感受吧。)下面是兩個對比圖。
未開啟抗鋸齒
開啟抗鋸齒
下面是代碼實(shí)現(xiàn):
BufferedImage image = new BufferedImage(260, 80, BufferedImage.TYPE_INT_BGR); //獲取Graphics2D對象 Graphics2D graphics = image.createGraphics(); //開啟文字抗鋸齒 graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //設(shè)置字體 Font font = new Font("Algerian", Font.ITALIC, 40); graphics.setFont(font); //向畫板上寫字 graphics.drawString("This is test!", 10, 60); graphics.dispose();
3、Stroke的使用方法
描繪一個 Shape 如同使用一支具有適當(dāng)大小和形狀的畫筆描繪其輪廓。
tips:相當(dāng)于畫筆頭,用來定義畫出來的線條的特征;通過設(shè)置不同的參數(shù)來表現(xiàn)出各種不同的線條類型。更多詳細(xì)介紹可以觀看BasicStroke的用法這篇文章,里面有詳細(xì)敘述。
下面是我自己寫的一個小例子(指定背景圖,并畫一個樹):
package com.msw.test; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.RenderingHints; import java.awt.Stroke; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class MyTest { public static void main(String[] args) throws IOException { BufferedImage image = new BufferedImage(400, 500, BufferedImage.TYPE_INT_BGR); BufferedImage image2 = ImageIO.read(new FileInputStream(new File("F:\\2.jpg"))); // 獲取Graphics2D對象 Graphics2D graphics = image.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); //把image2設(shè)置為背景圖片 graphics.drawImage(image2, 0, 0, 400, 500,null); // 設(shè)置畫筆 Stroke stroke = new BasicStroke(5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); graphics.setStroke(stroke); // 設(shè)置顏色--綠色 graphics.setColor(Color.GREEN); //畫樹 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Polygon polygon = new Polygon(); polygon.addPoint(200, 100); polygon.addPoint(180, 150); polygon.addPoint(220, 150); graphics.draw(polygon); polygon.reset(); polygon.addPoint(200, 150); polygon.addPoint(160, 200); polygon.addPoint(240, 200); graphics.draw(polygon); polygon.reset(); polygon.addPoint(200, 200); polygon.addPoint(140, 250); polygon.addPoint(260, 250); graphics.draw(polygon); polygon.reset(); polygon.addPoint(180, 250); polygon.addPoint(180, 300); polygon.addPoint(220, 300); polygon.addPoint(220, 250); graphics.draw(polygon); graphics.dispose(); File file = new File("F:\\1.jpg"); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); ImageIO.write(image, "jpg", fos); fos.close(); } }
使用Graphics2D圖片上寫字,計算寬和高 以及字體的位置
如上圖,這個是一個有中文與英文的字符串。 中文與英文字符的寬高是不一樣的,如果想要生成一張寬高剛的圖片,這樣我就需要計算每一個字符的寬度,及合適的高;
java.awt.FontMetrics 這個類對文字的寬高位置有詳細(xì)的介紹;
計算使用字體的一段字符串的寬
public static int getWordWidth(Font font, String content) { FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font); int width = 0; for (int i = 0; i < content.length(); i++) { width += metrics.charWidth(content.charAt(i)); } return width; }
計算使用字體的最大的高度
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font); int height = metrics.getHeight();
在圖片是寫文字時最合適的位置
Ascent是從基線到頂部最大的高,也可以當(dāng)做一個種字體畫圖時最有可以點(diǎn)用的高度
graphics.drawString(content, 0, metrics.getAscent());
測試代碼
import sun.font.FontDesignMetrics; import javax.imageio.ImageIO; import java.awt.*; import java.awt.font.LineMetrics; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * Created by zengrenyuan on 18/5/11. */ public class ImageTest { public static void main(String[] args) throws IOException { Font font = new Font("微軟雅黑", Font.BOLD, 32); String content = "你好Java!"; FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font); int width = getWordWidth(font, content);//計算圖片的寬 int height = metrics.getHeight();//計算高 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = bufferedImage.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); //設(shè)置背影為白色 graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()); graphics.setFont(font); graphics.setColor(Color.BLACK); graphics.drawString(content, 0, metrics.getAscent());//圖片上寫文字 graphics.dispose(); write(bufferedImage, "/data/test.png"); } public static int getWordWidth(Font font, String content) { FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font); int width = 0; for (int i = 0; i < content.length(); i++) { width += metrics.charWidth(content.charAt(i)); } return width; } public static void write(BufferedImage bufferedImage, String target) throws IOException { File file = new File(target); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try (OutputStream os = new FileOutputStream(target)) { ImageIO.write(bufferedImage, "PNG", os); } } }
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Java循環(huán)中的For和For-each哪個更快
本文主要介紹了淺談Java循環(huán)中的For和For-each哪個更快,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java 中實(shí)現(xiàn)隨機(jī)無重復(fù)數(shù)字的方法
為了更好地理解這個題意,我們先來看下具體內(nèi)容:生成一個1-100 的隨機(jī)數(shù)組,但數(shù)組中的數(shù)字不能重復(fù),即位置是隨機(jī)的,但數(shù)組元素不能重復(fù)2013-03-03idea導(dǎo)入配置Spring?Boot項(xiàng)目的詳細(xì)步驟教程
這篇文章主要給大家介紹了關(guān)于idea導(dǎo)入配置Spring?Boot項(xiàng)目的詳細(xì)步驟,在項(xiàng)目開發(fā)過程中,無論是導(dǎo)入運(yùn)行團(tuán)隊(duì)開發(fā)的項(xiàng)目,還是一些開源項(xiàng)目,還是其他的項(xiàng)目,想要在IDEA中完整的運(yùn)行起來總有很多坑,需要的朋友可以參考下2023-08-08Java如何跳出當(dāng)前多重循環(huán)你知道嗎
這篇文章主要為大家介紹了Java跳出當(dāng)前多重循環(huán),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01