使用压缩进行缓冲图像转换

时间:2013-02-09 16:01:07

标签: java bufferedimage javax.imageio

尝试读取缓冲的bmp图像,然后使用压缩转换为jpeg。缓冲的图像线有问题。谢谢,任何帮助表示赞赏。

public void jpegconvert(String Fname, String Crate)
{

BufferedImage image =  ImageIO.read(Fname);

Iterator<ImageWriter> i = ImageIO.getImageWritersByFormatName("jpeg"); 

// Just get the first JPEG writer available 
ImageWriter jpegWriter = i.next(); 

// Set the compression quality to 0.8 
ImageWriteParam param = jpegWriter.getDefaultWriteParam(); 
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
param.setCompressionQuality(0.8f); 

// Write the image to a file 
FileImageOutputStream out = new FileImageOutputStream(new File("newimage.jpg")); 
jpegWriter.setOutput(out); 
jpegWriter.write(null, new IIOImage(image, null, null), param); 
jpegWriter.dispose(); 
out.close();
}
}

悬停错误说明了这一点:

no suitable method found for read(String)
    method ImageIO.read(ImageInputStream) is not applicable
      (actual argument String cannot be converted to ImageInputStream by method invocation conversion)
    method ImageIO.read(URL) is not applicable
      (actual argument String cannot be converted to URL by method invocation conversion)
    method ImageIO.read(InputStream) is not applicable
      (actual argument String cannot be converted to InputStream by method invocation conversion)
    method ImageIO.read(File) is not applicable
      (actual argument String cannot be converted to File by method invocation conversion)

1 个答案:

答案 0 :(得分:2)

问题在于这个特殊的代码:
ImageIO.read(Fname);
coz Fname是一个String并且检查API for read method它不会将String作为任何重载方法中的参数

reference可能对您有所帮助

相关问题