图片上传、修改、缩略图
2015-06-10 09:37
239 查看
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);
}
}
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);
}
}
相关文章推荐
- 修改fckeditor使上传文件按用户id分目录存放,其上传图片加水印及缩略图
- asp.net 上传图片,生成缩略图,生成图片水印,文字水印
- 在ASP.NET中上传图片并生成缩略图
- 用ASP.NET上传图片并生成可带版权信息的缩略图
- 图片上传+缩略图+文字水印+图片水印
- 上传图片生成缩略图
- 上传图片并生成缩略图
- ionic开发之用户头像修改-图片选择与上传
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- 上传图片生成缩略图,水印
- C# 上传图片,生成缩略图,生成文字或图像水印图
- C#图片上传,加水印,自动生成缩略图类
- C#上传并保存图片、加水印、生成缩略图
- asp.net中使用Uploadify插件实现多张图片上传,上传后可显示缩略图、删除图片
- 图片上传 显示缩略图
- asp.net 2.0实战(2) 在GridView或DetailsView中加入日期选择和图片上传修改的自定义控件_jaye.net
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图(原创)
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(四)
- php实现图片上传并利用ImageMagick生成缩略图
- 上传图片并生成缩略图