10-04
09
Acdsee中的功能,进行类型,覆盖原有图片,重新定义质量
作者:Java伴侣 日期:2010-04-09
Acdsee中的功能,进行类型,覆盖原有图片,重新定义质量
复制内容到剪贴板 程序代码
public class Test {
//static String filepath = "e:\tmp";
/**
* 文件的拷贝及其质量处理
* @param filePath
* @throws Exception
*/
public void copyAndQuality(String filePath) throws Exception {
File _file = new File(filePath); // 读入文件
int pos = filePath.lastIndexOf(".");
String ext = filePath.substring(pos+1, filePath.length());//扩展名
Image src = ImageIO.read(_file); // 构造Image对象
int width = src.getWidth(null); // 得到源图宽
int height = src.getHeight(null); // 得到源图长
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(src, 0, 0, width, height, null);
ImageWriter writer = null;
ImageTypeSpecifier type = ImageTypeSpecifier
.createFromRenderedImage(image);
Iterator iter = ImageIO.getImageWriters(type, ext);
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
if (writer == null) {
return;
}
IIOImage iioImage = new IIOImage(image, null, null);
FileUtil fu = new FileUtil();
if(fu.isExists(filePath))
fu.delFile(filePath);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality((float) (5 / 10.0));
ImageOutputStream outputStream = ImageIO
.createImageOutputStream(new File(filePath));//"c:\\张某人."+ext
writer.setOutput(outputStream);
writer.write(null, iioImage, param);
outputStream.close();
}
/**
* 读取文件夹中的文件
*
* @param filepath
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static boolean readFiles(String filepath)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
// System.out.println("absolutepath="
// + readfile.getAbsolutePath());
//System.out.println("name=" + readfile.getName());
String path =readfile.getPath();
try {
new Test().copyAndQuality(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (readfile.isDirectory()) {
readFiles(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
public static void main(String[] args) {
// try {
// new Test().jpgTset();
// } catch (Exception e) {
// e.printStackTrace();
// }
String filepath = "e:/test";
try {
new Test().readFiles(filepath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//static String filepath = "e:\tmp";
/**
* 文件的拷贝及其质量处理
* @param filePath
* @throws Exception
*/
public void copyAndQuality(String filePath) throws Exception {
File _file = new File(filePath); // 读入文件
int pos = filePath.lastIndexOf(".");
String ext = filePath.substring(pos+1, filePath.length());//扩展名
Image src = ImageIO.read(_file); // 构造Image对象
int width = src.getWidth(null); // 得到源图宽
int height = src.getHeight(null); // 得到源图长
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(src, 0, 0, width, height, null);
ImageWriter writer = null;
ImageTypeSpecifier type = ImageTypeSpecifier
.createFromRenderedImage(image);
Iterator iter = ImageIO.getImageWriters(type, ext);
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
if (writer == null) {
return;
}
IIOImage iioImage = new IIOImage(image, null, null);
FileUtil fu = new FileUtil();
if(fu.isExists(filePath))
fu.delFile(filePath);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality((float) (5 / 10.0));
ImageOutputStream outputStream = ImageIO
.createImageOutputStream(new File(filePath));//"c:\\张某人."+ext
writer.setOutput(outputStream);
writer.write(null, iioImage, param);
outputStream.close();
}
/**
* 读取文件夹中的文件
*
* @param filepath
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static boolean readFiles(String filepath)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
// System.out.println("absolutepath="
// + readfile.getAbsolutePath());
//System.out.println("name=" + readfile.getName());
String path =readfile.getPath();
try {
new Test().copyAndQuality(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (readfile.isDirectory()) {
readFiles(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
public static void main(String[] args) {
// try {
// new Test().jpgTset();
// } catch (Exception e) {
// e.printStackTrace();
// }
String filepath = "e:/test";
try {
new Test().readFiles(filepath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
评论: 0 | 引用: 0 | 查看次数: 230
发表评论