当前位置: 技术问答>java相关
请教图片上传中的改变图片大小的问题!!急!!!
来源: 互联网 发布时间:2015-08-26
本文导语: 我在应用中需要解决一个问题,就是如何将上传的图片自动改成小的缩略图?? 请求各位高手给于解决的良方!万分感激!! | import java.io.File; import java.awt.image.BufferedImage; import java.awt.Image; import...
我在应用中需要解决一个问题,就是如何将上传的图片自动改成小的缩略图??
请求各位高手给于解决的良方!万分感激!!
请求各位高手给于解决的良方!万分感激!!
|
import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.RescaleOp;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.geom.AffineTransform;
public class ThumbNail{
String fromdir="";
String todir="";
String imgfile="";
String sysimgfile="";
String ext="jpg"; //默认图片格式
int size=0;
public ThumbNail(){}
public boolean CreateThumbnail() throws Exception
{
double Ratio=0.0;
BufferedImage Bi=null;
Image Itemp=null;
BufferedImage img11=null;
File F = new File(fromdir,imgfile);
if (!F.isFile())
throw new Exception(F+" is not image file error in CreateThumbnail!");
File ThF = new File(todir,sysimgfile+"."+ext);
try{
Bi = ImageIO.read(F);
int BW=(int)(size*Bi.getWidth()/Bi.getHeight());
int BH=size;
Itemp = Bi.getScaledInstance (BW,BH,Bi.SCALE_SMOOTH);
BufferedImage BImg=null;
try{
if (Bi.getHeight()>size){
Ratio =(double)(size)/Bi.getHeight();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
BImg=op.filter(Bi, null);
}catch(Exception ex){
BImg=new BufferedImage(Itemp.getWidth(null),Itemp.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2= BImg.createGraphics();
g2.drawImage(Itemp,0,0,null);
}
ImageIO.write((BufferedImage)BImg, ext, ThF);
}catch (Exception ex) {
this.saveJust();
System.out.println(" ImageIo.write error in CreatThum: "+ex.getMessage());
return false;
}
return (true);
}
public void saveJust()
{
try{
File F = new File(fromdir,imgfile);
if (!F.isFile())
throw new Exception(F+" is not image file error in CreateThumbnail!");
File ThF = new File(todir,imgfile);
FileInputStream fis=new FileInputStream(F);
FileOutputStream fos=new FileOutputStream(ThF);
byte[] b = new byte[255];
int bRead=0;
while((bRead=fis.read(b))>-1)
fos.write(b,0,bRead);
fos.close();
fis.close();
}catch(Exception e)
{
}
}
public void setfromdir(String str)
{
fromdir=str;
}
public void settodir(String str)
{
todir=str;
}
public void setimgfile(String str)
{
imgfile=str;
}
public void setsysimgfile(String str)
{
for(int i=str.length()-1;i>=0;i--)
if(str.charAt(i)=='.'){
sysimgfile=str.substring(0,i);
i=-1;
return;
}
sysimgfile=str;
}
public void setsize(int s)
{
size=s;
}
public void setext(String str)
{
ext=str;
}
/*public static void main(String args[])
{
ThumbNail tn=new ThumbNail();
tn.setsize(100);
tn.setfromdir("d:/javaTest/");
tn.setimgfile("abc.jpg");
tn.settodir("d:/");
tn.setsysimgfile("trytr");
try{
if(tn.CreateThumbnail())
System.out.println("succed");
}catch(Exception e){
System.out.println("failed to create the thumbnail");
}
}*/
}
认真看吧
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.RescaleOp;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.geom.AffineTransform;
public class ThumbNail{
String fromdir="";
String todir="";
String imgfile="";
String sysimgfile="";
String ext="jpg"; //默认图片格式
int size=0;
public ThumbNail(){}
public boolean CreateThumbnail() throws Exception
{
double Ratio=0.0;
BufferedImage Bi=null;
Image Itemp=null;
BufferedImage img11=null;
File F = new File(fromdir,imgfile);
if (!F.isFile())
throw new Exception(F+" is not image file error in CreateThumbnail!");
File ThF = new File(todir,sysimgfile+"."+ext);
try{
Bi = ImageIO.read(F);
int BW=(int)(size*Bi.getWidth()/Bi.getHeight());
int BH=size;
Itemp = Bi.getScaledInstance (BW,BH,Bi.SCALE_SMOOTH);
BufferedImage BImg=null;
try{
if (Bi.getHeight()>size){
Ratio =(double)(size)/Bi.getHeight();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
BImg=op.filter(Bi, null);
}catch(Exception ex){
BImg=new BufferedImage(Itemp.getWidth(null),Itemp.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2= BImg.createGraphics();
g2.drawImage(Itemp,0,0,null);
}
ImageIO.write((BufferedImage)BImg, ext, ThF);
}catch (Exception ex) {
this.saveJust();
System.out.println(" ImageIo.write error in CreatThum: "+ex.getMessage());
return false;
}
return (true);
}
public void saveJust()
{
try{
File F = new File(fromdir,imgfile);
if (!F.isFile())
throw new Exception(F+" is not image file error in CreateThumbnail!");
File ThF = new File(todir,imgfile);
FileInputStream fis=new FileInputStream(F);
FileOutputStream fos=new FileOutputStream(ThF);
byte[] b = new byte[255];
int bRead=0;
while((bRead=fis.read(b))>-1)
fos.write(b,0,bRead);
fos.close();
fis.close();
}catch(Exception e)
{
}
}
public void setfromdir(String str)
{
fromdir=str;
}
public void settodir(String str)
{
todir=str;
}
public void setimgfile(String str)
{
imgfile=str;
}
public void setsysimgfile(String str)
{
for(int i=str.length()-1;i>=0;i--)
if(str.charAt(i)=='.'){
sysimgfile=str.substring(0,i);
i=-1;
return;
}
sysimgfile=str;
}
public void setsize(int s)
{
size=s;
}
public void setext(String str)
{
ext=str;
}
/*public static void main(String args[])
{
ThumbNail tn=new ThumbNail();
tn.setsize(100);
tn.setfromdir("d:/javaTest/");
tn.setimgfile("abc.jpg");
tn.settodir("d:/");
tn.setsysimgfile("trytr");
try{
if(tn.CreateThumbnail())
System.out.println("succed");
}catch(Exception e){
System.out.println("failed to create the thumbnail");
}
}*/
}
认真看吧