Android Encode Animated Webp from list of bitmaps

504 views Asked by At

for whatsapp stickers i am generating animated gif using AnimatedGifEncoder and saving it as webp, but whatsapp does not accept it, so i need someone who guide me how to convert list of bitmaps into animated webp without using ffmpeg or covert the generated git into animated webp.

have tried this library but it throws the follow exception Not supported FourCC: I.C.C.P.

code is as follows:

    public boolean transform(List<Bitmap> bitmaps) {

    if (bitmaps == null || bitmaps.isEmpty()) {
        throw new IllegalArgumentException("Bitmaps is empty!");
    }

    //-----------------initiate the encoder----------------------
    AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    //----------------- encoder.start(bos) ----------------------
    encoder.start(bos);
    encoder.setRepeat(0);
    encoder.setQuality(quality);

    final int size = bitmaps.size();
    Bitmap sourceBmp;
    Bitmap resultBbm;
    for (int i = 0; i < size; i++) {
        sourceBmp = bitmaps.get(i);
        if (sourceBmp == null) {
            continue;
        }

        // result bitmap
        resultBbm = ThumbnailUtils.extractThumbnail(sourceBmp, sourceBmp.getWidth() / scaleX,
                sourceBmp.getHeight() / scaleY, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

        try {
            encoder.addFrame(resultBbm);
            if (onTransformProgressListener != null) {
                onTransformProgressListener.onProgress(i, size);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.gc();
            break;
        } finally {
            if (!sourceBmp.isRecycled()) {
                sourceBmp.recycle();
            }
            if (!resultBbm.isRecycled()) {
                resultBbm.recycle();
            }
        }
    }
    //----------------- encoder.finish() ----------------------
    encoder.finish();
    bitmaps.clear();

    byte[] data = bos.toByteArray();
    File saveFile = new File(outputPath);

    if (!saveFile.getParentFile().exists()) {
        saveFile.getParentFile().mkdirs();
    }

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(saveFile);
        fos.write(data);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return saveFile.exists() && saveFile.length() > 0;
}
0

There are 0 answers