
我現在有一個需求,就是給一張圖片,給一段文字,然后生成像上面圖片這樣的效果,文字很清晰,可以自動換行,并且文字有一個灰色透明的背景,然后可以設定文字顯示的位置(頂部、垂直居中、底部)、設定文字大小顏色,請問像這種效果要怎么實作呢?
uj5u.com熱心網友回復:
影像處理可以用opencv,lz自己谷歌百度一下opencv的包試試uj5u.com熱心網友回復:
如下代碼可以給圖片加水印。可以指定位置、文字字體(宋體、黑體之類)、文字是否加粗、文字顏色等。至于你說的灰色透明背景,可以再單獨在文字上疊加圖片。我這里有原來用的文字水印的方法供你參考。效果如下:
示例代碼:
public final static void pressText(String pressText,
String srcImageFile, String destImageFile, String fontName,
int fontStyle, Color color, int fontSize,int x,
int y, float alpha) {
try {
File img = new File(srcImageFile);
Image src = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 在指定坐標繪制水印文字
g.drawString(pressText, (width - (getLength(pressText) * fontSize))
/ 2 + x, (height - fontSize) / 2 + y);
g.dispose();
ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));// 輸出到檔案流
} catch (Exception e) {
e.printStackTrace();
}
}
呼叫示例:
pressText("我是水印文字","D:/1920.jpg","D:/1920_pressText.jpg","宋體",Font.BOLD,Color.white,80, 0, 0, 0.5f);
uj5u.com熱心網友回復:
我這個需求不能固定位置,因為原圖的高度不確定,文字還可能會比較多,還需要對文字換行。uj5u.com熱心網友回復:
2D圖片的處理,有多個庫可以使用。文字水印
1 文字生成透明背景圖片,可以指定字體,加粗等。自動換行是獲得圖片寬度,字體寬度/字間距后計算,
2 疊加圖片,
參考:
https://www.jianshu.com/p/44e09d43082e
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/133655.html
標籤:Java相關
上一篇:誰有完整的ElasticSearch7.6的demo?
下一篇:求助! Tomcat升級
