代碼片段:
/**
* 給圖片添加文字水印、圖片水印
* @param map
*/
public void markImageByText(Map<String,String> map){
System.out.println("markImageByText ---------- 開始 ---------- Start");
String srcImgPath = map.get("srcImgPath");//原圖片路徑
String newImagePath = map.get("newImgPath");//目標圖片路徑
String logText = map.get("logText");//水印文字
InputStream is = null;
OutputStream os = null;
try {
// 要添加水印的圖片
Image srcImg = ImageIO.read(new File(srcImgPath));
int width = srcImg.getWidth(null);// 原圖寬度
int height = srcImg.getHeight(null);// 原圖高度
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 得到畫筆物件
Graphics2D g = buffImg.createGraphics();
// 設定對線段的鋸齒狀邊緣處理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
// 設定水印旋轉
g.rotate(Math.toRadians(45), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
// 設定水印文字顏色
g.setColor(Color.GRAY);
// 設定水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f));
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
g.drawString(logText, i * 120 + j * 30, -i * 120 + j * 30);//畫出水印,并設定水印位置
}
}
try {
// 得到畫筆物件
Graphics2D g1 = buffImg.createGraphics();
// 水印的路徑
ClassPathResource c2 = new ClassPathResource("template/manage/watermark.jpg");
InputStream inputStream2 = c2.getInputStream();
File f2 = File.createTempFile("test", ".jpg");
try {
FileUtils.copyInputStreamToFile(inputStream2, f2);
} finally {
IOUtils.closeQuietly(inputStream2);
}
//水印圖片
ImageIcon imgIcon = new ImageIcon(f2.getPath());
// 得到Image物件。
Image syImg = imgIcon.getImage();
float alpha = 0.5f; // 透明度
int syWidth = syImg.getWidth(null);//水印的寬度
int syHeight = syImg.getHeight(null);//水印的高度
// 如果水印圖片高或寬大于目標圖片是做的處理,使水印寬或高等于目標圖片的寬高,并且等比例縮放
int newSyWidth = syWidth;
int newSyHeight = syHeight;
if (syWidth > width) {
newSyWidth = width;
newSyHeight = (int) ((double)newSyWidth / syWidth * height);
}
if (newSyHeight > height) {
newSyHeight = height;
newSyWidth = (int) ((double)newSyHeight / syHeight * newSyWidth);
}
// 根據位置引數確定坐標位置
int a = 0, b = 0;
//右上角
a = width - newSyWidth;
g1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 表示水印圖片的位置
g1.drawImage(syImg, a, b, newSyWidth, newSyHeight, null);
g.dispose();// 釋放資源
// 水印檔案結束
g1.dispose();
os = new FileOutputStream (newImagePath);
ImageIO.write (buffImg, "JPG", os);
System.out.println("markImageByText ---------- 結束 ---------- Start");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
報錯資訊:
java.lang.NullPointerException
at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
at sun.font.SunFontManager$2.run(SunFontManager.java:431)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.SunFontManager.<init>(SunFontManager.java:376)
at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
at sun.font.SunFontManager.getInstance(SunFontManager.java:250)
at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:264)
at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1107)
at javax.swing.JComponent.getFontMetrics(JComponent.java:1617)
at com.pactera.profile.service.CheckBorrowerRecordService.markImageByText(CheckBorrowerRecordService.java:535)
at com.pactera.profile.service.CheckBorrowerRecordService.checkLink(CheckBorrowerRecordService.java:416)
at com.pactera.profile.service.CheckBorrowerRecordService$$FastClassBySpringCGLIB$$29d9121.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
at com.pactera.profile.service.CheckBorrowerRecordService$$EnhancerBySpringCGLIB$$df6df947.checkLink(<generated>)
at com.pactera.profile.controller.CheckBorrowerRecordController.checkLink(CheckBorrowerRecordController.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
uj5u.com熱心網友回復:
求大神幫忙!!!uj5u.com熱心網友回復:
應該是字體問題,可能是測驗環境是Linux環境,沒有你本地Windows環境的某個字體。可以進去FontConfiguration.getVersion(FontConfiguration.java:1264)原始碼看下uj5u.com熱心網友回復:
嗯嗯 之前那個問題解決了 缺失是Linux環境缺少對應字體,現在又有一個 問題 就是 后臺把添加了文字水印和圖片水印的pdf 上傳到檔案服務器之后把路徑傳給前端 前端用vue-pdf插件顯示的時候 文字水印出不來了 這個是什么情況?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/123469.html
標籤:非技術區
上一篇:救命命!!!
下一篇:idea+activiti
