页面从数据库读取图片的地址,图片在存入数据前被进行过大小处理.结果不能显示.我把没处理过的图片放到相同路径,能够正常形式.图片处理代码:
public class convertImage {
private String fileInput ;
private String fileOutput ;
private int nw ;//转换后的图片宽
private int nh ;//转换后的图片高
public convertImage()
{
}
public String getFileInput() {
return fileInput;
}
public void setFileInput(String fileInput) {
this.fileInput = fileInput;
}
public String getFileOutput() {
return fileOutput;
}
public void setFileOutput(String fileOutput) {
this.fileOutput = fileOutput;
}
public void setImage(int nw,int nh) {
this.nw=nw;
this.nh=nh;
}
public void convert()
{
//BufferedImage src = ImageIO.read(new File("c:\\test.png"));//or png.
//BufferedImage dest = new BufferedImage(src.getWidth()/2, src.getHeight()/2, BufferedImage.TYPE_INT_RGB);
//dest.getGraphics().drawImage(src,0,0,src.getWidth()/2, src.getHeight()/2,null);
//ImageIO.write(dest, "jpeg", new File("C:\\small.jpg")); try {
File fi = new File(fileInput); //大图文件
File fo = new File(fileOutput); //将要转换出的小图文件
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
if(w<=nw||h<=nh)
{
//AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(w,h,BufferedImage.TYPE_3BYTE_BGR);
//ato.filter(bis,bid);
bid.getGraphics().drawImage(bis,0,0,w,h,null);
ImageIO.write(bid,"jpeg",fo);
return;
}
double sx = (double)nw/w;
double sy = (double)nh/h ;
//判断是横向图形还是坚向图形
if ( w > h ) //横向图形
{
if ( (int)(sx * h ) > nh ) //比较高不符合高度要求,就按高度比例
{
sx = sy ;
nw = (int)(w*sx) ;
}
else
{
sy = sx ;
nh = (int)( h*sy) ;
}
}
else
{
if ( (int)(sy * w ) > nw )
{
sy = sx ;
nh = (int)(h * sy ) ;
}
else
{
sx = sy ;
nw = (int)(w*sx) ;
}
} transform.setToScale(sx,sy);
//AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
//ato.filter(bis,bid);
bid.getGraphics().drawImage(bis,0,0,nw,nh,null);
ImageIO.write(bid,"jpeg",fo);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class convertImage {
private String fileInput ;
private String fileOutput ;
private int nw ;//转换后的图片宽
private int nh ;//转换后的图片高
public convertImage()
{
}
public String getFileInput() {
return fileInput;
}
public void setFileInput(String fileInput) {
this.fileInput = fileInput;
}
public String getFileOutput() {
return fileOutput;
}
public void setFileOutput(String fileOutput) {
this.fileOutput = fileOutput;
}
public void setImage(int nw,int nh) {
this.nw=nw;
this.nh=nh;
}
public void convert()
{
//BufferedImage src = ImageIO.read(new File("c:\\test.png"));//or png.
//BufferedImage dest = new BufferedImage(src.getWidth()/2, src.getHeight()/2, BufferedImage.TYPE_INT_RGB);
//dest.getGraphics().drawImage(src,0,0,src.getWidth()/2, src.getHeight()/2,null);
//ImageIO.write(dest, "jpeg", new File("C:\\small.jpg")); try {
File fi = new File(fileInput); //大图文件
File fo = new File(fileOutput); //将要转换出的小图文件
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
if(w<=nw||h<=nh)
{
//AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(w,h,BufferedImage.TYPE_3BYTE_BGR);
//ato.filter(bis,bid);
bid.getGraphics().drawImage(bis,0,0,w,h,null);
ImageIO.write(bid,"jpeg",fo);
return;
}
double sx = (double)nw/w;
double sy = (double)nh/h ;
//判断是横向图形还是坚向图形
if ( w > h ) //横向图形
{
if ( (int)(sx * h ) > nh ) //比较高不符合高度要求,就按高度比例
{
sx = sy ;
nw = (int)(w*sx) ;
}
else
{
sy = sx ;
nh = (int)( h*sy) ;
}
}
else
{
if ( (int)(sy * w ) > nw )
{
sy = sx ;
nh = (int)(h * sy ) ;
}
else
{
sx = sy ;
nw = (int)(w*sx) ;
}
} transform.setToScale(sx,sy);
//AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
//ato.filter(bis,bid);
bid.getGraphics().drawImage(bis,0,0,nw,nh,null);
ImageIO.write(bid,"jpeg",fo);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货