- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我目前正在使用 Javacv它利用了 public void onPreviewFrame(byte[] data, Camera camera)
相机功能。
由于不推荐使用相机,我一直在研究 camera2和 MediaProjection .这两个库都使用 ImageReader class .
目前我用以下代码实例化这样一个 ImageReader
:
ImageReader.newInstance(DISPLAY_WIDTH, DISPLAY_HEIGHT, PixelFormat.RGBA_8888, 2);
并像这样附加一个 OnImageAvailableListener
:
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new processImage(reader.acquireNextImage()));
}
我已尝试按照以下线程将 RGBA_8888
格式与 Javacv 一起使用:https://github.com/bytedeco/javacv/issues/298但这对我不起作用。
所以我考虑使用 Render script转换这些 Image s 到 NV21(YUV_420_SP)
格式(这是 onPreviewFrame
函数中相机的默认输出),因为它适用于 camera
库.
我也看过 this one 之类的帖子和 this website进行转换,但这些对我不起作用,我担心它们会太慢。此外,我对 C 的了解非常有限。基本上看起来我想要 https://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB.html 的反向操作
那么,如何从 Image
转到与 onPreviewFrame
函数的输出相匹配的字节数组,即 NV21(YUV_420_SP)
格式?最好使用 Renderscript,因为它更快。
我曾尝试使用 ImageFormat.YUV_420_888
但无济于事。我不断收到诸如 生产者输出缓冲区格式 0x1 与 ImageReader 配置的缓冲区格式不匹配
之类的错误。我切换回 PixelFormat.RGBA_8888
并发现 ImageObject
中只有一个平面。该平面的字节缓冲区大小为 width*height*4(R、G、B、A 分别为一个字节)。所以我尝试将其转换为 NV21
格式。
我从 this answer 修改了代码产生以下功能:
void RGBtoNV21(byte[] yuv420sp, byte[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int A, R, G, B, Y, U, V;
int index = 0;
int rgbIndex = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
R = argb[rgbIndex++];
G = argb[rgbIndex++];
B = argb[rgbIndex++];
A = argb[rgbIndex++];
// RGB to YUV conversion according to
// https://en.wikipedia.org/wiki/YUV#Y.E2.80.B2UV444_to_RGB888_conversion
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor
// of 2 meaning for every 4 Y pixels there are 1 V and 1 U.
// Note the sampling is every other pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (i % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}
并使用以下方法调用它:
int mWidth = mImage.getWidth();
int mHeight = mImage.getHeight();
byte[] rgbaBytes = new byte[mWidth * mHeight * 4];
mImage.getPlanes()[0].getBuffer().get(rgbaBytes);
mImage.close();
byte[] yuv = new byte[mWidth * mHeight * 3 / 2];
RGBtoNV21(yuv, rgbaBytes, mWidth, mHeight);
这里的mImage
是我的ImageReader
生成的Image
对象。
然而,这会产生类似于此图像的结果:
这显然是错误的。似乎我的转换已关闭,但我无法弄清楚到底是什么。
最佳答案
@TargetApi(19)
public static byte[] yuvImageToByteArray(Image image) {
assert(image.getFormat() == ImageFormat.YUV_420_888);
int width = image.getWidth();
int height = image.getHeight();
Image.Plane[] planes = image.getPlanes();
byte[] result = new byte[width * height * 3 / 2];
int stride = planes[0].getRowStride();
assert (1 == planes[0].getPixelStride());
if (stride == width) {
planes[0].getBuffer().get(result, 0, width*height);
}
else {
for (int row = 0; row < height; row++) {
planes[0].getBuffer().position(row*stride);
planes[0].getBuffer().get(result, row*width, width);
}
}
stride = planes[1].getRowStride();
assert (stride == planes[2].getRowStride());
int pixelStride = planes[1].getPixelStride();
assert (pixelStride == planes[2].getPixelStride());
byte[] rowBytesCb = new byte[stride];
byte[] rowBytesCr = new byte[stride];
for (int row = 0; row < height/2; row++) {
int rowOffset = width*height + width/2 * row;
planes[1].getBuffer().position(row*stride);
planes[1].getBuffer().get(rowBytesCb);
planes[2].getBuffer().position(row*stride);
planes[2].getBuffer().get(rowBytesCr);
for (int col = 0; col < width/2; col++) {
result[rowOffset + col*2] = rowBytesCr[col*pixelStride];
result[rowOffset + col*2 + 1] = rowBytesCb[col*pixelStride];
}
}
return result;
}
我发表了another function有类似的要求。新实现试图利用这样一个事实,即 YUV_420_888 通常只是伪装的 NV21。
关于Android 使用渲染脚本将 ImageReader Image 转换为 YCbCr_420_SP (NV21) 字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39067828/
我有 powershell 脚本。通过调度程序,我运行 bat 文件,该文件运行 PS1 文件。 BAT文件 Powershell.exe -executionpolicy remotesigned
什么更快? 或者 $.getScript('../js/SOME.js', function (){ ... // with $.ajaxSetup({ cache: true });
需要bash脚本来显示文件 #!/bin/bash my_ls() { # save current directory then cd to "$1" pushd "$1" >/dev/nu
我有一个输入 csv 文件,实际上我需要在输入文件中选择第 2 列和第 3 列值,并且需要转换两个值的时区(从 PT 到 CT),转换后我需要替换转换后的时区值到文件。 注意: 所有输入日期值都在太平
我正在使用/etc/init.d/httpd 作为 init.d 脚本的模板。我了解文件中发生的所有内容,但以下行除外: LANG=$HTTPD_LANG daemon --pidfile=${pid
我有以下选择: python runscript.py -O start -a "-a "\"-o \\\"-f/dev/sda1 -b256k -Q8\\\" -l test -p maim\""
我对 shell 脚本完全陌生,但我需要编写一个 shell 脚本来检查文件是否存在,然后移动到另一个位置 这是我写的: 一旦设备崩溃,我就会在/storage/sdcard1/1 中收集日志 #!/
我正在使用 bash 脚本从文本文件中读取数据。 数据: 04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q
这是单击按钮时运行的 javascript 的结尾 xmlObj.open ('GET', /ajax.php, true); xmlObj.send (''); } 所以这会执行根目录中的php脚本
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我需要将文件转换为可读流以通过 api 上传,有一个使用 fs.createReadStream 的 Node js 示例。任何人都可以告诉我上述声明的 python 等价物是什么? 例子 const
我有一个 shell 脚本 cron,它从同一目录调用 python 脚本,但是当这个 cron 执行时,我没有从我的 python 脚本中获得预期的输出,当我手动执行它时,我的 python 脚本的
如何使 XMLHttpRequest (ajax) 调用的 php 脚本安全。 我的意思是,不让 PHP 文件通过直接 url 运行,只能通过脚本从我的页面调用(我不想向未登录的用户显示数据库结果,并
我正在尝试添加以下内容 我正在使用经典的 asp。但我不断收到的错误是“一个脚本 block 不能放在另一个脚本 block 内。”我尝试了此处的 document.write 技术:Javasc
如何从另一个 PHP 脚本(如批处理文件)中运行多个 PHP 脚本?如果我了解 include 在做什么,我认为 include 不会起作用;因为我正在运行的每个文件都会重新声明一些相同的函数等。我想
我想创建具有动态内容的网页。我有一个 HTML 页面,我想从中调用一个 lua 脚本 如何调用 lua 脚本? ? ? 从中检索数据?我可以做类似的事情吗: int xx = 0; xx
我删除了我的第一个问题,并重新编写了更多细节和附加 jSfiddle domos。 我有一个脚本,它运行查询并返回数据,然后填充表。表中的行自动循环滚动。所有这些工作正常,并通过使用以下代码完成。然而
我尝试使用 amp 脚本,但收到此错误: “[amp-script] 脚本哈希未找到。amp-script[script="hello-world"].js 必须在元[name="amp-script
我有一个读取输入的 Shell 脚本 #!/bin/bash echo "Type the year that you want to check (4 digits), followed by [E
我正在从 nodejs 调用 Lua 脚本。我想传递一个数组作为参数。我在 Lua 中解析该数组时遇到问题。 下面是一个例子: var script = 'local actorlist = ARGV
我是一名优秀的程序员,十分优秀!