一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java實現(xiàn)給網(wǎng)站上傳圖片蓋章的方法

Java實現(xiàn)給網(wǎng)站上傳圖片蓋章的方法

2019-12-29 14:22罪惡的花生 JAVA教程

這篇文章主要介紹了Java實現(xiàn)給網(wǎng)站上傳圖片蓋章的方法,涉及java針對圖片的合成操作技巧,類似水印功能,需要的朋友可以參考下

本文實例講述了Java實現(xiàn)給網(wǎng)站上傳圖片蓋章的方法。分享給大家供大家參考。具體如下:

最近無聊,上了一會校友錄,覺的校友錄的圖片都會加入一個章,呵呵,自己也就做了一個,不過只適合jpg格式。發(fā)出來給大家研究研究。歡迎討論!
很老的代碼了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/************************************************
* <p>java對圖片的操作(只能使用jpg)</p>
* 對圖片的簽章<br>
* 對圖片的縮圖<br>
* <p>Title:java對圖片的操作(只能使用jpg)</p>
* <p>CreateData: 2004-12-2</p>
* <p>Description:</p>
* <p>Copyright: Copyright (c) 2004</p>
* @author 王凱
* @version 1.0
***********************************************/
package com.cn.wangk.test;
import java.io.*;
import com.sun.image.codec.jpeg.*;//sun公司僅提供了jpg圖片文件的編碼api
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import javax.imageio.ImageIO;
/**
* @author wangkai
*/
public class Test {
  /**
   * 
   */
  public Test() {
    try {
      //生成以后新的圖片地址
      File fo = new File("c:\\4.jpg");
      //讀取的圖片文件
      String imagePath = "C:\\Documents and Settings\\Administrator"
          + "\\My Documents\\My Pictures\\1.jpg";
      //蓋章的圖片文件
      String toimagepth = "C:\\1.jpg";
      //得到圖片的文件流
      InputStream imageIn;
      imageIn = new FileInputStream(new File(imagePath));
      //得到輸入的編碼器,將文件流進行jpg格式編碼
      JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
      //得到編碼后的圖片對象
      BufferedImage image = decoder.decodeAsBufferedImage();
      Graphics g = image.getGraphics();
      try {
        InputStream imageIn2 = null;
        imageIn2 = new FileInputStream(new File(toimagepth));
        //得到輸入的編碼器,將文件流進行jpg格式編碼
        JPEGImageDecoder decoder2 = JPEGCodec
            .createJPEGDecoder(imageIn2);
        //得到編碼后的圖片對象
        BufferedImage image2 = decoder2.decodeAsBufferedImage();
        //加蓋圖片章
        ImageObserver obser = null;
        int x = image.getWidth() - image2.getWidth();
        int y = image.getHeight() - image2.getHeight();
        g.drawImage(image2, x, y, obser);
      } catch (FileNotFoundException e) {
        //打開文件失敗,表示章圖片不存在,這時候直接加蓋文件章(簽名
        g.setFont(new Font("宋體", Font.PLAIN, 18));
        g.drawString("秋水工作室", image.getWidth() - 100,
            image.getHeight() - 20);
        g.drawString("[email protected]", image.getWidth() - 180,
            image.getHeight() - 10);
      }
      g.dispose();
      ImageIO.write(image, "jpeg", fo);
      System.out.println("ok");
    } catch (FileNotFoundException e) {
      // 自動生成 catch 塊
      e.printStackTrace();
    } catch (ImageFormatException e) {
      // 自動生成 catch 塊
      e.printStackTrace();
    } catch (IOException e) {
      // 自動生成 catch 塊
      e.printStackTrace();
    }
  }
  public static void saveFixedBoundIcon(File imageFile, int height, int width)
      throws Exception {
    double Ratio = 0.0;
    if (imageFile == null || !imageFile.isFile())
      throw new Exception(imageFile + "找不到指定的文件!");
    String filePath = imageFile.getPath();
    BufferedImage Bi = ImageIO.read(imageFile);
    if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
      if (Bi.getHeight() > Bi.getWidth()) {
        Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
      } else {
        Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
      }
      File savefile = new File(filePath + "_" + height + "_" + width
          + ".jpg");
      Image Itemp = Bi.getScaledInstance(width, height,
          Image.SCALE_SMOOTH);
      AffineTransformOp op = new AffineTransformOp(AffineTransform
          .getScaleInstance(Ratio, Ratio), null);
      Itemp = op.filter(Bi, null);
      try {
        ImageIO.write((BufferedImage) Itemp, "jpeg", savefile);
      } catch (Exception ex) {
      }
    }
  }
  public static void main(String[] args) {
    //    Test ts = new Test();
    try {
      Test.saveFixedBoundIcon(new File(
          "C:\\test.jpg"), 200, 200);
    } catch (Exception e) {
      // 自動生成 catch 塊
      e.printStackTrace();
    }
  }
}

希望本文所述對大家的java程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 妹妹骑上来蹭着蹭着就射了 | 强制高h| 99ri国产在线 | 青青青国产视频 | 无人影院在线播放 | 国产99久久精品 | 免费欧美日韩 | 久久精品成人免费看 | 午夜福利试看120秒体验区 | 99撸 | 日本高清视频在线观看 | 52zfl宅福利yxpjw| 强女明星系列小说 | 欧美a在线观看 | 色多多在线观看视频 | 扒开腚眼子视频大全 | 免费高清特黄a 大片 | 精品综合久久久久久88小说 | 成全视频在线观看免费 | 加勒比一本大道在线 | 日韩毛片在线影视 | 高清麻生希在线 | 韩国三级在线高速影院 | 好男人资源大全免费观看 | 精品欧美小视频在线观看 | 91久久精品视频 | 国内精品久久久久影院男同志 | 青青草精品在线观看 | 好男人在线观看免费高清2019韩剧 | 亚洲剧情在线 | 乌克兰一级毛片 | 肥胖老寡妇做性 | 欧美一卡2卡3卡四卡海外精品 | 国产经典一区二区三区蜜芽 | 亚洲视频第一页 | 波多野结衣中文字幕乱七八糟 | 91香蕉国产在线观看人员 | 欧美在线欧美 | 久久国产精品永久免费网站 | 婷婷久久综合 | 午夜理论片日本中文在线 |