国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java 圖片加水印源碼

2019-11-17 04:03:21
字體:
來源:轉載
供稿:網友
第一種是添加圖片水印和文字水印兩種方法,水印圖片可以是GIF,PNG透明的文件,我一般采用的是PNG的,因為它的質量和GIF相比要高一些:

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 TestCenter {
    public TestCenter() {

    }

    /**//*
     * 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);
            System.out.println("file: "+_file.toString()+">>>>"+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);
            //水印文件結束
            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(Color.RED);
            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) {
        pressImage("D://123.gif","D://aa.gif", 0, 0);
        //pressText("劉若英,我很好,后來...","D://aa.gif", "宋體", 16, 10, 16, 200, 300);
    }
}


第二種是添加文字水印:


import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;

public class TestCenter {
    /** *//**
     * 給圖片添加水印
     *
     * @param filePath
     *            需要添加水印的圖片的路徑
     * @param markContent
     *            水印的文字
     * @param markContentColor
     *            水印文字的顏色
     * @param qualNum
     *            圖片質量
     * @return
     */
    public boolean createMark(String filePath, String markContent,
            Color markContentColor, float qualNum) {
        ImageIcon imgIcon = new ImageIcon(filePath);
        Image theImg = imgIcon.getImage();
        int width = theImg.getWidth(null);
        int height = theImg.getHeight(null);
        BufferedImage bimage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bimage.createGraphics();
        g.setColor(markContentColor);
        g.setBackground(Color.white);
        g.drawImage(theImg, 0, 0, null);
        g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和設置水印文字出現的內容
        g.dispose();
        try {
            FileOutputStream out = new FileOutputStream(filePath);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
            param.setQuality(qualNum, true);
            encoder.encode(bimage, param);
            out.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    
    public static void main(String[] args)
{
     TestCenter tc = new TestCenter();
     tc.createMark("D://123.gif", "水印的文字", Color.red, 1);
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 咸宁市| 鱼台县| 涟源市| 深泽县| 饶平县| 海兴县| 新乡市| 上犹县| 丰宁| 黄浦区| 驻马店市| 印江| 庆元县| 晋州市| 平邑县| 罗甸县| 闽侯县| 田林县| 施秉县| 宁远县| 明溪县| 农安县| 民乐县| 许昌市| 永平县| 文山县| 蒙城县| 岳阳市| 霍邱县| 东平县| 连州市| 黔东| 镇原县| 池州市| 龙口市| 禹州市| 夏河县| 宜春市| 潞西市| 子长县| 湾仔区|