日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java图片透明化处理_java的图片背景透明及透明度处理

發(fā)布時間:2023/12/31 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java图片透明化处理_java的图片背景透明及透明度处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如題,以下為通過java實現(xiàn)的針對圖片的背景透明及透明度處理,供大家需要時參考:

/**

* 設(shè)置源圖片為背景透明,并設(shè)置透明度

* @param srcFile 源圖片

* @param desFile 目標(biāo)文件

* @param alpha 透明度

* @param formatName 文件格式

* @throws IOException

*/

public static void transparentImage(String srcFile,String desFile,int alpha,String formatName) throws IOException{

BufferedImage temp = ImageIO.read(new File(srcFile));//取得圖片

transparentImage(temp, desFile, alpha, formatName);

}

/**

* 設(shè)置源圖片為背景透明,并設(shè)置透明度

* @param srcImage 源圖片

* @param desFile 目標(biāo)文件

* @param alpha 透明度

* @param formatName 文件格式

* @throws IOException

*/

public static void transparentImage(BufferedImage srcImage,

String desFile, int alpha, String formatName) throws IOException {

int imgHeight = srcImage.getHeight();//取得圖片的長和寬

int imgWidth = srcImage.getWidth();

int c = srcImage.getRGB(3, 3);

//防止越位

if (alpha < 0) {

alpha = 0;

} else if (alpha > 10) {

alpha = 10;

}

BufferedImage bi = new BufferedImage(imgWidth, imgHeight,

BufferedImage.TYPE_4BYTE_ABGR);//新建一個類型支持透明的BufferedImage

for(int i = 0; i < imgWidth; ++i)//把原圖片的內(nèi)容復(fù)制到新的圖片,同時把背景設(shè)為透明

{

for(int j = 0; j < imgHeight; ++j)

{

//把背景設(shè)為透明

if(srcImage.getRGB(i, j) == c){

bi.setRGB(i, j, c & 0x00ffffff);

}

//設(shè)置透明度

else{

int rgb = bi.getRGB(i, j);

rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);

bi.setRGB(i, j, rgb);

}

}

}

ImageIO.write(bi, StringUtils.isEmpty(formatName)?FORMAT_PNG:formatName, new File(desFile));

}

總結(jié)

以上是生活随笔為你收集整理的java图片透明化处理_java的图片背景透明及透明度处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。