zoukankan      html  css  js  c++  java
  • java image

    package com.lindows.util;
    
    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.JPEGImageEncoder;
    import com.sun.image.codec.jpeg.JPEGCodec;
    
    public class ImageProcess {
    
    	/**
    	 * @param processImg
    	 *            水印文件,最好用gif或者png可以支持透明
    	 * @param oldImg
    	 *            原始图片文件
    	 * @param newImg
    	 *            生成图片文件
    	 * @param x
    	 *            水印的横坐标
    	 * @param y
    	 *            水印的纵坐标
    	 */
    	public static final void processImg(String processImg, String oldImg,
    			String newImg, int x, int y) {
    
    		try {
    			// 目标文件
    			File file = new File(oldImg);
    			Image oldImage = ImageIO.read(file);
    			int width = oldImage.getWidth(null);
    			int height = oldImage.getHeight(null);
    			BufferedImage image = new BufferedImage(width, height,
    					BufferedImage.TYPE_INT_RGB);
    			Graphics g = image.createGraphics();
    			g.drawImage(oldImage, 0, 0, width, height, null);
    			// 水印文件
    			File file2 = new File(processImg);
    			Image addImg = ImageIO.read(file2);
    			int width1 = addImg.getWidth(null);
    			int height1 = addImg.getHeight(null);
    			g.drawImage(addImg, width - width1 - x, height - height1 - y,
    					width1, height1, null);
    			// g.drawImage(addImg, (width - width1) / 2 - x, (height - height1)/
    			// 2 - y, null);
    			// 水印结束
    			g.dispose();
    			FileOutputStream out = new FileOutputStream(newImg);
    			JPEGImageEncoder encorder = JPEGCodec.createJPEGEncoder(out);
    			encorder.encode(image);
    			out.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void main(String[] args) {
    		// 测试方法
    		ImageProcess.processImg("G:/png.png", "G:/1.png", "G:/3.jpg", 0, 0);
    	}
    }
    
     
  • 相关阅读:
    Python数据可视化---pygal模块
    Linux常用命令---常用的用户,解压,网络,关机命令
    Python实战---制作专属有声小说(调用百度语音合成接口)
    Linux基本操作---文件搜索命令
    MySQL必知必会1-20章读书笔记
    这是反馈的地方呀
    设计模式--建造者模式
    python 弹窗提示警告框MessageBox
    算法分析设计--递归算法
    Web程序开发最基本的编程模式--MVC编程模式
  • 原文地址:https://www.cnblogs.com/lindows/p/14390642.html
Copyright © 2011-2022 走看看