图片增加水印,图片缩放,图片切割服务

#图片增加水印图

#图片水印文字

#图片原图缩放


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

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

// 图片操作,改变大小加水印
public class ImageOperate {
	public static void main(String[] args) throws IOException {
		ImageOperate.waterMark("/Users/kevin/Downloads/test1.jpg", "/Users/kevin/Downloads/test.png");
		ImageOperate.waterMarkTxt("/Users/kevin/Downloads/test1.jpg", "http://blog.csdn.net/kevin_luan");
		ImageOperate.zoomImage("/Users/kevin/Downloads/test1.jpg", 300, 200);
	}

	/**
	 * 给图片加水印,但不改变大小
	 * 
	 * @param strOriginalFileName
	 *            原始文件
	 * @param strWaterMarkFileName
	 *            水印
	 */
	public static void waterMark(String originalPath, String waterMarkPath) {
		try {
			File fileOriginal = new File(originalPath);
			Image imageOriginal = ImageIO.read(fileOriginal);
			int widthOriginal = imageOriginal.getWidth(null);
			int heightOriginal = imageOriginal.getHeight(null);
			BufferedImage bufImage = new BufferedImage(widthOriginal, heightOriginal, BufferedImage.TYPE_INT_RGB);
			Graphics g = bufImage.createGraphics();
			g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, null);

			// 水印文件
			File fileWaterMark = new File(waterMarkPath);
			Image imageWaterMark = ImageIO.read(fileWaterMark);
			int widthWaterMark = imageWaterMark.getWidth(null);
			int heightWaterMark = imageWaterMark.getHeight(null);

			// 水印文件在源文件的右下角
			g.drawImage(imageWaterMark, widthOriginal - widthWaterMark, heightOriginal - heightWaterMark,
					widthWaterMark, heightWaterMark, null);
			g.dispose();

			FileOutputStream fos = new FileOutputStream(originalPath);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
			encoder.encode(bufImage);
			fos.flush();
			fos.close();
			fos = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 给图片加水印文字
	 * 
	 * @param strOriginalFileName
	 *            原始文件
	 * @param strWaterMarkFileName
	 *            水印
	 */
	public static void waterMarkTxt(String originalPath, String data) {
		try {
			File fileOriginal = new File(originalPath);
			Image imageOriginal = ImageIO.read(fileOriginal);
			int widthOriginal = imageOriginal.getWidth(null);
			int heightOriginal = imageOriginal.getHeight(null);
			BufferedImage bufImage = new BufferedImage(widthOriginal, heightOriginal, BufferedImage.TYPE_INT_RGB);
			Graphics g = bufImage.createGraphics();
			g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, null);

			g.setColor(Color.red);
			g.setFont(new Font("宋体", Font.BOLD, 20));
			g.drawString(data, (int) (widthOriginal / 2), heightOriginal - 20);
			g.dispose();

			FileOutputStream fos = new FileOutputStream(originalPath);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
			encoder.encode(bufImage);
			fos.flush();
			fos.close();
			fos = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 缩放图片
	 * 
	 * @throws IOException
	 */
	public static String zoomImage(String srcImgFile, int width, int height) throws IOException {
		java.io.File file = new java.io.File(srcImgFile);
		int splitIndex = srcImgFile.indexOf(".");
		String path = srcImgFile.substring(0, splitIndex) + width + "_" + height + srcImgFile.substring(splitIndex);
		Image src = javax.imageio.ImageIO.read(file);
		java.awt.image.BufferedImage bufferedImage = new java.awt.image.BufferedImage(width, height,
				java.awt.image.BufferedImage.TYPE_INT_RGB);
		bufferedImage.getGraphics().drawImage(src, 0, 0, width, height, null);
		FileOutputStream newimage = new FileOutputStream(path);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
		encoder.encode(bufferedImage); // 近JPEG编码
		newimage.close();
		return path;
	}

	
}

#基于Spring MVN搭建一个简单版的图片切割服务

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 切割图片服务
 * 
 * @author KEVIN LUAN
 *
 */
@Controller
public class ImageScaleController {
	/**
	 * 切割图片 访问URL:
	 * http://localhost:8080/h5/scale_110x111?imgUrl=http://www.mdtit
	 * .com/frontend/images/slider1.jpg
	 * 
	 * @param response
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param imgUrl
	 *            切割图URL
	 * @throws IOException
	 */
	@RequestMapping(value = "/scale_{width}x{height}")
	public void scale(HttpServletResponse response, @PathVariable int width, @PathVariable int height, String imgUrl)
			throws IOException {
		BufferedImage image = ImageIO.read(new URL(imgUrl));
		image = scale(image, width, height);
		response.setDateHeader("Expires", System.currentTimeMillis() + 2592000000L);
		response.setContentType("image/png");
		ImageIO.write(image, "png", response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}

	/**
	 * 裁剪图片
	 * 
	 * @param image
	 * @param x
	 * @param y
	 * @return
	 */
	private BufferedImage scale(BufferedImage image, int x, int y) {
		int height = y;
		int width = x;
		if (height == 0) {
			height = (int) (width / getXYRatioOf(image));
		} else if (height < 0) {
			height = -height * width;
			width = (int) (height / getXYRatioOf(image));
		}
		if (needResize(image, width, height)) {
			if (needCut(image, width, height)) {
				image = cutImage(image, width, height);
			}
			if (needResize(image, width, height)) {
				image = resizeImage(image, width, height);
			}
		}
		return image;
	}

	BufferedImage resizeImage(BufferedImage image, int width, int height) {
		int y = (int) ((1.0f * image.getHeight() * width) / image.getWidth());
		Image result = image.getScaledInstance(width, y, BufferedImage.SCALE_DEFAULT);
		if (result instanceof BufferedImage) {
			return (BufferedImage) result;
		} else {
			BufferedImage bi = new BufferedImage(width, y, BufferedImage.TYPE_INT_RGB);
			Graphics graphics = bi.getGraphics();
			graphics.drawImage(result, 0, 0, null);
			graphics.dispose();
			return bi;
		}
	}

	boolean needResize(BufferedImage image, int maxX, int maxY) {
		return image.getWidth() > maxX || image.getHeight() > maxY * 3;
	}

	BufferedImage cutImage(BufferedImage image, int wantX, int wantY) {
		int width = image.getWidth();
		int height = image.getHeight();
		int startX = 0;
		int startY = 0;

		float wantRatio = getRatio(wantX, wantY);
		float imageRatio = getXYRatioOf(image);

		if (isTooHigh(imageRatio, wantRatio)) {
			height = (int) (width / wantRatio);
			startY = (image.getHeight() - height) / 2;
		} else {
			width = (int) (height * wantRatio);
			if (width < wantX) {
				width = wantX;
			}
			startX = (image.getWidth() - width) / 2;
		}
		return image.getSubimage(startX, startY, width, height);
	}

	boolean isTooHigh(float realRatio, float wantRatio) {
		return wantRatio > realRatio;
	}

	boolean needCut(BufferedImage image, int wantX, int wantY) {
		float ratio = getXYRatioOf(image);
		float wantRatio = getRatio(wantX, wantY);
		return wantRatio > ratio + 0.01f || wantRatio < ratio - 0.01f;
	}

	private float getXYRatioOf(BufferedImage image) {
		return getRatio(image.getWidth(), image.getHeight());
	}

	private float getRatio(int x, int y) {
		return ((float) x) / y;
	}
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值