java对图片的处理(保存和显示)

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

 

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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值