我现有一jpg格式图像,想把它缩小了并重新保存 先读入 BufferedImage img = ImageIO.read(filename);
然后
int syswidth = 200;
int sysheight = 150;
Image simg = img.getScaledInstance(sysheight, syswidth,
BufferedImage.SCALE_DEFAULT); 请问如何写到硬盘上去呢,或者能换个方法缩小 ImageIo.write是不行了, 因为它需要的参数是BufferedImage, 而我得到的是Image型
然后
int syswidth = 200;
int sysheight = 150;
Image simg = img.getScaledInstance(sysheight, syswidth,
BufferedImage.SCALE_DEFAULT); 请问如何写到硬盘上去呢,或者能换个方法缩小 ImageIo.write是不行了, 因为它需要的参数是BufferedImage, 而我得到的是Image型
AffineTransform transform = new AffineTransform();
transform.setToScale(sx,sy);
//操作
AffineTransformOp op = new AffineTransformOp(transform,null);
op.filter(source,target);然后就可以用ImageIo.write(target,"jpeg",outstream)
sy = 150/1536;抛出异常如下
Exception in thread "main" java.awt.image.ImagingOpException: Unable to invert transform AffineTransform[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
at java.awt.image.AffineTransformOp.validateTransform(Unknown Source)
at java.awt.image.AffineTransformOp.<init>(Unknown Source)不知道是不是我哪个参数错了
{
double ratio = 0.0;
File file = new File(filePath);
if(!file.isFile())
{
throw new Exception(file+" is not image file error in getFixedBoundIcon!");
}
Icon ret = new ImageIcon(filePath);
BufferedImage bi = ImageIO.read(file);
if(bi.getHeight()>height||bi.getWidth()>width)
{
if(bi.getHeight()>bi.getWidth())
{
ratio = (new Integer(height)).doubleValue()/bi.getHeight();
}
else
{
ratio = (new Integer(width)).doubleValue()/bi.getWidth();
}
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0,lastLength);
String fileType = filePath.substring(lastLength);
File zoomFile = new File(subFilePath+fileType);
Image temp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),null);
temp = op.filter(bi, null);
ImageIO.write((BufferedImage)temp, "jpg", zoomFile);
ret = new ImageIcon(zoomFile.getPath());
}
return ret;
}
http://blog.csdn.net/fancyerII/archive/2010/08/25/5837991.aspx
try{
Image image = ImageIO.read(new File(inPath));
int w=(int)(image.getWidth(null)*scale);
int h=(int)(image.getHeight(null)*scale); BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(image, 0, 0, w, h, null);
g2.dispose();
FileOutputStream newimage = new FileOutputStream(outPath);
ImageIO.write(tmp, "JPEG", newimage);
}catch(Exception e){
e.printStackTrace();
}
}