Java如何添加水印

這篇文章將為大家詳細講解有關Java如何添加水印,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、海晏網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5開發(fā)商城建設、集團公司官網(wǎng)建設、外貿(mào)營銷網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為海晏等各大城市提供網(wǎng)站開發(fā)制作服務。

JAVA/JSP在圖片中添加文字或在圖片中添加圖片.

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public final class ImageUtils {
   public ImageUtils() {

}

/**//*
    * public final static String getPressImgPath() { return ApplicationContext
    * .getRealPath("/template/data/util/shuiyin.gif"); }
    */

/** *//**
    * 把圖片印刷到圖片上
    *
    * @param pressImg --
    *            水印文件
    * @param targetImg --
    *            目標文件
    * @param x
    *            --x坐標
    * @param y
    *            --y坐標
    */
   public final static void pressImage(String pressImg, String targetImg,
           int x, int y) {
       try {
           //目標文件
           File _file = new File(targetImg);
           Image src = ImageIO.read(_file);
           int wideth = src.getWidth(null);
           int height = src.getHeight(null);
           BufferedImage image = new BufferedImage(wideth, height,
                   BufferedImage.TYPE_INT_RGB);
           Graphics g = image.createGraphics();
           g.drawImage(src, 0, 0, wideth, height, null);

//水印文件
           File _filebiao = new File(pressImg);
           Image src_biao = ImageIO.read(_filebiao);
           int wideth_biao = src_biao.getWidth(null);
           int height_biao = src_biao.getHeight(null);
           g.drawImage(src_biao, (wideth - wideth_biao) / 2,
                   (height - height_biao) / 2, wideth_biao, height_biao, null);
           //水印文件結(jié)束
           g.dispose();
           FileOutputStream out = new FileOutputStream(targetImg);
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
           encoder.encode(image);
           out.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

/** *//**
    * 打印文字水印圖片
    *
    * @param pressText
    *            --文字
    * @param targetImg --
    *            目標圖片
    * @param fontName --
    *            字體名
    * @param fontStyle --
    *            字體樣式
    * @param color --
    *            字體顏色
    * @param fontSize --
    *            字體大小
    * @param x --
    *            偏移量
    * @param y
    */

public static void pressText(String pressText, String targetImg,
           String fontName, int fontStyle, int color, int fontSize, int x,
           int y) {
       try {
           File _file = new File(targetImg);
           Image src = ImageIO.read(_file);
           int wideth = src.getWidth(null);
           int height = src.getHeight(null);
           BufferedImage image = new BufferedImage(wideth, height,
                   BufferedImage.TYPE_INT_RGB);
           Graphics g = image.createGraphics();
           g.drawImage(src, 0, 0, wideth, height, null);
           // String s="www.qhd.com.cn";
           g.setColor(new Color(color,false));
           g.setFont(new Font(fontName, fontStyle, fontSize));

g.drawString(pressText, wideth - fontSize - x, height - fontSize
                   / 2 - y);
           g.dispose();
           FileOutputStream out = new FileOutputStream(targetImg);
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
           encoder.encode(image);
           out.close();
       } catch (Exception e) {
           System.out.println(e);
       }
   }

public static void main(String[] args) {
    pressText("李白杜","e:1.jpg","font-weight", Font.BOLD,255,70,700, 100);
     pressImage("e:/2.png",          "e:/1.jpg", 0, 0);

}
}

關于“Java如何添加水印”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

分享標題:Java如何添加水印
瀏覽地址:http://muchs.cn/article6/ispdig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站導航、移動網(wǎng)站建設、網(wǎng)站策劃、外貿(mào)建站、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司