最近的写的插件要做到,文件浏览器获得图片位置,然后压缩图片,再存为本地文件
压缩图片很简单,关键是把ImageIcon 保存为本地图片废了好些力气
我的解决方法是将ImageIcon 写入BufferedImage ,
然后调用标准的图片保存方法: javax.imageio.ImageIO.write(bi, "JPEG", new java.io.File(lastfilename));
同时注意保存文件的路径问题:window下通过 getClass().getResource("/")获得的路进都是带有“file//”开头的,但是保存的时候将“file//”开头的路径作为文件名,windows下就会报错说找不到路径。把“file//”去掉解决。还会遇到路径中“/”,"/"的问题,最好把所有的“/”都过滤成"/",减少出错的几率。
完整代码在下面:
先从文件选择器读入图片的地址。然后传入MakeFace生成压缩图片,再返回图片的相对地址。
JFileChooser fDialog=new JFileChooser(); //文件选择器
Frame frame=new Frame();
int result=fDialog.showOpenDialog(frame);
if(result==JFileChooser.APPROVE_OPTION){
try {
java.lang.String fname = fDialog.getSelectedFile().getAbsolutePath();
MakeFace mf=new MakeFace(getClass().getResource("/faceimage/").toString(),fname);
image.setText(changeXie("/faceimage"+mf.getImagepath()));
// System.out.print(changeXie("/faceimage"+mf.getImagepath()));
} catch (Exception ex) {
}
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
package com.soulnew.util;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
/**
*
* @author [email protected]
* @ blog: http://souljava.blog.163.com
*/
public class MakeFace {
String floder;
ImageIcon imageicon;
String imagepath;
public String getImagepath() {
return imagepath;
}
public ImageIcon getImageicon() {
return imageicon;
}
/*
@floder : the floder where used to save the image
@imagepath : the image path.
*/
public MakeFace(String floder,String imagepath) {
this.floder=floder;
buildimage(imagepath);
String imagename;
if(imagepath.indexOf("/")!=-1)
imagename=imagepath.substring(imagepath.lastIndexOf("/"),imagepath.indexOf("."));
else
imagename=imagepath.substring(imagepath.lastIndexOf("//"),imagepath.indexOf("."));
this.imagepath=imagename+".jpg";
// System.out.print("#"+this.getImagepath());
saveImage(floder+imagename+".jpg");
}
public void buildimage(String imagepath){
ImageIcon icon = new ImageIcon(imagepath);
// scale to 64 pix in largest dimension
Image img = icon.getImage();
float factor = 1.0f;
if (img.getWidth(null) > img.getHeight(null))
factor = Math.min(64f / img.getWidth(null), 1.0f);
else
factor = Math.min(64f / img.getHeight(null), 1.0f);
Image scaledImage =
img.getScaledInstance((int) (img.getWidth(null) * factor),
(int) (img.getHeight(null) * factor),
Image.SCALE_FAST);
icon.setImage(scaledImage);
imageicon=icon;
}
public void saveImage(String filename){
try {
java.awt.image.BufferedImage bi = new java.awt.image.BufferedImage(imageicon.getIconWidth(), imageicon.getIconHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Image image = imageicon.getImage();
Graphics2D g2= (Graphics2D)bi.getGraphics();
g2.drawImage(image,0,0,null);
String temp=filename;
String lastfilename=null;
if(temp.indexOf("file//")==-1)
lastfilename=temp.substring(temp.indexOf("file//")+7,temp.length());
lastfilename=changeSpace(lastfilename);
javax.imageio.ImageIO.write(bi, "JPEG", new java.io.File(lastfilename));
g2.dispose();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String changeSpace(String string){
StringBuffer sb=new StringBuffer(string);
int pos=sb.indexOf("%20");
while(pos!=-1){
sb.replace(pos, pos+3, "/u0020");
pos=sb.indexOf("%20");
}
return sb.toString();
}
public static void main(String arg[]){
MakeFace mf=new MakeFace("d:/图","C://laoma.gif");
//java.lang.System.out.print(mf.getImagepath());
}
}