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

您所在的位置:网站首页 gif调整尺寸 对gif动图进行缩放等处理(java)

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

2023-01-01 06:10| 来源: 网络整理| 查看: 265

使用的是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, "啊哈哈"); }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3