resizestrategiesexample.java

来自「java对GIF的编解码工具,有完整的说明和例子」· Java 代码 · 共 72 行

JAVA
72
字号

import com.gif4j.light.GifEncoder;
import com.gif4j.light.GifFrame;
import com.gif4j.light.GifImage;

import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * This example demostrates {@link com.gif4j.light.GifImage} resize strategies usage and differences.
 */

public class ResizeStrategiesExample {

    public static void main(String[] args) {
        // change out directory if it is necessary
        File outputDir = new File("." + File.separator + "result");
        if (!outputDir.exists())
            outputDir.mkdirs();
        try {
            GifImage imageWithExtendToCurrentResizeStrategy = new GifImage(GifImage.RESIZE_STRATEGY_EXTEND_TO_CURRENT);
            // set indefinite looping
            imageWithExtendToCurrentResizeStrategy.setLoopNumber(0);
            // 100x100 frame 1 at position (0,0)
            imageWithExtendToCurrentResizeStrategy.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(100, 100)));
            // 50x50 frame 2 at position (150,150)
            imageWithExtendToCurrentResizeStrategy.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(150, 150), new Point(50, 50)));
            // 100x100 frame 3 at position (100,100)
            imageWithExtendToCurrentResizeStrategy.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(100, 100), new Point(100, 100)));
            GifEncoder.encode(imageWithExtendToCurrentResizeStrategy, new File(outputDir, "ResizeStrategiesExample_extend_to_current.gif"), true);


            GifImage imageWithCropToFitImageSize = new GifImage(150, 150, GifImage.RESIZE_STRATEGY_CROP_TO_FIT_IMAGE_SIZE);
            // set indefinite looping
            imageWithCropToFitImageSize.setLoopNumber(0);
            // 100x100 frame 1 at position (0,0)
            imageWithCropToFitImageSize.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(100, 100)));
            // 50x50 frame 2 at position (150,150)
            imageWithCropToFitImageSize.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(150, 150), new Point(50, 50)));
            // 100x100 frame 3 at position (100,100)
            imageWithCropToFitImageSize.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(100, 100), new Point(100, 100)));
            GifEncoder.encode(imageWithCropToFitImageSize, new File(outputDir, "ResizeStrategiesExample_crop_to_fit.gif"), true);


            GifImage imageWithScaleToFitImageSize = new GifImage(150, 150, GifImage.RESIZE_STRATEGY_SCALE_TO_FIT_IMAGE_SIZE);
            // set indefinite looping
            imageWithScaleToFitImageSize.setLoopNumber(0);
            // 100x100 frame 1 at position (0,0)
            imageWithScaleToFitImageSize.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(100, 100)));
            // 50x50 frame 2 at position (150,150)
            imageWithScaleToFitImageSize.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(150, 150), new Point(50, 50)));
            // 100x100 frame 3 at position (100,100)
            imageWithScaleToFitImageSize.addGifFrame(
                    new GifFrame(TestUtils.genTestImage(100, 100), new Point(100, 100)));
            GifEncoder.encode(imageWithScaleToFitImageSize, new File(outputDir, "ResizeStrategiesExample_scale_to_fit.gif"), true);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?