Java?生成透明圖片的設(shè)置實(shí)現(xiàn)demo
設(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); } /\*\* \* 動(dòng)態(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()); // 獲取整個(gè)str用了font樣式的寬度這里用四舍五入后+1保證寬度絕對(duì)能容納這個(gè)字符串作為圖片的寬度 int width = (int) Math.round(r.getWidth()) + 1; // 把單個(gè)字符的高度+3保證高度絕對(duì)能容納字符串作為圖片的高度 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)容上整個(gè)的寬高 int[] arr = getWidthAndHeight(text, font); // 設(shè)置背景寬高 int width = arr[0]; int height = arr[1]; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE\_4BYTE\_ABGR); // 獲取圖形上下文對(duì)象 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)建圖片輸出流對(duì)象,基于文件對(duì)象 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); // 對(duì)當(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); // 通過(guò)RGB三分量來(lái)判斷當(dāng)前顏色是否在指定的顏色區(qū)間內(nèi) if (red >= colorRange && green >= colorRange && blue >= colorRange) { return true; } return false; }
以上就是Java 生成透明圖片設(shè)置及實(shí)現(xiàn)示例,更多關(guān)于Java 生成透明圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在SpringBoot項(xiàng)目中使用Java8函數(shù)式接口的方法示例
在Spring Boot項(xiàng)目中,Java 8 的函數(shù)式接口廣泛用于實(shí)現(xiàn)各種功能,如自定義配置、數(shù)據(jù)處理等,函數(shù)式接口在Spring Boot中非常有用,本文展示了在SpringBoot項(xiàng)目中使用Java8的函數(shù)式接口的方法示例,需要的朋友可以參考下2024-03-03SpringBoot集成drools的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成drools的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Java Graphics實(shí)現(xiàn)界面顯示文字并換行
Java中Graphics類提供了一些基本的幾何圖形繪制方法,本文將利用Graphics實(shí)現(xiàn)界面顯示文字并換行效果,感興趣的小伙伴可以動(dòng)手嘗試一下2022-08-08Java設(shè)計(jì)模塊系列之書店管理系統(tǒng)單機(jī)版(三)
這篇文章主要為大家詳細(xì)介紹了Java單機(jī)版的書店管理系統(tǒng)設(shè)計(jì)模塊和思想第三章,感興趣的小伙伴們可以參考一下2016-08-08Java中語(yǔ)音url轉(zhuǎn)換成InputStream的示例代碼
在Java中,可以使用java.net.URL和java.net.URLConnection類來(lái)將語(yǔ)音URL轉(zhuǎn)換為InputStream,本文通過(guò)示例代碼介紹Java中語(yǔ)音url轉(zhuǎn)換成InputStream的相關(guān)知識(shí),感興趣的朋友一起看看吧2024-01-01Java設(shè)計(jì)模式之橋模式(Bridge模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之橋模式(Bridge模式)介紹,本文講解了為什么使用橋模式、如何實(shí)現(xiàn)橋模式、Bridge模式在EJB中的應(yīng)用等內(nèi)容,需要的朋友可以參考下2015-03-03關(guān)于SpringBoot中事務(wù)失效的幾種情況
這篇文章主要介紹了關(guān)于SpringBoot中事務(wù)失效的幾種情況,Spring AOP默認(rèn)使用動(dòng)態(tài)代理,會(huì)給被代理的類生成一個(gè)代理類,事務(wù)相關(guān)的操作都通過(guò)代理來(lái)完成,使用內(nèi)部方法調(diào)用時(shí),使用的是實(shí)例調(diào)用,沒(méi)有通過(guò)代理類調(diào)用方法,因此事務(wù)不會(huì)檢測(cè)到失敗,需要的朋友可以參考下2023-08-08詳解IDEA使用Maven項(xiàng)目不能加入本地Jar包的解決方法
這篇文章主要介紹了詳解IDEA使用Maven項(xiàng)目不能加入本地Jar包的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08