Java?生成透明圖片的設(shè)置實現(xiàn)demo
更新時間:2023年02月08日 09:41:47 作者:IT小馬哥
這篇文章主要為大家介紹了Java?生成透明圖片的設(shè)置實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
設(shè)置圖片透明
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import javax.swing.\*; import java.awt.\*; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /\*\* \* 圖片處理工具類 \* \* @author maruifu \*/ public class ImageUtils1 { private static final Logger log = LoggerFactory.getLogger(ImageUtils.class); public static void main(String[] args) { String text = "我是小馬哥"; String filePath = "/Users/maruifu/Desktop/1.png"; //生成指定文字透明圖片 createImage(text,filePath,"宋體",45); //設(shè)置指定圖片透明 //setColorInRange(path,path); } /\*\* \* 動態(tài)獲取文字寬高 \* @param text 文字 \* @param font 字體 \* @return \*/ private static int[] getWidthAndHeight(String text, Font font) { Rectangle2D r = font.getStringBounds(text, new FontRenderContext( AffineTransform.getScaleInstance(1, 1), false, false)); int unitHeight = (int) Math.floor(r.getHeight()); // 獲取整個str用了font樣式的寬度這里用四舍五入后+1保證寬度絕對能容納這個字符串作為圖片的寬度 int width = (int) Math.round(r.getWidth()) + 1; // 把單個字符的高度+3保證高度絕對能容納字符串作為圖片的高度 int height = unitHeight + 3; return new int[]{width, height}; } /\*\* \* 生成指定文字透明圖片 \* @param text 文字內(nèi)容 "我是小馬哥" \* @param filePath 生成地址 "/Users/maruifu/Desktop/123.png" \* @param fontName 字體名稱 "宋體" \* @param fontSize 字體大小 45 \*/ public static void createImage(String text, String filePath,String fontName,int fontSize) { ImageOutputStream imageOutputStream = null; try { Font font = new Font(fontName, Font.BOLD,fontSize); // 獲取font的樣式應(yīng)用在輸出內(nèi)容上整個的寬高 int[] arr = getWidthAndHeight(text, font); // 設(shè)置背景寬高 int width = arr[0]; int height = arr[1]; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE\_4BYTE\_ABGR); // 獲取圖形上下文對象 Graphics2D graphics = (Graphics2D)image.getGraphics(); graphics.clearRect(0, 0, width, height); // 填充 graphics.fillRect(0, 0, width, height); // 設(shè)定字體大小及樣式 graphics.setFont(font); // 字體顏色 graphics.setColor(Color.BLACK); // 描繪字符串 graphics.drawString(text, 0, font.getSize()); // 圖片透明度 setTransparency(image, graphics); File jpgFile = new File(filePath); if(!jpgFile.exists()) { jpgFile.createNewFile(); } // 創(chuàng)建圖片輸出流對象,基于文件對象 imageOutputStream = ImageIO.createImageOutputStream(jpgFile); // 寫入 ImageIO.write(image, FilenameUtils.getExtension(filePath),imageOutputStream); } catch (IOException e) { log.error("生成圖片失敗{}",e); } finally { // 關(guān)閉流 IOUtils.closeQuietly(imageOutputStream); } } private static void setTransparency(BufferedImage image, Graphics2D graphics) { int alpha = 0; // 外層遍歷是Y軸的像素 for (int y = image.getMinY(); y < image.getHeight(); y++) { // 內(nèi)層遍歷是X軸的像素 for (int x = image.getMinX(); x < image.getWidth(); x++) { int rgb = image.getRGB(x, y); // 對當(dāng)前顏色判斷是否在指定區(qū)間內(nèi) if (colorInRange(rgb)) { alpha = 0; } else { // 設(shè)置為不透明 alpha = 255; } // #AARRGGBB 最前兩位為透明度 rgb = (alpha << 24) | (rgb & 0x00ffffff); image.setRGB(x, y, rgb); } } // 繪制設(shè)置了RGB的新圖片 graphics.drawImage(image, 0, 0, null); //釋放畫筆 graphics.dispose(); } /\*\* \* 設(shè)置指定圖片透明 \* @param sourcePath 源文件地址 \* @param targetPath 新文件地址 \*/ public static void setColorInRange(String sourcePath,String targetPath ) { try { BufferedImage image = ImageIO.read(new File(sourcePath)); // 高度和寬度 int height = image.getHeight(); int width = image.getWidth(); // 生產(chǎn)背景透明和內(nèi)容透明的圖片 ImageIcon imageIcon = new ImageIcon(image); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE\_4BYTE\_ABGR); // 獲取畫筆 Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); // 繪制Image的圖片 g2D.drawImage(imageIcon.getImage(), 0, 0, null); // 圖片透明 setTransparency(bufferedImage, g2D); // 生成圖片為PNG ImageIO.write(bufferedImage, FilenameUtils.getExtension(sourcePath), new File(targetPath)); } catch (IOException e) { log.error("設(shè)置圖片透明失敗{}",e); } } /\*\* \* 判斷是背景還是內(nèi)容 \* @param color \* @return \*/ public static boolean colorInRange(int color) { // 色差范圍0~255 int colorRange = 210; // 獲取color(RGB)中R位 int red = (color & 0xff0000) >> 16; // 獲取color(RGB)中G位 int green = (color & 0x00ff00) >> 8; // 獲取color(RGB)中B位 int blue = (color & 0x0000ff); // 通過RGB三分量來判斷當(dāng)前顏色是否在指定的顏色區(qū)間內(nèi) if (red >= colorRange && green >= colorRange && blue >= colorRange) { return true; } return false; }
以上就是Java 生成透明圖片設(shè)置及實現(xiàn)示例,更多關(guān)于Java 生成透明圖片的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在SpringBoot項目中使用Java8函數(shù)式接口的方法示例
在Spring Boot項目中,Java 8 的函數(shù)式接口廣泛用于實現(xiàn)各種功能,如自定義配置、數(shù)據(jù)處理等,函數(shù)式接口在Spring Boot中非常有用,本文展示了在SpringBoot項目中使用Java8的函數(shù)式接口的方法示例,需要的朋友可以參考下2024-03-03Java Graphics實現(xiàn)界面顯示文字并換行
Java中Graphics類提供了一些基本的幾何圖形繪制方法,本文將利用Graphics實現(xiàn)界面顯示文字并換行效果,感興趣的小伙伴可以動手嘗試一下2022-08-08Java設(shè)計模塊系列之書店管理系統(tǒng)單機版(三)
這篇文章主要為大家詳細(xì)介紹了Java單機版的書店管理系統(tǒng)設(shè)計模塊和思想第三章,感興趣的小伙伴們可以參考一下2016-08-08Java中語音url轉(zhuǎn)換成InputStream的示例代碼
在Java中,可以使用java.net.URL和java.net.URLConnection類來將語音URL轉(zhuǎn)換為InputStream,本文通過示例代碼介紹Java中語音url轉(zhuǎn)換成InputStream的相關(guān)知識,感興趣的朋友一起看看吧2024-01-01關(guān)于SpringBoot中事務(wù)失效的幾種情況
這篇文章主要介紹了關(guān)于SpringBoot中事務(wù)失效的幾種情況,Spring AOP默認(rèn)使用動態(tài)代理,會給被代理的類生成一個代理類,事務(wù)相關(guān)的操作都通過代理來完成,使用內(nèi)部方法調(diào)用時,使用的是實例調(diào)用,沒有通過代理類調(diào)用方法,因此事務(wù)不會檢測到失敗,需要的朋友可以參考下2023-08-08詳解IDEA使用Maven項目不能加入本地Jar包的解決方法
這篇文章主要介紹了詳解IDEA使用Maven項目不能加入本地Jar包的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08