码迷,mamicode.com
首页 > 编程语言 > 详细

java对图片的处理

时间:2017-11-14 14:19:27      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:www.   close   exp   信息   nbsp   gif   pre   smooth   宽度   

原文地址:http://www.blogjava.net/PrettyBoyCyb/archive/2006/11/13/80922.html

java 中的图片处理是很让人头疼的一件事情。目前 java api 中的 imageIO 可以将 gif 图片转换成 png 图片, jpg 图片可以正常转换。据说 gif 转 jpg 也是有办法的,但是将 jpg 转成 gif ,我费了很大的工夫才找到一个很好的解决方案。

首先介绍的是一段很好的缩放图片的代码:

public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
技术分享    // targetW,targetH分别表示目标长和宽
技术分享        int type = source.getType();
技术分享        BufferedImage target = null;
技术分享        double sx = (double) targetW / source.getWidth();
技术分享        double sy = (double) targetH / source.getHeight();
技术分享
技术分享        //这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
技术分享        //则将下面的if else语句注释即可
技术分享        if(sx>sy)
技术分享        {
技术分享            sx = sy;
技术分享            targetW = (int)(sx * source.getWidth());
技术分享        }else{
技术分享            sy = sx;
技术分享            targetH = (int)(sy * source.getHeight());
技术分享        }
技术分享
技术分享        if (type == BufferedImage.TYPE_CUSTOM) { //handmade
技术分享            ColorModel cm = source.getColorModel();
技术分享            WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
技术分享            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
技术分享            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
技术分享        } else
技术分享            target = new BufferedImage(targetW, targetH, type);
技术分享        Graphics2D g = target.createGraphics();
技术分享        //smoother than exlax:
技术分享        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
技术分享
技术分享
技术分享        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
技术分享        g.dispose();
技术分享        return target;
技术分享    }

接下来是将InputStream保存为jpg文件

技术分享public static void saveImageAsJpg (InputStream in, File saveFile,int width,int hight)    
技术分享            throws Exception {
技术分享        BufferedImage srcImage;
技术分享
技术分享        srcImage = ImageIO.read(in);
技术分享
技术分享        if(width > 0 || hight > 0)
技术分享        {
技术分享             srcImage = resize(srcImage, width, hight);
技术分享        }
技术分享
技术分享        ImageIO.write(srcImage, "JPEG", saveFile);
技术分享        in.close();
技术分享    }

参数解释:

in ::是一个 jpg 图片的 InputStream

saveFile ::目标文件

width ::目标宽度,如果不需要缩放则置 0

hight ::目标高度,如果不需要缩放则置 0

 

 

然后是将 InputStream 保存为 gif 文件:
private static void saveImageAsGif(InputStream in, File fileToSave,int width, int hight)
            throws Exception {
        BufferedImage srcImage;

        srcImage = ImageIO.read(in);

        if(width>0 && hight >0)
        {
             srcImage = resize(srcImage, width, hight);
        }

        FileOutputStream out = new FileOutputStream(fileToSave);
        GifEncoder encoder = new GifEncoder(srcImage, out);
        encoder.encode();

        in.close();
    }

参数解释:

in::是一个jpg或者gif图片的InputStream

saveFile::目标文件

width::目标宽度,如果不需要缩放则置0

hight::目标高度,如果不需要缩放则置0

 

GifEncoder这个类是java api中没有的,也是我找寻了很久的一个东西。它是Acme.JPM.Encoders.GifEncoder,只要找到这个包,将jpg转为gif就不是问题了。

 

 

Jpg图片使用的是24-bit的编码,pngpng-24png-8两种,但是gif8-bit的编码。如果强行将jpg图片信息流按字节拆开,转换成gif图片,即使使用标准256色,也会出现严重的失真。

 

我曾经使用了gif4j_light_trial_1.0.jar这个包,但是这个包让我很失望。尽管它可以将多张图片合成一个动态的gif图片,但是它是付费的,免费使用期只有一个月。并且,在使用它将jpg图片转换成gif图片之后中间会有一条白色的横线,不知道这是系统的缺陷还是在费用包中做的手脚。

java对图片的处理

标签:www.   close   exp   信息   nbsp   gif   pre   smooth   宽度   

原文地址:http://www.cnblogs.com/eyesmoon/p/7831675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!