
圖像中的標註可以突出顯示關鍵細節,使視覺效果更加信息豐富和引人入勝。無論是標記圖表的一部分還是突出顯示圖形的重要部分,添加標註都可以提升清晰度。這使您可以輕鬆地將注意力集中在圖像中的特定點上。在本文中,您將學習如何在 Java 中添加圖像標註。以程式化方式添加標註成為提升圖像清晰度和自動化的一個強大工具。
本文涵蓋以下主題:
Java API 添加圖像標註
Aspose.Drawing for Java 是一個強大的庫,可以以程式化方式添加圖像標註。它使您可以輕鬆地創建和編輯圖形。Aspose.Drawing for Java 使您可以直接在圖像上繪製形狀、線條和文字。讓我們探索如何有效地使用這個庫在您的 Java 應用程序中添加圖像標註。
請下載 API 的 JAR 文件 或在基於 Maven 的 Java 應用程序中添加以下 pom.xml 配置。
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-drawing</artifactId>
<version>24.4</version>
</dependency>
</dependencies>
在 Java 中添加標註到圖像
請按照以下簡單步驟以程式化方式向圖像添加標註:
- 使用 Image.fromFile() 方法加載圖像。
- 使用 fromImage() 方法創建 Graphics 對象。
- 初始化 Pen 類對象,設置所需的顏色和大小來繪製標註。
- 定義標註元素的位置,設置指定的顏色和大小。
- 使用 drawString() 方法寫入文本。
- 使用 save() 方法保存輸出圖像。
以下代碼示例演示了 如何在 Java 中向圖像添加標註。
import com.aspose.drawing.Color; | |
import com.aspose.drawing.Font; | |
import com.aspose.drawing.FontStyle; | |
import com.aspose.drawing.Graphics; | |
import com.aspose.drawing.GraphicsUnit; | |
import com.aspose.drawing.Image; | |
import com.aspose.drawing.Pen; | |
import com.aspose.drawing.PointF; | |
import com.aspose.drawing.RectangleF; | |
import com.aspose.drawing.SizeF; | |
import com.aspose.drawing.SolidBrush; | |
import com.aspose.drawing.text.TextRenderingHint; |
public static void main(String[] args) { | |
// Load an image | |
Image image = Image.fromFile("gears.png"); | |
Graphics graphics = Graphics.fromImage(image); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
graphics.setPageUnit(GraphicsUnit.Pixel); | |
drawCallOut(graphics, new PointF(107, 55), new PointF(179, 5), 74, "mm"); | |
drawCallOut(graphics, new PointF(111, 146), new PointF(29, 180), 28, "mm"); | |
image.save("gears_callout.jpg"); | |
} | |
private static void drawCallOut(Graphics graphic, PointF startAnchor, PointF endAnchor, int value, String unit) | |
{ | |
Pen pen = new Pen(Color.getRed(), 1); | |
Font font = new Font("Arial", 10, FontStyle.Bold); | |
String outputValue = value + " " + unit; | |
SizeF textSize = graphic.measureString(outputValue, font); | |
int diameterSymbolSize = 12; | |
int spaceSize = 3; | |
textSize.setWidth(textSize.getWidth() + diameterSymbolSize + spaceSize); | |
float callOutMiddleX = endAnchor.getX() > startAnchor.getX() ? endAnchor.getX() - textSize.getWidth() : endAnchor.getX() + textSize.getWidth(); | |
float callOutMiddleY = endAnchor.getY() > startAnchor.getY() ? endAnchor.getY() - textSize.getHeight() : endAnchor.getY() + textSize.getHeight(); | |
graphic.drawLine(pen, startAnchor.getX(), startAnchor.getY(), callOutMiddleX, callOutMiddleY); | |
float textAnchorX = Math.min(callOutMiddleX, endAnchor.getX()); | |
float textAnchorY = callOutMiddleY; | |
graphic.drawLine(pen, callOutMiddleX, callOutMiddleY, | |
textAnchorX == callOutMiddleX ? textAnchorX + textSize.getWidth() : textAnchorX, | |
callOutMiddleY); | |
graphic.drawEllipse(pen, new RectangleF((int)textAnchorX + spaceSize, | |
(int)(textAnchorY - textSize.getHeight()) + spaceSize, 10, 10)); | |
graphic.drawLine(pen, (int)textAnchorX + 1, (int)textAnchorY - 1, | |
(int)textAnchorX + diameterSymbolSize + 2, | |
(int)textAnchorY - diameterSymbolSize - 2); | |
SolidBrush brush = new SolidBrush(Color.getBlue()); | |
graphic.drawString(outputValue, font, brush, (int)textAnchorX + diameterSymbolSize + spaceSize, | |
(int)(textAnchorY - textSize.getHeight())); | |
} |

在 Java 中添加標註到圖像
圖像標註 – 免費資源
探索庫的各種功能,除了向照片和圖像添加標註外,還可以使用以下資源:
獲取免費臨時許可證 體驗 Aspose.Drawing for Java 的全部功能。這使您可以在決定購買之前評估庫的功能和能力。
結論
在這篇文章中,我們學習了 如何在 Java 中添加圖像標註。添加圖像標註可以通過突出顯示關鍵細節來提升您的視覺效果。按照概述的步驟和提供的代碼示例輕鬆地將注意力集中在圖像中的關鍵元素上。如果有任何疑問,請隨時在我們的 免費支持論壇 聯繫我們。