需要的jar
jai_codec-1.1.3.jar,jai_core-1.1.3.jar,simpleimage-1.2.0.jar,metadata-extractor-2.3.1.jar(旋转时的jar)
//图片工具类
package com.javajy.util;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import mons.io.IOUtils;
import mons.lang.StringUtils;
import com.alibaba.simpleimage.ImageFormat;
import com.alibaba.simpleimage.ImageWrapper;
import com.alibaba.simpleimage.SimpleImageException;
import com.alibaba.simpleimage.render.CropParameter;
import com.alibaba.simpleimage.render.ScaleParameter;
import com.alibaba.simpleimage.render.WatermarkParameter;
import com.alibaba.simpleimage.render.WriteParameter;
import com.alibaba.simpleimage.render.ScaleParameter.Algorithm;
import com.alibaba.simpleimage.util.ImageCropHelper;
import com.alibaba.simpleimage.util.ImageDrawHelper;
import com.alibaba.simpleimage.util.ImageReadHelper;
import com.alibaba.simpleimage.util.ImageScaleHelper;
import com.alibaba.simpleimage.util.ImageWriteHelper;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectory;
import com.sun.image.codec.jpeg.*;
public class YasuoUtil {
/* @SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
String path = resizeFix("D:\\workspace/frshop/src/main/webapp/image/123.jpg", "D:\\workspace/frshop/src/main/webapp/image/456.jpg");
System.out.println(path);
System.out.println(path.substring(path.lastIndexOf("/") + 1));
System.out.println("结束:" + new Date().toLocaleString());
}*/
//--------------方法一----------------------
/**
* @param fileName 原图片路径
* @param postPath 存放的压缩图片路径
* @return
* @throws IOException
*/
public static String resizeFix(String fileName, String postPath) throws IOException {
String path = "";
//此处是用来处理上传图片旋转
int angel = getRotateAngleForPhoto(fileName);//旋转图片角度
fileName = rotatePhonePhoto(fileName, angel); //获取旋转后的图片
File file = new File(fileName);// 读入服务器文件
Image img = ImageIO.read(file); // 构造Image对象
int width = img.getWidth(null); // 得到源图宽
int height = img.getHeight(null); // 得到源图长
//把长跟宽等比缩小,大于1000就压缩
if (width > 1000 || height > 1000) {
int w = 500;
int h = 500;
if ((width >= 1000 && width < 3000) || (height >= 1000 && height < 3000)) {
w = (int) (width / 2);//w int 新宽度
h = (int) (height / 2);//h int 新高度
} else if ((width >= 3000 && width < 5000) || (height >= 3000 && height < 5000)) {
w = (int) (width / 3);//w int 新宽度
h = (int) (height / 3);//h int 新高度
} else if ((width >= 5000) || (height >= 5000)) {
w = (int) (width / 5);//w int 新宽度
h = (int) (height / 5);//h int 新高度
}
resize(img, w, h, postPath);//源宽度或长度大于1000的时候进行压缩图片
path = postPath;
} else {//图片宽高小于1000的时候显示原图
path = fileName;//路径
}
return path;
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w int 新宽度
* @param h int 新高度
*/
public static void resize(Image img, int w, int h, String postPath) throws IOException {
String path = "";
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
// File destFile = new File("D:\\项目/链焙/图片/654.png");
File destFile = new File(postPath);
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image); // JPEG编码
out.close();
}
//----------------------------以下为阿里压缩 多种类型 方法二-------------------
/**
* 阿里巴巴 simpleimage 图片处理工具类 功能 等比例缩放 裁切 加水印 复合操作
*
*/
// public static String WATER_IMAGE_URL = "D:\\img\\watermark.png";//打水印
protected static ImageFormat outputFormat = ImageFormat.JPEG;
/* public static void main(String[] args) {
// 1.等比例缩放
scaleNormal("D:\\workspace/frshop/src/main/webapp/image/123.jpg", "D:\\workspace/frshop/src/main/webapp/image/456.jpg");
// 2.等比例缩放加水印
// scaleWithWaterMark("D:\\img\\src.jpg",
// "D:\\img\\scaleWithWaterMark.jpg",720, 1080);
// 3.缩放到指定宽度
// scaleWithWidth("D:\\123.png", "D:\\456.jpg",400);
// 4.缩放到指定高度
// scaleWithHeight("D:\\img\\src.jpg", "D:\\img\\scaleWithHeight.jpg",
// 600);
// 5.裁切成正方形
// Cut("D:\\img\\src.jpg", "D:\\img\\cut.jpg");
// 6.从中间裁切
// CutCenter("D:\\img\\src.jpg", "D:\\img\\cutCenter.jpg", 600,800);
// print(40,20, 4,2);
// print(20,40, 4,2);
// print(40,20, 4,3);
}*/
//等比例压缩(不剪裁)
public final static String scaleNormal(String src, String target) {
String path="";
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
int angel = getRotateAngleForPhoto(src);//此处是用来处理上传图片旋转
src = rotatePhonePhoto(src, angel);
File in = new File(src); // 原图片路径
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();//原图宽
int h = imageWrapper.getHeight();//原图高
int width = 720;//新宽度
int height = 1080;//新高度
int cw=w, ch=h,x=0,y=0;
boolean isDeal=true;
if(height>h || width>w){
isDeal=false;
}else if((w - width)>(h-height)){
ch=h;
cw=(h*width)/height;
x=(w-cw)/2;
if(cw>h){
cw=w;
ch=(w*height)/width;
y=(h-ch)/2;
x=0;
}
}else if((w - width)<=(h-height)){
cw=w;
ch=(w*height)/width;
y=(h-ch)/2;
if(ch>h){
ch=h;
cw=(h*width)/height;
x=(w-cw)/2;
y=0;
}
}
System.out.println("x: "+x+" y" +y+"cw: "+cw+" ch"+ch+"");
if(isDeal){
// CropParameter cropParam = new CropParameter(x, y, cw, ch);// 裁切参数
// PlanarImage planrImage = ImageCropHelper.crop(imageWrapper.getAsPlanarImage(), cropParam);
// ScaleParameter scaleParam = new ScaleParameter(width, height, Algorithm.LANCZOS); // 缩放参数
// planrImage = ImageScaleHelper.scale(planrImage, scaleParam);
// imageWrapper = new ImageWrapper(planrImage);
// 1.缩放
ScaleParameter scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS); // 缩放参数
if (w < width) {// 如果图片宽和高都小于目标图片则不做缩放处理
scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS);
} else {
int newHeight = getHeight(w, h, width);
scaleParam = new ScaleParameter(width, newHeight + 1, Algorithm.LANCZOS);
}
PlanarImage planrImage = ImageScaleHelper.scale(imageWrapper.getAsPlanarImage(), scaleParam);
imageWrapper = new ImageWrapper(planrImage);
}
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
if(!isDeal){
path = src;
}else {
path = target;
}
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
System.out.println("原图-------"+src+"压缩--------"+target);
return path;
}
/**
*
* @param pInput
* @param pImgeFlag
* @return
* @throws Exception
*/
public static boolean isPicture(String pInput, String pImgeFlag) throws Exception {
if (StringUtils.isBlank(pInput)) {
return false;
}
String tmpName = pInput.substring(pInput.lastIndexOf(".") + 1, pInput.length());
String imgeArray[][] = {{"bmp", "0"}, {"dib", "1"}, {"gif", "2"}, {"jfif", "3"}, {"jpe", "4"}, {"jpeg", "5"}, {"jpg", "6"}, {"png", "7"}, {"tif", "8"},
{"tiff", "9"}, {"ico", "10"}};
for (int i = 0; i < imgeArray.length; i++) {
if (!StringUtils.isBlank(pImgeFlag) && imgeArray[i][0].equals(tmpName.toLowerCase()) && imgeArray[i][1].equals(pImgeFlag)) {
return true;
}
if (StringUtils.isBlank(pImgeFlag) && imgeArray[i][0].equals(tmpName.toLowerCase())) {
return true;
}
}
return false;
}
/**
* 等比例缩放 会裁切部分内容
*
* @param src
* @param target
* @param width
* @param height
*/
@SuppressWarnings("static-access")
public final static void scale(String src, String target, int width, int height) {
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
File in = new File(src); // 原图片
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();
int h = imageWrapper.getHeight();
float w1= 0f, h1 = 0f;
float sp = (float) w / h;
float rp = (float) width / height;
if (sp > rp) {
w1 = (width * h) / (float)w;
h1 = width;
} else if(sp<rp){
h1 = (height * w) /(float) h;
w1 = width;
}else{
w1=width;
h1=height;
}
// 1.缩放
ScaleParameter scaleParam = new ScaleParameter((int)w1, (int)h1, Algorithm.LANCZOS); // 缩放参数
PlanarImage planrImage = ImageScaleHelper.scale(imageWrapper.getAsPlanarImage(), scaleParam);
imageWrapper = new ImageWrapper(planrImage);
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
}
/**
* 压缩图片到 指定尺寸,图片比目标图片小则不会变形(有水印)
*
* @param src
* @param target
* @param width
* @param height
*/
public final static void scaleWithWaterMark(String src, String target, int width, int height) {
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
File in = new File(src); // 原图片
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();
int h = imageWrapper.getHeight();
int cw=w, ch=h,x=0,y=0;
boolean isDeal=true;
if(height>h||width>w){
isDeal=false;
}else if((w - width)>(h-height)){
ch=h;
cw=(h*width)/height;
x=(w-cw)/2;
if(cw>h){
cw=w;
ch=(w*height)/width;
y=(h-ch)/2;
x=0;
}
}else if((w - width)<=(h-height)){
cw=w;
ch=(w*height)/width;
y=(h-ch)/2;
if(ch>h){
ch=h;
cw=(h*width)/height;
x=(w-cw)/2;
y=0;
}
}
System.out.println("x: "+x+" y" +y+"cw: "+cw+" ch"+ch+"");
if(isDeal){
CropParameter cropParam = new CropParameter(x, y, cw, ch);// 裁切参数
PlanarImage planrImage = ImageCropHelper.crop(imageWrapper.getAsPlanarImage(), cropParam);
ScaleParameter scaleParam = new ScaleParameter(width, height, Algorithm.LANCZOS); // 缩放参数
planrImage = ImageScaleHelper.scale(planrImage, scaleParam);
imageWrapper = new ImageWrapper(planrImage);
}
// 3.打水印
// BufferedImage waterImage = ImageIO.read(new File(WATER_IMAGE_URL));
BufferedImage waterImage = ImageIO.read(new File("D:\\img\\watermark.png"));
ImageWrapper waterWrapper = new ImageWrapper(waterImage);
Point p =calculate(imageWrapper.getWidth(),imageWrapper.getHeight(),
waterWrapper.getWidth(), waterWrapper.getHeight());
WatermarkParameter param = new WatermarkParameter(waterWrapper, 1f,(int) p.getX(),(int) p.getY());
BufferedImage bufferedImage = ImageDrawHelper.drawWatermark(imageWrapper.getAsBufferedImage(), param);
imageWrapper = new ImageWrapper(bufferedImage);
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
}
/**
* 缩放到指定宽度 高度自适应
*
* @param src
* @param target
* @param width
*/
@SuppressWarnings("static-access")
public final static void scaleWithWidth(String src, String target, Integer width) {
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
File in = new File(src); // 原图片
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();
int h = imageWrapper.getHeight();
// 1.缩放
ScaleParameter scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS); // 缩放参数
if (w < width) {// 如果图片宽和高都小于目标图片则不做缩放处理
scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS);
} else {
int newHeight = getHeight(w, h, width);
scaleParam = new ScaleParameter(width, newHeight + 1, Algorithm.LANCZOS);
}
PlanarImage planrImage = ImageScaleHelper.scale(imageWrapper.getAsPlanarImage(), scaleParam);
imageWrapper = new ImageWrapper(planrImage);
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
}
/**
* 缩放到指定高度,宽度自适应
*
* @param src
* @param target
* @param height
*/
public final static void scaleWithHeight(String src, String target, Integer height) {
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
File in = new File(src); // 原图片
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();
int h = imageWrapper.getHeight();
// 1.缩放
ScaleParameter scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS); // 缩放参数
if (w < height) {// 如果图片宽和高都小于目标图片则不做缩放处理
scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS);
} else {
int newWidth = getWidth(w, h, height);
scaleParam = new ScaleParameter(newWidth + 1, height, Algorithm.LANCZOS);
}
PlanarImage planrImage = ImageScaleHelper.scale(imageWrapper.getAsPlanarImage(), scaleParam);
imageWrapper = new ImageWrapper(planrImage);
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
}
/**
* 根据宽、高和目标宽度 等比例求高度
*
* @param w
* @param h
* @param width
* @return
*/
public static Integer getHeight(Integer w, Integer h, Integer width) {
return h * width / w;
}
/**
* 根据宽、高和目标高度 等比例求宽度
*
* @param w
* @param h
* @param height
* @return
*/
public static Integer getWidth(Integer w, Integer h, Integer height) {
return w * height / h;
}
/**
* 从中间裁切需要的大小
*
* @param src
* @param target
* @param width
* @param height
*/
@SuppressWarnings("static-access")
public final static void CutCenter(String src, String target, Integer width, Integer height) {
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
File in = new File(src); // 原图片
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();
int h = imageWrapper.getHeight();
int x = (w - width) / 2;
int y = (h - height) / 2;
CropParameter cropParam = new CropParameter(x, y, width, height);// 裁切参数
if (x < 0 || y < 0) {
cropParam = new CropParameter(0, 0, w, h);// 裁切参数
}
PlanarImage planrImage = ImageCropHelper.crop(imageWrapper.getAsPlanarImage(), cropParam);
imageWrapper = new ImageWrapper(planrImage);
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
}
/**
* 裁切 为正文形
*
* @param src
* @param target
*/
public final static void Cut(String src, String target) {
File out = new File(target); // 目的图片
FileOutputStream outStream = null;
File in = new File(src); // 原图片
FileInputStream inStream = null;
try {
inStream = new FileInputStream(in);
ImageWrapper imageWrapper = ImageReadHelper.read(inStream);
int w = imageWrapper.getWidth();
int h = imageWrapper.getHeight();
int width = 0;
int height = 0;
if (w >= h) {
width = h;
height = h;
} else {
width = w;
height = w;
}
CropParameter cropParam = new CropParameter(0, 0, width, height);// 裁切参数
PlanarImage planrImage = ImageCropHelper.crop(imageWrapper.getAsPlanarImage(), cropParam);
imageWrapper = new ImageWrapper(planrImage);
// 4.输出
outStream = new FileOutputStream(out);
String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
ImageWriteHelper.write(imageWrapper, outStream, outputFormat.getImageFormat(prefix), new WriteParameter());
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
} finally {
IOUtils.closeQuietly(inStream); // 图片文件输入输出流必须记得关闭
IOUtils.closeQuietly(outStream);
}
}
public static Point calculate(int enclosingWidth, int enclosingHeight,
int width, int height)
{
int x = (enclosingWidth / 2) - (width / 2);
int y = (enclosingHeight / 2) - (height / 2);
return new Point(x, y);
}
//--------------------------------------以下是调整原图竖拍旋转问题----------------------------------------
/**
* 上传图片旋转问题
*
* 1.首先获取图片正确显示需要旋转的角度(顺时针)
* @return
*/
public static int getRotateAngleForPhoto(String filePath){
File file = new File(filePath);
int angle = 0;
Metadata metadata;
try {
metadata = JpegMetadataReader.readMetadata(file);
Directory directory = metadata.getDirectory(ExifDirectory.class);
if(directory.containsTag(ExifDirectory.TAG_ORIENTATION)){
// Exif信息中方向
int orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION);
// 原图片的方向信息
if(6 == orientation ){
//6旋转90
angle = 90;
}else if( 3 == orientation){
//3旋转180
angle = 180;
}else if( 8 == orientation){
//8旋转90
angle = 270;
}
}
} catch (JpegProcessingException e) {
e.printStackTrace();
} catch (MetadataException e) {
e.printStackTrace();
}
return angle;
}
/**
* 2.调用 旋转的手机照片
* @param fullPath //原图路径
* @param angel //传入1.
* @return
*/
public static String rotatePhonePhoto(String filePath, int angel){
if(angel == 0){
return filePath;
}
BufferedImage src;
try {
src = ImageIO.read(new File(filePath));
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);
BufferedImage res = new BufferedImage(rect_des.width, rect_des.height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
ImageIO.write(res, "jpg", new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
// if angel is greater than 90 degree, we need to do some conversion
if (angel >= 90) {
if(angel / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
}