import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import org.apache.commons.io.IOUtils;
import org.apache.commons.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.ram.RamConstants;
/**
* 阿里巴巴 simpleimage 图片处理工具类 功能 等比例缩放 裁切 加水印 复合操作
*
* @author 13643
*
*/
public class ImageUtils {
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:\\img\\src.jpg", "D:\\img\\scaleNormal.jpg", 720,
// 1080);
// 2.等比例缩放加水印
// scaleWithWaterMark("D:\\img\\src.jpg",
// "D:\\img\\scaleWithWaterMark.jpg",720, 1080);
// 3.缩放到指定宽度
// scaleWithWidth("D:\\img\\src.jpg", "D:\\img\\scaleWithWidth.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);
}
/**
*
* @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