我一直在考虑转换从 onPreviewFrame() 获得的 NV21 byte[]。我在论坛和谷歌上搜索了各种解决方案。我尝试过 RenderScripts 和其他一些代码示例。其中一些给我一个带有黄色色调的图像,一些给我一个红色和蓝色翻转的图像(当我在代码中将其翻转回来后,我得到黄色色调),一些给我整个图像奇怪的颜色特征(几乎就像底片一样),有些给我一个灰度图像,有些给我一个太暗的图像,你无法真正看清任何东西。
由于我是输入问题的人,我意识到我一定是房间里的白痴,所以我们将从 this post 开始。这个特殊的解决方案给了我一个非常黑暗的图像,但我还不够酷,无法发表评论。有没有人尝试过这种解决方案,或者有一种解决方案可以生成与原始 NV21 格式具有相同质量的图像?
我需要一个有效的 ARGB byte[] 或一个有效的位图,我可以修改我的项目来处理其中任何一个。仅供引用,我尝试过这些(以及其他一些实际上只是这些的副本):
最佳答案
如果您尝试将 YUV 从相机转换为位图,您可以尝试以下操作:
// import android.renderscript.*
// RenderScript mRS;
// ScriptIntrinsicYuvToRGB mYuvToRGB;
// Allocation yuvPreviewAlloc;
// Allocation rgbOutputAlloc;
// Create RenderScript context, ScriptIntrinsicYuvToRGB and Allocations and keep reusing them.
if (NotInitialized) {
mRS = RenderScript.create(this).
mYuvToRGB = ScriptIntrinsicYuvToRGB.create(mRS, Element.YUV(mRS));
// Create a RS Allocation to hold NV21 data.
Type.Builder tYuv = new Type.Builder(mRS, Element.YUV(mRS));
tYuv.setX(width).setY(height).setYuvFormat(android.graphics.ImageFormat.NV21);
yuvPreviewAlloc = Allocation.createTyped(mRS, tYuv.create(), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_INPUT);
// Create a RS Allocation to hold RGBA data.
Type.Builder tRgb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
tRgb.setX(width).tRgb(height);
rgbOutputAlloc = Allocation.createTyped(mRS, tRgb.create(), Allocation.USAGE_SCRIPT);
// Set input of ScriptIntrinsicYuvToRGB
mYuvToRGB.setInput(yuvPreviewAlloc);
}
// Use rsPreviewSurface as one of the output surface from Camera API.
// You can refer to https://github.com/googlesamples/android-HdrViewfinder/blob/master/Application/src/main/java/com/example/android/hdrviewfinder/HdrViewfinderActivity.java#L504
Surface rsPreviewSurface = yuvPreviewAlloc.getSurface();
...
// Whenever a new frame is available
// Update the yuv Allocation with a new Camera buffer without any copy.
// You can refer to https://github.com/googlesamples/android-HdrViewfinder/blob/master/Application/src/main/java/com/example/android/hdrviewfinder/ViewfinderProcessor.java#L109
yuvPreviewAlloc.ioReceive();
// The actual Yuv to Rgb conversion.
mYuvToRGB.forEach(rgbOutputAlloc);
// Copy the rgb Allocation to a Bitmap.
rgbOutputAlloc.copyTo(mBitmap);
// continue processing mBitmap.
...
关于android - Android 上的 NV21 转位图,图像非常暗、灰度还是黄色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39753777/