java gif等比例缩放_对gif动图进行缩放等处理(java)

使用的是gif4j的工具包(官方网站上下载的是含有水印的)

1.对gif图进行尺寸的调整缩放,按尺寸

/**

* 将gif动图重新调整大小并且输出

* @param src 源文件

* @param dest 生成文件

* @param width 生成文件的宽度

* @param height 生成文件的高度

* @throws IOException

*/

public static void gifReSize(File src, File dest, int width, int height) throws IOException {

GifImage srcImage = GifDecoder.decode(src);

//主要调用的就是GifTransformer种的resize方法进行图片尺寸的调整

GifImage resizeImage = GifTransformer.resize(srcImage, width, height, true);

GifEncoder.encode(resizeImage, dest);

}

2.对gif图进行尺寸调整缩放,按比例

/**

* 将gif动图重新调整大小并且输出

* @param src 源文件

* @param dest 生成文件

* @param wQuotiety 调整的比例,比如80%就是0.8

* @param hQuotiety 调整的比例,比如80%就是0.8

* @throws IOException

*/

public static void gifScale(File src, File dest, double wQuotiety, double hQuotiety) throws IOException {

GifImage srcImage = GifDecoder.decode(src);

GifImage scaleImg = GifTransformer.scale(srcImage, wQuotiety, hQuotiety, true);

GifEncoder.encode(scaleImg, dest);

}

3.对gif图进行剪切

/**

* 对gif图剪切,参数是坐标和宽高

*/

public static void gifCut(File src, File dest, int x, int y, int w, int h) throws IOException {

Rectangle rectangle = new Rectangle(x, y, w, h);

GifImage srcImg = GifDecoder.decode(src);

GifImage cropImg = GifTransformer.crop(srcImg, rectangle);

GifEncoder.encode(cropImg, dest);

}

4.对gif图加入水印

/**

* 加水印,参数是文字

*/

public static void shuiyin(File src, File dest, String text) throws IOException {

GifImage srcImg = GifDecoder.decode(src);

TextPainter textPainter = new TextPainter(new Font("微软雅黑", Font.BOLD, 12));

textPainter.setOutlinePaint(Color.WHITE);

BufferedImage renderedWatermarkText = textPainter.renderString(text, true);

Watermark watermark = new Watermark(renderedWatermarkText, Watermark.LAYOUT_TOP_LEFT);

GifImage applyImg = watermark.apply(srcImg, true);

GifEncoder.encode(applyImg, dest);

}

例子和调用

public static void main(String[] arg) throws IOException {

File in = new File("C:\\xxxx\\2.gif");

File out = new File("C:\\xxxx\\2_scale.gif");

File out2 = new File("C:\\xxxx\\2_cut.gif");

File out3 = new File("C:\\xxxx\\2_水印.gif");

GIFRebuild.gifScale(in, out, 0.7, 0.7);

GIFRebuild.gifCut(in, out2, 70, 0, 120, 280);

GIFRebuild.shuiyin(in, out3, "啊哈哈");

}

  • 0
    点赞
  • 2
    收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39852647

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值