java 實(shí)現(xiàn)圖片圓角處理、背景透明化
java 圖片圓角處理、背景透明化
/**圖片圓角處理,背景透明化 * @param srcImageFile 原圖片 * @param result 處理后圖片 * @param type 圖片格式 * @param cornerRadius 720為圓角 */ public void makeRoundedCorner(File srcImageFile, File result, String type, int cornerRadius) { try { BufferedImage bi1 = ImageIO.read(srcImageFile); // 根據(jù)需要是否使用 BufferedImage.TYPE_INT_ARGB BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_INT_ARGB); Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1 .getHeight()); Graphics2D g2 = image.createGraphics(); image = g2.getDeviceConfiguration().createCompatibleImage(bi1.getWidth(), bi1.getHeight(), Transparency.TRANSLUCENT); g2 = image.createGraphics(); g2.setComposite(AlphaComposite.Clear); g2.fill(new Rectangle(image.getWidth(), image.getHeight())); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f)); g2.setClip(shape); // 使用 setRenderingHint 設(shè)置抗鋸齒 g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.fillRoundRect(0, 0,bi1.getWidth(), bi1.getHeight(), cornerRadius, cornerRadius); g2.setComposite(AlphaComposite.SrcIn); g2.drawImage(bi1, 0, 0, bi1.getWidth(), bi1.getHeight(), null); g2.dispose(); ImageIO.write(image, type, result); } catch (Exception e) { // TODO: handle exception } }
java 的圖片處理解析
直接上效果圖,現(xiàn)在有的需求就是把用戶的頭像,跟昵稱嵌入到這個(gè)背景圖中。
第一步,把頭像切成圓角,背景透明的圖片。
第二部,把第一步生成的圖片,當(dāng)成水印放到坐標(biāo)的左邊的紅箭頭的地方
第三部,創(chuàng)建文字水印,然后放入到右邊的紅箭頭地方。
效果圖如下:
由于需要thumbnailator組件支持
先導(dǎo)入maven
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
直接上代碼:
package com.image; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Point; import java.awt.Transparency; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Position; public class ImageDo { public static void main(String[] args) throws IOException { //首先獲取 makeRoundedCorner("C:/Users/luojie/Desktop/0.jpg", "C:/Users/luojie/Desktop/2.png", "png", 170); //后續(xù)水印在背景圖片的的x軸y軸的坐標(biāo) Position ab= new Position() { @Override public Point calculate(int enclosingWidth, int enclosingHeight, int width, int height, int insetLeft, int insetRight, int insetTop, int insetBottom) { // TODO Auto-generated method stub return new Point(89, 53); } }; //把生成的圓形圖片變換成寬高142x142的圖片 Thumbnails.of("C:/Users/luojie/Desktop/2.png").size(142, 142).toFile( "C:/Users/luojie/Desktop/2_142x142.png"); //把生成的圓形圖片,當(dāng)水印貼到背景圖中,ab為圓形圖片應(yīng)該到背景圖的x軸y軸的坐標(biāo) Thumbnails.of("C:/Users/luojie/Desktop/cmbg.png").size(1280, 1024).watermark(ab, ImageIO.read(new File("C:/Users/luojie/Desktop/2_142x142.png")), 1f) .outputQuality(0.8f).toFile("C:/Users/luojie/Desktop/image_watermark_bottom_right.jpg"); //給文字水印 pressText("C:/Users/luojie/Desktop/image_watermark_bottom_right.jpg", "WEIXINYONGHU", "Comic Sans MS", Font.BOLD, 30, Color.BLACK,275, 65, 1f); } /** * 添加文字水印 * @param targetImg 目標(biāo)圖片路徑,如:C://myPictrue//1.jpg * @param pressText 水印文字, 如:中國證券網(wǎng) * @param fontName 字體名稱, 如:宋體 * @param fontStyle 字體樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC) * @param fontSize 字體大小,單位為像素 * @param color 字體顏色 * @param x 水印文字距離目標(biāo)圖片左側(cè)的偏移量,如果x<0, 則在正中間 * @param y 水印文字距離目標(biāo)圖片上側(cè)的偏移量,如果y<0, 則在正中間 * @param alpha 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) */ public static void pressText(String targetImg,String pressText,String fontName,int fontStyle,int fontSize,Color color,int x,int y,float alpha){ try { File file = new File(targetImg); Image image = ImageIO.read(file); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image,0,0, width, height, null); g.setFont(new Font(fontName, fontStyle, fontSize)); g.setColor(color); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); int width_wi = fontSize*getTextLength(pressText); int height_wi = fontSize; int widthDiff = width-width_wi; int heightDiff = height-height_wi; if(x<0){ x = widthDiff/2; }else if(x>widthDiff){ x=widthDiff; } if(y<0){ y = heightDiff/2; }else if(y>heightDiff){ y = heightDiff; } g.drawString(pressText, x, y+height_wi);//水印文件 g.dispose(); ImageIO.write(bufferedImage, "JPEG", file); } catch (IOException e) { e.printStackTrace(); } } /** * 計(jì)算文字像素長度 * @param text * @return */ private static int getTextLength(String text){ int textLength = text.length(); int length = textLength; for (int i = 0; i < textLength; i++) { int wordLength = String.valueOf(text.charAt(i)).getBytes().length; if(wordLength > 1){ length+=(wordLength-1); } } return length%2==0 ? length/2:length/2+1; } /* * 圓角處理 * @param BufferedImage * @param cornerRadius * */ public static String makeRoundedCorner(String srcImageFile, String result, String type, int cornerRadius) { try { BufferedImage image = ImageIO.read(new File(srcImageFile)); // int w = image.getWidth(); // int h = image.getHeight(); int w = image.getWidth(); int h = image.getHeight(); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); output = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT); g2.dispose(); g2 = output.createGraphics(); //這里繪畫圓角矩形 // g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // g2.fillRoundRect(0, 0,w, h, cornerRadius, cornerRadius); // g2.setComposite(AlphaComposite.SrcIn); //這里繪畫原型圖 Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,w, h); g2.setClip(shape); g2.drawImage(image, 0, 0, w, h, null); g2.dispose(); ImageIO.write(output, type, new File(result)); return result; } catch (IOException e) { e.printStackTrace(); } return null; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot使用swagger-ui 2.10.5 有關(guān)版本更新帶來的問題小結(jié)
這篇文章主要介紹了springboot使用swagger-ui 2.10.5 有關(guān)版本更新帶來的問題小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12SpringBoot初始教程之Servlet、Filter、Listener配置詳解
本篇文章主要介紹了SpringBoot初始教程之Servlet、Filter、Listener配置詳解,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09springboot+mybatis-plus+oracle實(shí)現(xiàn)邏輯刪除
最近在做一個(gè)前后端分離的小項(xiàng)目,需要?jiǎng)h除用戶表的用戶,本文主要實(shí)現(xiàn)了springboot+mybatis-plus+oracle邏輯刪除,具有一定的參考價(jià)值,感興趣的可以了解一下2021-08-08Java8新特性之lambda(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
這篇文章主要介紹了Java8新特性之lambda(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)表達(dá)式的相關(guān)知識(shí),包括lambda語法方面的知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06springboot響應(yīng)json?null值過濾方式
這篇文章主要介紹了springboot響應(yīng)json?null值過濾方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):緒論
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛好者有所幫助,同時(shí)祝大家有一個(gè)好成績,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07Java中l(wèi)ist.contains()的用法及拓展
List集合相信大家在開發(fā)過程中幾乎都會(huì)用到,有時(shí)候難免會(huì)遇到集合里的數(shù)據(jù)是重復(fù)的,需要進(jìn)行去除,下面這篇文章主要給大家介紹了關(guān)于Java中l(wèi)ist.contains()的用法及拓展的相關(guān)資料,需要的朋友可以參考下2023-03-03