Java?生成透明圖片的設置實現demo
更新時間:2023年02月08日 09:41:47 作者:IT小馬哥
這篇文章主要為大家介紹了Java?生成透明圖片的設置實現demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
設置圖片透明
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);
//設置指定圖片透明
//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 文字內容 "我是小馬哥"
\* @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的樣式應用在輸出內容上整個的寬高
int[] arr = getWidthAndHeight(text, font);
// 設置背景寬高
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);
// 設定字體大小及樣式
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 {
// 關閉流
IOUtils.closeQuietly(imageOutputStream);
}
}
private static void setTransparency(BufferedImage image, Graphics2D graphics) {
int alpha = 0;
// 外層遍歷是Y軸的像素
for (int y = image.getMinY(); y < image.getHeight(); y++) {
// 內層遍歷是X軸的像素
for (int x = image.getMinX(); x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
// 對當前顏色判斷是否在指定區(qū)間內
if (colorInRange(rgb)) {
alpha = 0;
} else {
// 設置為不透明
alpha = 255;
}
// #AARRGGBB 最前兩位為透明度
rgb = (alpha << 24) | (rgb & 0x00ffffff);
image.setRGB(x, y, rgb);
}
}
// 繪制設置了RGB的新圖片
graphics.drawImage(image, 0, 0, null);
//釋放畫筆
graphics.dispose();
}
/\*\*
\* 設置指定圖片透明
\* @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();
// 生產背景透明和內容透明的圖片
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("設置圖片透明失敗{}",e);
}
}
/\*\*
\* 判斷是背景還是內容
\* @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三分量來判斷當前顏色是否在指定的顏色區(qū)間內
if (red >= colorRange && green >= colorRange && blue >= colorRange) {
return true;
}
return false;
}以上就是Java 生成透明圖片設置及實現示例,更多關于Java 生成透明圖片的資料請關注腳本之家其它相關文章!
相關文章
在SpringBoot項目中使用Java8函數式接口的方法示例
在Spring Boot項目中,Java 8 的函數式接口廣泛用于實現各種功能,如自定義配置、數據處理等,函數式接口在Spring Boot中非常有用,本文展示了在SpringBoot項目中使用Java8的函數式接口的方法示例,需要的朋友可以參考下2024-03-03
詳解IDEA使用Maven項目不能加入本地Jar包的解決方法
這篇文章主要介紹了詳解IDEA使用Maven項目不能加入本地Jar包的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

