2

对于whatsapp贴纸,我正在使用AnimatedGifEncoder生成动画gif并将其保存为webp,但whatsapp不接受它,所以我需要有人指导我如何在不使用ffmpeg的情况下将位图列表转换为动画webp或将生成的git转换为动画webp .

已经尝试过这个,但它抛出了以下异常不支持 FourCC:ICCP

代码如下:

    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;
}
4

0 回答 0