图片处理降低失真

之前写过关于图片生成缩略图的东西,后来发现压缩效果不是太好,已经变形;研究了并收索代码用以下处理方法极大提高图片质量

package util.info.piccut;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;

import util.info.ImageUtil;

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

public class ZoomImage {

	public static void ZoomTheImage(String fileUrl, String newUrl, int width,
			int height) {
		java.io.File file = new java.io.File(fileUrl); // 读入刚才上传的文件
		Image src = null;
		try {
			src = javax.imageio.ImageIO.read(file);
			// 构造Image对象
			BufferedImage tag = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			// tag.getGraphics().drawImage(src, 0, 0, width, height, null); //
			// 绘制缩小后的图
			tag.getGraphics().drawImage(
					src.getScaledInstance(width, height, Image.SCALE_SMOOTH),
					0, 0, null);
			FileOutputStream newimage = new FileOutputStream(newUrl); // 输出到文件流
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
			JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
			jep.setQuality((float) 1.0, true);
			encoder.encode(tag, jep);
			newimage.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		ZoomImage zoomImage = new ZoomImage();
		zoomImage
				.ZoomTheImage(
						"E:\\Workspaces\\qhedu\\WebRoot\\test\\temp\\5ccd8264-e38c-4c8a-8fac-f5bf180e4d38.jpg",
						"E:\\Workspaces\\qhedu\\WebRoot\\test\\temp\\5ccd8264-e38c-4c8a-8fac-f5bf180e4d382.jpg",
						252, 189);
	}
}
顺便再来个裁剪的代码,由网上代码修改而来

package util.info;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

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

/**
 * 图像裁剪以及压缩处理工具类
 * 
 * 提供基于JDK Image I/O 的解决方案(JDK探索失败)
 * 
 * 
 * @author
 * @since 1.0
 */
public class ImageUtil {

	// 输出打印日志
	public static boolean IS_DEBUG = true;

	/**
	 * 
	 * @param srcImg
	 *            原图片路径
	 * @param destImg
	 *            输出图片路径
	 * @param left
	 *            左边距
	 * @param top
	 *            上边距
	 * @param width
	 *            截剪宽度
	 * @param height
	 *            截剪高度
	 * @return
	 * @throws IOException
	 */
	public static boolean cutImage(String srcImg, String destImg, int left,
			int top, Integer width, Integer height) throws IOException {
		if (destImg == null || destImg.trim().length() == 0) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:输出图片路径[" + destImg + "]错误。。。");
			}
			return false;
		}
		File file = new File(srcImg);
		if (file == null || file.exists() == false || file.isFile() == false) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:[" + srcImg + "]文件不存在。。。");
			}
			return false;
		}
		return cutImage(javax.imageio.ImageIO.read(file), destImg, left, top,
				width, height);
	}

	/**
	 * 
	 * @param input
	 *            原图片输入流
	 * @param destImg
	 *            输出图片路径
	 * @param left
	 *            左边距
	 * @param top
	 *            上边距
	 * @param width
	 *            截剪宽度
	 * @param height
	 *            截剪高度
	 * @return
	 * @throws IOException
	 */
	public static boolean cutImage(InputStream input, String destImg, int left,
			int top, Integer width, Integer height) throws IOException {
		if (destImg == null || destImg.trim().length() == 0) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:输出图片路径[" + destImg + "]错误。。。");
			}
			return false;
		}
		if (input == null) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:输入流为空。。。");
			}
			return false;
		}
		return cutImage(javax.imageio.ImageIO.read(input), destImg, left, top,
				width, height);
	}

	/**
	 * 
	 * @param imginput
	 *            原图片输入流
	 * @param destImg
	 *            输出图片路径
	 * @param left
	 *            左边距
	 * @param top
	 *            上边距
	 * @param width
	 *            截剪宽度
	 * @param height
	 *            截剪高度
	 * @return
	 * @throws IOException
	 */
	public static boolean cutImage(ImageInputStream imginput, String destImg,
			int left, int top, Integer width, Integer height)
			throws IOException {
		if (destImg == null || destImg.trim().length() == 0) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:输出图片路径[" + destImg + "]错误。。。");
			}
			return false;
		}
		if (imginput == null) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:图片输入流为空。。。");
			}
			return false;
		}
		return cutImage(javax.imageio.ImageIO.read(imginput), destImg, left,
				top, width, height);
	}

	public static boolean cutImage(Image srcImage, String destImg, int left,
			int top, Integer width, Integer height) throws IOException {
		if (destImg == null || destImg.trim().length() == 0) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:输出图片路径[" + destImg + "]错误。。。");
			}
			return false;
		}
		if (srcImage == null) {
			if (IS_DEBUG) {
				System.err.println("图片截剪:源图不是有效的图片。。。");
			}
			return false;
		}
		StringBuffer sb = null;
		boolean params_error = false;
		if (IS_DEBUG) {
			sb = new StringBuffer("图片截剪:");
		}
		int src_w = srcImage.getWidth(null); // 源图宽
		int src_h = srcImage.getHeight(null);// 源图高

		if (left < 0 || left >= src_w) {
			if (IS_DEBUG) {
				sb.append("左边距超出原图有效宽度! ");
			}
			params_error = true;
		}
		if (top < 0 || top >= src_h) {
			if (IS_DEBUG) {
				sb.append("上边距超出原图有效高度! ");
			}
			params_error = true;
		}
		if (width != null && width <= 0) {
			if (IS_DEBUG) {
				sb.append("截剪宽度不能小于或等于 0 ! ");
			}
			params_error = true;
		}
		if (height != null && height <= 0) {
			if (IS_DEBUG) {
				sb.append("截剪高度不能小于或等于 0 ! ");
			}
			params_error = true;
		}
		if (params_error) {
			if (IS_DEBUG) {
				System.err.println(sb.toString());
			}
			return false;
		}

		// 目标图片宽
		if (width == null || width > src_w || width + left > src_w) {
			width = src_w - left;
		}
		// 目标图片高
		if (height == null || height > src_h || height + top > src_h) {
			height = src_h - top;
		}
		// 目标图片
		ImageFilter cropFilter = new CropImageFilter(left, top, width, height);
		Image cutImage = Toolkit.getDefaultToolkit().createImage(
				new FilteredImageSource(srcImage.getSource(), cropFilter));
		// 重绘图片
		BufferedImage tag = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(
				cutImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
				0, 0, null); // 绘制缩小后的图
		g.dispose();
		// 输出为文件
		FileOutputStream out = new FileOutputStream(destImg);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
		jep.setQuality((float) 1.0, true);
		encoder.encode(tag, jep);
		out.close();
		if (IS_DEBUG) {
			System.out.println("图片截剪:原图片宽高为[" + src_w + " × " + src_h
					+ "],输出图片的宽高为[" + width + " × " + height + "].");
		}
		return true;
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		ImageUtil
				.cutImage(
						ImageIO.read(new File(
								"E:\\Workspaces\\qhedu\\WebRoot\\test\\temp\\5ccd8264-e38c-4c8a-8fac-f5bf180e4d382.jpg")),
						"D:\\t2.jpg", 15, 20, 110, 150);
	}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值