package com.sp.util;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
public class PhotoUtil {
/**
* @author wwd
* @Title: copyPhotoByOne
* @Description: 上传、修改文件
* @param photoname 修改原始图片名称
* @param savePath 保存路径
* @param photoFiles 上传或修改的文件
* @param photoName 上传或修改文件名
* @param flag 1:上传 其余为修改
* @param packages 要存储的最后一个包名
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String copyPhotoByOne( String savePath, File photoFiles, String photoName,String packages,int flag) {
String newName ="";
try {
//新文件名
newName = packages + File.separator + UUID.randomUUID() + photoName.substring(photoName.lastIndexOf("."), photoName.length());
//判断文件夹是否存在,不在创建
File uploadDir = new File(savePath+packages);
if (!uploadDir.isDirectory()) {
uploadDir.mkdirs();
}
if(flag == 1){
//新增文件
File file1 = new File(new File(savePath), newName);
FileUtils.copyFile(photoFiles, file1);
}else{
//修改文件
File file1 = new File(new File(savePath), photoName);
if (!file1.getParentFile().exists()) {
file1.getParentFile().mkdirs();
}
FileUtils.copyFile(photoFiles, file1);
// 修改文件名称
file1.renameTo(new File(savePath + newName));
}
newName = newName.replace(File.separator, "/");
} catch (IOException e) {
e.printStackTrace();
}
return newName;
}
/**
* @author wwd
* @Title: deletePhoto
* @Description: 删除文件
* @param @param savePath 存储路径
* @param @param fileName 文件名
* @return void 返回类型
* @throws
*/
public static void deletePhoto(String savePath,String fileName){
fileName = fileName.replace("/",File.separator);
File old_file = new File(new File(savePath), fileName.toString());
if (old_file.exists())
old_file.delete(); // 若存在即删除
}
/**
* @author wwd
* @Title: getNewPhoto
* @Description: 等比例压缩后获取图片名称
* @param @param fileName
* @param @param packages
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String getNewPhoto(String fileName,String packages,String savePath,int photoSize){
//根据大图生成小图 ,转换为了获取包路径
String newName ="";
try {
fileName = fileName.replace("/",File.separator);
File originalImage = new File(savePath+fileName);
newName = packages + File.separator + UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."), fileName.length());
ImageZipUtil.resize(originalImage, new File(savePath+newName),photoSize, 0.75f);
newName = newName.replace(File.separator,"/");
} catch (IOException e) {
e.printStackTrace();
}
return newName;
}
/**
* @author wwd
* @Title: copyPhotoByOne
* @Description: 上传图片不改名
* @param @param savePath
* @param @param photoFiles
* @param @param photoName
* @param @param packages
* @param @param flag
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static void updatePhotoByOne( String savePath, String fileName,File photoFiles) {
try {
//修改文件
File file = new File(savePath);
if (!file.isDirectory()) {
file.mkdirs();
}
File file1 = new File(new File(savePath), fileName);
if (!file1.getParentFile().exists()) {
file1.getParentFile().mkdirs();
}
FileUtils.copyFile(photoFiles, file1);
// 修改文件名称
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.sp.util;
import java.awt.Color;
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 com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
import javax.swing.ImageIcon;
public class ImageZipUtil {
/**
* @author wwd
* @Title: resize
* @Description: 等比例压缩图片
* @param @param originalFile
* @param @param resizedFile
* @param @param newWidth
* @param @param quality
* @param @throws IOException 设定文件
* @return void 返回类型
* @throws
*/
public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException {
if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
out.close();
} // Example usage
public static void main(String[] args) throws IOException {
File originalImage = new File("E:\\1.png");
resize(originalImage, new File("E:\\2.png"),455, 0.75f);
}
}
图片上传、修改、缩略图
猜你喜欢
转载自wang94777600.iteye.com/blog/2218414
今日推荐
周排行