熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

java.imageIo給圖片添加水印的實現代碼

2022-06-13   來源: JSP教程 
最近項目在做一個商城項目 項目上的圖片要添加水印①添加圖片水印;②:添加文字水印;一下提供下個方法希望大家可以用得著   復制代碼 代碼如下:
package comblogsimage;
import javaawtAlphaComposite;
import javaawtColor;
import javaawtFont;
import javaawtGraphicsD;
import javaawtImage;
import javaawtRenderingHints;
import javaawtimageBufferedImage;
import javaioFile;
import javaioFileOutputStream;
import javaioInputStream;
import javaioOutputStream;
import javaximageioImageIO;
import javaxswingImageIcon;
/**
 * 圖片水印
 */
public class ImageUtil {
    /**
     * @param args
     */
    public static void main(String[] args) {
        String srcImgPath = "e:/png";
        String iconPath = "e:logojpg";
        String targerPath = "e:/jpg";
        // 給圖片添加水印
        ImageUtilwaterMarkImageByIcon(iconPath srcImgPath targerPath
f);
        // 給圖片添加水印水印旋轉
        // ImageMarkLogoByIconmarkImageByIcon(iconPath srcImgPath
        // targerPath );
    }
    /**
     * 給圖片添加水印可設置水印圖片旋轉角度
     *
     * @param iconPath
     *            水印圖片路徑
     * @param srcImgPath
     *            源圖片路徑
     * @param targerPath
     *            目標圖片路徑
     * @param degree
     *            水印圖片旋轉角度
     * @param width
     *            寬度(與左相比)
     * @param height
     *            高度(與頂相比)
     * @param clarity
     *            透明度(小於的數)越接近越透明
     */
    public static void waterMarkImageByIcon(String iconPath String srcImgPath
            String targerPath Integer degree Integer width Integer height
            float clarity) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIOread(new File(srcImgPath));
            Systemoutprintln("width:" + srcImggetWidth(null));
            Systemoutprintln("height:" + srcImggetHeight(null));
            BufferedImage buffImg = new BufferedImage(srcImggetWidth(null)
                    srcImggetHeight(null) BufferedImageTYPE_INT_RGB);
            // 得到畫筆對象
            // Graphics g= buffImggetGraphics();
            GraphicsD g = buffImgcreateGraphics();
            // 設置對線段的鋸齒狀邊緣處理
            gsetRenderingHint(RenderingHintsKEY_INTERPOLATION
                    RenderingHintsVALUE_INTERPOLATION_BILINEAR);
            gdrawImage(
                    srcImggetScaledInstance(srcImggetWidth(null)
                            srcImggetHeight(null) ImageSCALE_SMOOTH)
                    null);
            if (null != degree) {
                // 設置水印旋轉
                grotate(MathtoRadians(degree)
                        (double) buffImggetWidth() /
                        (double) buffImggetHeight() / );
            }
            // 水印圖象的路徑 水印一般為gif或者png的這樣可設置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);
            // 得到Image對象
            Image img = imgIcongetImage();
            float alpha = clarity; // 透明度
            gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP
                    alpha));
            // 表示水印圖片的位置
            gdrawImage(img width height null);
            gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_OVER));
            gdispose();
            os = new FileOutputStream(targerPath);
            // 生成圖片
            ImageIOwrite(buffImg "JPG" os);
            Systemoutprintln("添加水印圖片完成!");
        } catch (Exception e) {
            eprintStackTrace();
        } finally {
            try {
                if (null != os)
                    osclose();
            } catch (Exception e) {
                eprintStackTrace();
            }
        }
    }
    /**
     * 給圖片添加水印可設置水印圖片旋轉角度
     *
     * @param logoText
     *            水印文字
     * @param srcImgPath
     *            源圖片路徑
     * @param targerPath
     *            目標圖片路徑
     * @param degree
     *            水印圖片旋轉角度
     * @param width
     *            寬度(與左相比)
     * @param height
     *            高度(與頂相比)
     * @param clarity
     *            透明度(小於的數)越接近越透明
     */
    public static void waterMarkByText(String logoText String srcImgPath
            String targerPath Integer degree Integer width Integer height
            Float clarity) {
        // 主圖片的路徑
        InputStream is = null;
        OutputStream os = null;
        try {
            Image srcImg = ImageIOread(new File(srcImgPath));
            BufferedImage buffImg = new BufferedImage(srcImggetWidth(null)
                    srcImggetHeight(null) BufferedImageTYPE_INT_RGB);
            // 得到畫筆對象
            // Graphics g= buffImggetGraphics();
            GraphicsD g = buffImgcreateGraphics();
            // 設置對線段的鋸齒狀邊緣處理
            gsetRenderingHint(RenderingHintsKEY_INTERPOLATION
                    RenderingHintsVALUE_INTERPOLATION_BILINEAR);
            gdrawImage(
                    srcImggetScaledInstance(srcImggetWidth(null)
                            srcImggetHeight(null) ImageSCALE_SMOOTH)
                    null);
            if (null != degree) {
                // 設置水印旋轉
                grotate(MathtoRadians(degree)
                        (double) buffImggetWidth() /
                        (double) buffImggetHeight() / );
            }
            // 設置顏色
            gsetColor(Colorred);
            // 設置 Font
            gsetFont(new Font("宋體" FontBOLD ));
            float alpha = clarity;
            gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP
                    alpha));
            // 第一參數>設置的內容後面兩個參數>文字在圖片上的坐標位置(xy)
            gdrawString(logoText width height);
            gdispose();
            os = new FileOutputStream(targerPath);
            // 生成圖片
            ImageIOwrite(buffImg "JPG" os);
            Systemoutprintln("添加水印文字完成!");
        } catch (Exception e) {
            eprintStackTrace();
        } finally {
            try {
                if (null != is)
                    isclose();
            } catch (Exception e) {
                eprintStackTrace();
            }
            try {
                if (null != os)
                    osclose();
            } catch (Exception e) {
                eprintStackTrace();
            }
        }
    }
}

  
還有一個圖片縮放代碼:

復制代碼 代碼如下:

  
/**
     * 圖片縮放(圖片等比例縮放為指定大小空白部分以白色填充)
     *
     * @param srcPath
     *            源圖片路徑
     * @param destPath
     *            縮放後圖片路徑
     */
    public static void zoomImage(String srcPath String destPath int destHeight int destWidth) {
        try {
            BufferedImage srcBufferedImage = ImageIOread(new File(srcPath));
            int imgWidth = destWidth;
            int imgHeight = destHeight;
            int srcWidth = srcBufferedImagegetWidth();
            int srcHeight = srcBufferedImagegetHeight();
            if (srcHeight >= srcWidth) {
                imgWidth = (int) Mathround(((destHeight * / srcHeight) * srcWidth));
            } else {
                imgHeight = (int) Mathround(((destWidth * / srcWidth) * srcHeight));
            }
            BufferedImage destBufferedImage = new BufferedImage(destWidth destHeight BufferedImageTYPE_INT_RGB);
            GraphicsD graphicsD = destBufferedImagecreateGraphics();
            graphicsDsetBackground(ColorWHITE);
            graphicsDclearRect( destWidth destHeight);
            graphicsDdrawImage(srcBufferedImagegetScaledInstance(imgWidth imgHeight ImageSCALE_SMOOTH) (destWidth / ) (imgWidth / ) (destHeight / ) (imgHeight / ) null);
            graphicsDdispose();
            ImageIOwrite(destBufferedImage "JPEG" new File(destPath));
        } catch (IOException e) {
            eprintStackTrace();
        }
    }


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