---
title: "图像类型转换 "
author: "thoughtfly"
date: "2009-10-28T15:28:39Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-05-25T17:13:16Z"
id: 505698
url: "https://thoughtfly.iteye.com/blog/505698"
---

```
 /**
      * 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X) 
      */  
     public static void convert(String source, String result)  
     {  
         try  
         {  
             File f = new File(source);  
             f.canRead();  
             f.canWrite();  
            BufferedImage src = ImageIO.read(f);  
             ImageIO.write(src, "JPG", new File(result));  
         }  
         catch (Exception e)  
         {  
             // TODO Auto-generated catch block  
             e.printStackTrace();  
         }  
     }  
   
     /** *//** 
      * 彩色转为黑白 
      * @param source 
      * @param result 
      */  
     public static void gray(String source, String result)  
     {  
         try  
         {  
             BufferedImage src = ImageIO.read(new File(source));  
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
            ColorConvertOp op = new ColorConvertOp(cs, null);  
             src = op.filter(src, null);  
             ImageIO.write(src, "JPEG", new File(result));  
        }  
         catch (IOException e)  
         {  
             e.printStackTrace();  
         }  
     }  
```