<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    lexy

    支持開源,尊重他人的勞動(dòng)!

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      17 隨筆 :: 0 文章 :: 9 評(píng)論 :: 0 Trackbacks

    貼一段 最近寫的用來(lái) 給 給定目錄下的圖片 添加水印的代碼。

    WaterMark.java:


    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    import com.sun.image.codec.jpeg.JPEGCodec;

    import javax.imageio.ImageIO;
    import java.io.File;
    import java.io.IOException;
    import java.io.FilenameFilter;
    import java.io.FileOutputStream;
    import java.util.Properties;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.util.List;
    import java.util.Arrays;
    import java.util.ArrayList;

    public class WaterMark {

    ??? private List<File> filterFiles(String path, FilenameFilter filter, List<File> fileList) {
    ??????? if (new File(path).exists() && new File(path).isDirectory()) {
    ??????????? File[] files = new File(path).listFiles(filter);
    ??????????? fileList.addAll(Arrays.asList(files));

    ??????????? File[] allFiles = new File(path).listFiles();
    ??????????? for (File file : allFiles) {
    ??????????????? if (file.isDirectory())
    ??????????????????? filterFiles(file.getPath(), filter, fileList);
    ??????????? }
    ??????? }
    ??????? return fileList;
    ??? }

    ??? private void drawWaterMark(File srcPicture, File logoPicture, String destPath, float logoPercent) {
    ??????? try {
    ??????????? Image src = ImageIO.read(srcPicture);
    ??????????? int width = src.getWidth(null);
    ??????????? int height = src.getHeight(null);
    ??????????? BufferedImage image = new BufferedImage(width, height,
    ??????????????????? BufferedImage.TYPE_INT_RGB);
    ??????????? Graphics g = image.createGraphics();
    ??????????? g.drawImage(src, 0, 0, width, height, null);

    ??????????? Image src_logo = ImageIO.read(logoPicture);

    ??????????? int width_src_logo = src_logo.getWidth(null);
    ??????????? int heigth_src_logo = src_logo.getHeight(null);

    ??????????? int width_logo = (int) (width * logoPercent);
    ??????????? int height_logo = heigth_src_logo * width_logo / width_src_logo;

    ??????????? g.drawImage(src_logo, (width - width_logo) / 2, (height - height_logo) / 2,
    ??????????????????? width_logo, height_logo, null);
    ??????????? g.dispose();
    ??????????? FileOutputStream out = new FileOutputStream(destPath + "\\" + srcPicture.getName());
    ??????????? JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    ??????????? encoder.encode(image);
    ??????????? out.close();
    ??????? } catch (IOException e) {
    ??????????? e.printStackTrace();
    ??????? }
    ??? }

    ??? public void draw() {
    ??????? try {
    ??????????? Properties props = new Properties();
    ??????????? props.load(WaterMark.class.getClassLoader().getResourceAsStream("system.properties"));
    ??????????? FFilter filter = new FFilter(props.getProperty("extensions.pictures"));
    ??????????? List<File> files = new ArrayList<File>();
    ??????????? String srcPath = props.getProperty("path.pictures.src");
    ??????????? files = filterFiles(srcPath, filter, files);
    ??????????? File logoPic = new File(props.getProperty("path.picture.logo"));
    ??????????? String destPath = props.getProperty("path.pictures.dest");
    ??????????? float logoSize = Float.valueOf(props.getProperty("percent.picture.logo"));
    ??????????? for (File pic : files) {
    ??????????????? String path = pic.getPath().substring(srcPath.length());
    ??????????????? path = path.substring(0, path.indexOf(pic.getName()));
    ??????????????? String newPath = destPath + path;
    ??????????????? if (!new File(newPath).exists())
    ??????????????????? new File(newPath).mkdirs();
    ??????????????? drawWaterMark(pic, logoPic, newPath, logoSize);
    ??????????? }
    ??????? } catch (Exception e) {
    ??????????? e.printStackTrace();
    ??????? }
    ??? }

    ??? public static void main(String[] args) {
    ??????? new WaterMark().draw();
    ??? }
    }

    FFilter.java :

    import java.io.FilenameFilter;
    import java.io.File;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;

    public class FFilter implements FilenameFilter {
    ??? private String regEx = "";

    ??? public FFilter(String regEx) {
    ??????? this.regEx = regEx;
    ??? }

    ??? public boolean accept(File dir, String filename) {
    ??????? Pattern p = Pattern.compile(regEx);
    ??????? Matcher m = p.matcher(filename);
    ??????? return m.find();
    ??? }
    }

    system.properties:

    #source pictures directory
    path.pictures.src=C:\\Documents and Settings\\Lexy\\Desktop\\testPic
    #destination pictures directory
    path.pictures.dest=C:\\Documents and Settings\\Lexy\\Desktop\\destTestPicDest
    #picture's extensions,space by "|"
    extensions.pictures=.jpg
    #path of the watermark logo
    path.picture.logo=C:\\Documents and Settings\\Lexy\\Desktop\\logo.png
    #the watermark percent of the pictures
    percent.picture.logo=0.5

    posted on 2008-04-01 17:28 lexy 閱讀(703) 評(píng)論(1)  編輯  收藏

    評(píng)論

    # re: 給圖片添加水印的JAVA實(shí)現(xiàn) 2008-04-12 10:49 豆抓搜索
    找了好長(zhǎng)時(shí)間了,哈謝啦.http://www.douzhua.com  回復(fù)  更多評(píng)論
      


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费的一级黄色片| 中文字幕视频免费| 四虎国产精品免费久久影院| 亚洲av无码专区青青草原| 四虎永久在线精品免费网址| 亚洲伊人色一综合网| 最近2019中文字幕免费大全5| 亚洲丁香色婷婷综合欲色啪| 免费不卡在线观看AV| 亚洲性天天干天天摸| 亚洲视频在线免费看| 亚洲伊人久久大香线蕉结合| 在线观看人成视频免费| 日韩成人精品日本亚洲| 亚洲欧洲日产国码一级毛片| 国产一级黄片儿免费看| 亚洲美女免费视频| 成年女人毛片免费观看97| 国产在亚洲线视频观看| 在线精品亚洲一区二区三区| 国产真人无码作爱视频免费| 亚洲精品在线免费看| 四虎成人免费观看在线网址| 一区二区三区免费在线观看| 久久精品国产96精品亚洲| 最近高清中文字幕免费| 亚洲欧美国产日韩av野草社区| 全亚洲最新黄色特级网站 | 亚洲老熟女五十路老熟女bbw| 国内自产少妇自拍区免费| 产传媒61国产免费| 亚洲成色在线综合网站| 少妇高潮太爽了在线观看免费| 亚洲av无码专区在线电影天堂| 久久亚洲av无码精品浪潮| 最近免费字幕中文大全视频| 偷自拍亚洲视频在线观看99| 亚洲一区二区三区夜色| 国产成人免费片在线观看| 国产成人久久AV免费| 毛片亚洲AV无码精品国产午夜|