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);
    	}
    }
    
     
  • 相关阅读:
    Css3 常见鼠标滑过效果集合
    HTML5 Media事件
    HTML 5 Audio/Video DOM buffered 属性
    Cocos2d-x 3.X 事件分发机制
    在 WPF 程序中使用 MVVM 模式
    Windows Phone 版 Cocos2d-x 程序的结构
    转载:Cocos2D-x 游戏接入 Windows 设备所需做的六件事
    使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境
    转载:Windows Phone 8.1 投影我的屏幕使用教程
    NHibernate 中使用 nvarchar(max) 类型
  • 原文地址:https://www.cnblogs.com/lindows/p/14390642.html
Copyright © 2011-2022 走看看