gpt4 book ai didi

java - 我们如何使 ExtractMpegFramesTest 中的 saveFrame() 方法更高效?

转载 作者:太空狗 更新时间:2023-10-29 16:38:48 27 4
gpt4 key购买 nike

[edit] 根据 fadden@ 建议重新格式化为问答格式。

ExtractMpegFramesTest_egl14.java.txt ,方法 saveFrame(),有一个循环用于将 RGBA 重新排序为 ARGB 以进行位图 png 压缩(请参阅该文件中的以下引述),如何对此进行优化?

// glReadPixels gives us a ByteBuffer filled with what is essentially big-endian RGBA
// data (i.e. a byte of red, followed by a byte of green...). We need an int[] filled
// with little-endian ARGB data to feed to Bitmap.
//

...

// So... we set the ByteBuffer to little-endian, which should turn the bulk IntBuffer
// get() into a straight memcpy on most Android devices. Our ints will hold ABGR data.
// Swapping B and R gives us ARGB. We need about 30ms for the bulk get(), and another
// 270ms for the color swap.

...

for (int i = 0; i < pixelCount; i++) {
int c = colors[i];
colors[i] = (c & 0xff00ff00) | ((c & 0x00ff0000) >> 16) | ((c & 0x000000ff) << 16);
}

最佳答案

事实证明,还有一种更快的方法。

使用@elmiguelao 的回答中的建议,我修改了 fragment 着色器来进行像素交换。这允许我从 saveFrame() 中删除交换代码。因为我不再需要内存中像素的临时副本,所以我完全消除了 int[] 缓冲区,从这里切换:

int[] colors = [... copy from mPixelBuf, swap ...]
Bitmap.createBitmap(colors, mWidth, mHeight, Bitmap.Config.ARGB_8888);

为此:

Bitmap bmp = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(mPixelBuf);

我一这样做,我所有的颜色都错了。

事实证明,Bitmap#copyPixelsFromBuffer() 需要 RGBA 顺序的像素,不是 ARGB 顺序。 glReadPixels() 的值已经是正确的格式。因此,通过这种方式,我避免了交换,避免了不必要的复制,并且根本不需要调整 fragment 着色器。

关于java - 我们如何使 ExtractMpegFramesTest 中的 saveFrame() 方法更高效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634450/

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