加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java实现给网站上传图片盖章的方法

发布时间:2020-12-14 14:21:43 所属栏目:Java 来源:网络整理
导读:本篇章节讲解Java实现给网站上传图片盖章的方法。供大家参考研究。具体如下: 最近无聊,上了一会校友录,觉的校友录的图片都会加入一个章,呵呵,自己也就做了一个,不过只适合jpg格式。发出来给大家研究研究。欢迎讨论! 很老的代码了 /**********

本篇章节讲解Java实现给网站上传图片盖章的方法。分享给大家供大家参考。具体如下:

最近无聊,上了一会校友录,觉的校友录的图片都会加入一个章,呵呵,自己也就做了一个,不过只适合jpg格式。发出来给大家研究研究。欢迎讨论!
很老的代码了

/************************************************
* <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 SettingsAdministrator"
          + "My DocumentsMy Pictures1.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("water_wang@xs.zj.cn",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,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程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读