欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

圖片疊加效果Java代碼實(shí)現(xiàn)

 更新時(shí)間:2018年02月09日 14:19:54   作者:畢來(lái)生  
這篇文章主要為大家詳細(xì)介紹了圖片疊加效果Java代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖片疊加效果展示的具體代碼,供大家參考,具體內(nèi)容如下

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
public class NewImageUtils {
  /**
   * 
   * @Title: 構(gòu)造圖片
   * @Description: 生成水印并返回java.awt.image.BufferedImage
   * @param file
   *      源文件(圖片)
   * @param waterFile
   *      水印文件(圖片)
   * @param x
   *      距離右下角的X偏移量
   * @param y
   *      距離右下角的Y偏移量
   * @param alpha
   *      透明度, 選擇值從0.0~1.0: 完全透明~完全不透明
   * @return BufferedImage
   * @throws IOException
   */
  public static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException {
    // 獲取底圖
    BufferedImage buffImg = ImageIO.read(file);
    // 獲取層圖
    BufferedImage waterImg = ImageIO.read(waterFile);
    // 創(chuàng)建Graphics2D對(duì)象,用在底圖對(duì)象上繪圖
    Graphics2D g2d = buffImg.createGraphics();
    int waterImgWidth = waterImg.getWidth();// 獲取層圖的寬度
    int waterImgHeight = waterImg.getHeight();// 獲取層圖的高度
    // 在圖形和圖像中實(shí)現(xiàn)混合和透明效果
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
    // 繪制
    g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
    g2d.dispose();// 釋放圖形上下文使用的系統(tǒng)資源
    return buffImg;
  }

  /**
   * 輸出水印圖片
   * 
   * @param buffImg
   *      圖像加水印之后的BufferedImage對(duì)象
   * @param savePath
   *      圖像加水印之后的保存路徑
   */
  private void generateWaterFile(BufferedImage buffImg, String savePath) {
    int temp = savePath.lastIndexOf(".") + 1;
    try {
      ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }

  /**
   * 
   * @param args
   * @throws IOException
   *       IO異常直接拋出了
   * @author bls
   */
  public static void main(String[] args) throws IOException {
    String sourceFilePath = "D://img//di.png";
    String waterFilePath = "D://img//ceng.png";
    String saveFilePath = "D://img//new.png";
    NewImageUtils newImageUtils = new NewImageUtils();
    // 構(gòu)建疊加層
    BufferedImage buffImg = NewImageUtils.watermark(new File(sourceFilePath), new File(waterFilePath), 0, 0, 1.0f);
    // 輸出水印圖片
    newImageUtils.generateWaterFile(buffImg, saveFilePath);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論