在Android中创建GIF需要很长时间

时间:2016-07-06 06:40:09

标签: java android animated-gif

我正在创建一个应用程序,它会突发捕获图像并创建一个GIF作为输出。我的问题是从图像序列创建GIF需要很长时间,图像的分辨率是否为320x240。我正在使用AnimatedGifEncoder类进行GIF编码。如下link

我创建GIF的代码如下:

private void saveGifImage() {
    FileOutputStream outStream = 
    String fileName = "test.gif";

    try {
        File file = new File(Environment.getExternalStorageDirectory() + "/gif_convertor/sample/");
        if (!file.exists())
            file.mkdirs();
        File file1 = new File(file + File.separator + 
        if (file1.exists()) {

        } else {
            try {
                // file1.mkdirs();
                file1.createNewFile();
            } catch (Exception e) {
            }
        }
        outStream = new FileOutputStream(file1);
        Log.d("Location", file1.getPath().toString());

        outStream.write(generateGIF());
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DialogUtils.stopProgressDisplay();
    }
}

private byte[] generateGIF() {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    encoder.start(bos);
    encoder.delay = 33; // 50 means 0.5 seconds ( 100 value is equivalent to 1 seconds)
    encoder.repeat = 0; // 0 means repeat forever, other n positive integer means n times repeat , -1 means no repeat
    encoder.sizeSet = true; //  resize allowed with true flag

    encoder.width = 320;
    encoder.height = 240;
    File tempDir = new File(getActivity().getExternalFilesDir(null), "temp");
    if (tempDir.exists()) {
        for (File file : tempDir.listFiles()) {
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            encoder.addFrame(bitmap);
        }
    }
    encoder.finish();
    return bos.toByteArray();
}

0 个答案:

没有答案