网上流传的保存图片的代码,菜鸟正在学习中

保存图片的代码
 
public static void saveImage(Image image,String path)     
{     
    BufferedImage bi=new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();     
    g2.drawImage(image, null, null);     
    FileOutputStream fos=null;     //文件输出流对象
    try {     
            fos=new FileOutputStream(path);     //获得路径

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();     
    }     
    JPEGImageEncoder je= JPEGCodec.createJPEGEncoder(fos);   //创建一个指向  fos的JPEGImageEncoder对象
    JPEGEncodeParam jp=je.getDefaultJPEGEncodeParam(bi);     //@@@
    jp.setQuality(0.5f, false);      //创建替代当前已建量化表的新量化表
    je.setJPEGEncodeParam(jp);      //设置JPEGImageEncoder对象编码操作
    try {     
        je.encode(bi);      // 将 BufferedImage 作为 JPEG 数据流编码。
        fos.close();     
    } catch (ImageFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();     
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();     
    }              
}

你可能感兴趣的:(网上流传的保存图片的代码,菜鸟正在学习中)