熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java圖片處理 文字水印 圖片水印 縮放 補白

2022-06-13   來源: Java核心技術 

  package util;

  import javaawtAlphaComposite;

  import javaawtColor;

  import javaawtFont;

  import javaawtGraphicsD;

  import javaawtImage;

  import javaawtgeomAffineTransform;

  import javaawtimageAffineTransformOp;

  import javaawtimageBufferedImage;

  import javaioFile;

  import javaioIOException;

  import javaximageioImageIO;

  /**

  * @author Eric Xu

  *

  */

  public final class ImageUtils {

  /**

  * 圖片水印

  * @param pressImg 水印圖片

  * @param targetImg 目標圖片

  * @param x 修正值 默認在中間

  * @param y 修正值 默認在中間

  * @param alpha 透明度

  */

  public final static void pressImage(String pressImg String targetImg int x int y float alpha) {

  try {

  File img = new File(targetImg);

  Image src = ImageIOread(img);

  int wideth = srcgetWidth(null);

  int height = srcgetHeight(null);

  BufferedImage image = new BufferedImage(wideth height BufferedImageTYPE_INT_RGB);

  GraphicsD g = imagecreateGraphics();

  gdrawImage(src wideth height null);

  //水印文件

  Image src_biao = ImageIOread(new File(pressImg));

  int wideth_biao = src_biaogetWidth(null);

  int height_biao = src_biaogetHeight(null);

  gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP alpha));

  gdrawImage(src_biao (wideth wideth_biao) / (height height_biao) / wideth_biao height_biao null);

  //水印文件結束

  gdispose();

  ImageIOwrite((BufferedImage) image jpg img);

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  /**

  * 文字水印

  * @param pressText 水印文字

  * @param targetImg 目標圖片

  * @param fontName 字體名稱

  * @param fontStyle 字體樣式

  * @param color 字體顏色

  * @param fontSize 字體大小

  * @param x 修正值

  * @param y 修正值

  * @param alpha 透明度

  */

  public static void pressText(String pressText String targetImg String fontName int fontStyle Color color int fontSize int x int y float alpha) {

  try {

  File img = new File(targetImg);

  Image src = ImageIOread(img);

  int width = srcgetWidth(null);

  int height = srcgetHeight(null);

  BufferedImage image = new BufferedImage(width height BufferedImageTYPE_INT_RGB);

  GraphicsD g = imagecreateGraphics();

  gdrawImage(src width height null);

  gsetColor(color);

  gsetFont(new Font(fontName fontStyle fontSize));

  gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP alpha));

  gdrawString(pressText (width (getLength(pressText) * fontSize)) / + x (height fontSize) / + y);

  gdispose();

  ImageIOwrite((BufferedImage) image jpg img);

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  /**

  * 縮放

  * @param filePath 圖片路徑

  * @param height 高度

  * @param width 寬度

  * @param bb 比例不對時是否需要補白

  */

  public static void resize(String filePath int height int width boolean bb) {

  try {

  double ratio = ; //縮放比例

  File f = new File(filePath);

  BufferedImage bi = ImageIOread(f);

  Image itemp = bigetScaledInstance(width height biSCALE_SMOOTH);

  //計算比例

  if ((bigetHeight() > height) || (bigetWidth() > width)) {

  if (bigetHeight() > bigetWidth()) {

  ratio = (new Integer(height))doubleValue() / bigetHeight();

  } else {

  ratio = (new Integer(width))doubleValue() / bigetWidth();

  }

  AffineTransformOp op = new AffineTransformOp(AffineTransformgetScaleInstance(ratio ratio) null);

  itemp = opfilter(bi null);

  }

  if (bb) {

  BufferedImage image = new BufferedImage(width height BufferedImageTYPE_INT_RGB);

  GraphicsD g = imagecreateGraphics();

  gsetColor(Colorwhite);

  gfillRect( width height);

  if (width == itempgetWidth(null))

  gdrawImage(itemp (height itempgetHeight(null)) / itempgetWidth(null) itempgetHeight(null) Colorwhite null);

  else

  gdrawImage(itemp (width itempgetWidth(null)) / itempgetWidth(null) itempgetHeight(null) Colorwhite null);

  gdispose();

  itemp = image;

  }

  ImageIOwrite((BufferedImage) itemp jpg f);

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  public static void main(String[] args) throws IOException {

  pressImage(D:\\shuiyinpng D:\\testjpg f);

  pressText(我是文字水印 D:\\testjpg 微軟雅黑 Colorwhite f);

  resize(D:\\testjpg true);

  }

  public static int getLength(String text) {

  int length = ;

  for (int i = ; i < textlength(); i++) {

  if (new String(textcharAt(i) + )getBytes()length > ) {

  length += ;

  } else {

  length += ;

  }

  }

  return length / ;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26874.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.