gpt4 book ai didi

java - 将 jpeg 转换为 gif 太长

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:17 25 4
gpt4 key购买 nike

我正在尝试使用从摄像机获取的 jpeg 制作 gif 动画。但是这个过程是虚幻的漫长。我使用了两个不同的库。 First是用 native C++ 代码编写的,其次是 Java 的 one .

我尽可能地压缩帧,但即使这样也不能减少生成时间。

native 库需要大约 80-100 秒,Java 需要大约 40-60 秒(我不知道 java 是如何快 2 倍,但日志显示这个结果)5 秒视频 16 fps(每 80 帧)动图)。

我根据this修改了一点C++算法,因为我遇到了同样的问题(通过更改一段代码和更改整个 learn() 函数尝试了两个版本)。

在这里你可以看到一段日志:

它是 native 实现中的最后三帧:

D/TimeUtils: Adding frame executed in 949ms
D/TimeUtils: Adding frame executed in 976ms
D/TimeUtils: Adding frame executed in 1028ms
D/TimeUtils: Creating gif with native library executed in 82553ms

这是 Java 版本的最后三帧:

D/TimeUtils: Adding frame executed in 541ms
D/TimeUtils: Adding frame executed in 513ms
D/TimeUtils: Adding frame executed in 521ms
D/TimeUtils: Creating gif with nbadal's library executed in 44811ms

也许还有一些其他有用的日志:

D/CameraActivity: Duration of the captured video is 5000ms
V/CameraActivity: Dimensions are 288w x 288h
D/CameraActivity: Final bitmaps count: 80

TimeUtils.java 包含检查方法执行时间的静态方法。

NativeGifConverter.java(仅转换函数):

@Override public void createGifFile(String path, List<String> bitmapPaths) {

Bitmap bitmap = BitmapUtils.retrieve(bitmapPaths.get(0));

if (init(path, bitmap.getWidth(), bitmap.getHeight(), mNumColors, mQuality, mFrameDelay) != 0) {
Timber.e("Gifflen init failed");
return;
}

bitmap.recycle();

for (String bitmapPath : bitmapPaths) {

bitmap = howLong("Retrieving bitmap", () -> BitmapUtils.retrieve(bitmapPath));

final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final int[] pixels = new int[width * height];
final Bitmap finalBitmap = bitmap; // for counting time
howLongVoid("Retrieving pixels", () -> finalBitmap.getPixels(pixels, 0, width, 0, 0, width, height));
howLongVoid("Adding frame", () -> addFrame(pixels));

bitmap.recycle();
}
bitmap = null;
close();
}

NbadalGifConverter.java(仅转换函数):

  @Override public void createGifFile(String path, List<String> bitmapsNames) {

final ByteArrayOutputStream bos = new ByteArrayOutputStream();

final AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.setDelay(mDelay);
encoder.setQuality(mQuality);
encoder.start(bos);

for (String bitmapName : bitmapsNames) {
final Bitmap bitmap = howLong("Retrieving bitmap", () -> BitmapUtils.retrieve(bitmapName));
howLongVoid("Adding frame", () -> encoder.addFrame(bitmap));
}

encoder.finish();
FileUtils.store(bos.toByteArray(), path.substring(0, path.lastIndexOf('.')) + ".gif");
}

我愿意向您展示另一段相关代码。如果有任何帮助,我将不胜感激。

[更新]

检索位图的日志:

D/TimeUtils: Retrieving bitmap executed in 3ms
D/TimeUtils: Retrieving bitmap executed in 3ms
D/TimeUtils: Retrieving bitmap executed in 4ms

最佳答案

首先我要感谢@Spektre 的回答:Effective gif/image color quantization?

我和我的同事刚刚将它从 C++ 翻译成 Java。它在 4 倍的时间内显示出良好的结果。我会尝试改进它,但这已经比 AnimatedGifEncoder.java(我以前用过)好得多

代码如下:

public static final int MAX_COLOR_COUNT = 65536;

/**
* @param pixels rgb 888
* @param palette int[256]
* @return indices of colors in palette
*/
private int[][][] createPalette(int[] pixels, int[] palette) {

final int[] histogram = new int[MAX_COLOR_COUNT]; // pixel count histogram
final int[] indices = new int[MAX_COLOR_COUNT]; // here index is color value

for (int i = 0; i < MAX_COLOR_COUNT; i++) {
indices[i] = i;
}

// creating histogram
for (int color : pixels) {
// 0001 1111 0111 1110 0000 1111 1000 0000 0000
color = ((color >> 3) & 0x1F) | ((color >> 5) & 0x7E0) | ((color >> 8) & 0xF800);
if (histogram[color] < Integer.MAX_VALUE) { // picture must be really big
histogram[color]++;
}
}

// removing zeros
int j = 0;
for (int i = 0; i < MAX_COLOR_COUNT; i++) {
histogram[j] = histogram[i];
indices[j] = indices[i];
if (histogram[j] != 0) {
j++;
}
}
final int histograms = j;

// bubble sort
for (int i = 1; i != 0; ) {
i = 0;
for (int x = 0, y = 1; y < histograms; x++, y++) {
if (histogram[x] < histogram[y]) {
i = histogram[x];
histogram[x] = histogram[y];
histogram[y] = i;
i = indices[x];
indices[x] = indices[y];
indices[y] = i;
i = 1;
}
}
}

final int[][][] colorMap = new int[32][64][32];

int colorTableIndex = 0, x = 0;
for (; x < histograms; x++) { // main colors
final int color = indices[x];
// 1f (16) = 0001 1111 (2)
// 3f (16) = 0011 1111 (2)
// (1111 1)(111 111)(1 1111)
final int b = color & 0x1f;
final int g = (color >> 5) & 0x3f;
final int r = (color >> 11) & 0x1f;

// skip if similar color already in palette[]
int a = 0, i = 0;
for (; i < colorTableIndex; i++) {
final byte tempB = (byte) ((palette[i] >> 3) & 0x1f);
final byte tempG = (byte) ((palette[i] >> 10) & 0x3f);
final byte tempR = (byte) ((palette[i] >> 19) & 0x1f);

// if difference between two colors is pretty small
// taxicab distance
int difference = tempB - b;
if (difference < 0) {
difference = -difference;
}
a = difference;
difference = tempG - g;
if (difference < 0) {
difference = -difference;
}
a += difference;
difference = tempR - r;
if (difference < 0) {
difference = -difference;
}
a += difference;
if (a <= 2) { // smaller than 16/8
a = 1;
break;
}
a = 0;
}

if (a != 0) {
colorMap[r][g][b] = i; // map to existing color
} else {
colorMap[r][g][b] = colorTableIndex; // map to new index

// 1111 1000 1111 1100 1111 1000
palette[colorTableIndex] = b << 3 | (g << 10) | (r << 19); // fill this index with new color
colorTableIndex++;
if (colorTableIndex >= 256/*palette.length*/) {
x++;
break;
}
}
} // colorTableIndex = new color table size

for (; x < histograms; x++) { // minor colors

final int color = indices[x];

final int b = color & 0x1f;
final int g = (color >> 5) & 0x3f;
final int r = (color >> 11) & 0x1f;

// find closest color
int minDistance = -1;
int colorIndex = 0;
for (int a, i = 0; i < colorTableIndex; i++) {
final byte tempB = (byte) ((palette[i] >> 3) & 0x1f);
final byte tempG = (byte) ((palette[i] >> 10) & 0x3f);
final byte tempR = (byte) ((palette[i] >> 19) & 0x1f);

int difference = tempB - b;
if (difference < 0) {
difference = -difference;
}
a = difference;
difference = tempG - g;
if (difference < 0) {
difference = -difference;
}
a += difference;
difference = tempR - r;
if (difference < 0) {
difference = -difference;
}
a += difference;
if ((minDistance < 0) || (minDistance > a)) {
minDistance = a;
colorIndex = i;
}
}
colorMap[r][g][b] = colorIndex;
}

return colorMap;
}

private byte[] map(int[] pixels, int[][][] colorMap) {
final int pixelsLength = pixels.length;

final byte[] mapped = new byte[pixelsLength];
for (int i = 0; i < pixelsLength; i++) {
final int color =
((pixels[i] >> 3) & 0x1F) | ((pixels[i] >> 5) & 0x7E0) | ((pixels[i] >> 8) & 0xF800);

final int b = color & 0x1f;
final int g = (color >> 5) & 0x3f;
final int r = (color >> 11) & 0x1f;

mapped[i] = (byte) colorMap[r][g][b];
}
return mapped;
}

关于java - 将 jpeg 转换为 gif 太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33941767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com