- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以使用 RenderScript 拍摄 Y'UV 格式的相机图像:
最佳答案
是的!我想出了如何并认为我会与他人分享。 RenderScript 有一点学习曲线,更简单的示例似乎会有所帮助。
裁剪时,您仍然需要设置输入和输出分配以及脚本本身的分配。一开始可能看起来很奇怪,但输入和输出分配必须具有相同的大小,因此如果您正在裁剪,则需要设置另一个分配来写入裁剪后的输出。稍后会详细介绍。
#pragma version(1)
#pragma rs java_package_name(com.autofrog.chrispvision)
#pragma rs_fp_relaxed
/*
* This is mInputAllocation
*/
rs_allocation gInputFrame;
/*
* This is where we write our cropped image
*/
rs_allocation gOutputFrame;
/*
* These dimensions define the crop region that we want
*/
uint32_t xStart, yStart;
uint32_t outputWidth, outputHeight;
uchar4 __attribute__((kernel)) yuv2rgbFrames(uchar4 in, uint32_t x, uint32_t y)
{
uchar Y = rsGetElementAtYuv_uchar_Y(gInputFrame, x, y);
uchar U = rsGetElementAtYuv_uchar_U(gInputFrame, x, y);
uchar V = rsGetElementAtYuv_uchar_V(gInputFrame, x, y);
uchar4 rgba = rsYuvToRGBA_uchar4(Y, U, V);
/* force the alpha channel to opaque - the conversion doesn't seem to do this */
rgba.a = 0xFF;
uint32_t translated_x = x - xStart;
uint32_t translated_y = y - yStart;
uint32_t x_rotated = outputWidth - translated_y;
uint32_t y_rotated = translated_x;
rsSetElementAt_uchar4(gOutputFrame, rgba, x_rotated, y_rotated);
return rgba;
}
设置分配:
private fun createAllocations(rs: RenderScript) {
/*
* The yuvTypeBuilder is for the input from the camera. It has to be the
* same size as the camera (preview) image
*/
val yuvTypeBuilder = Type.Builder(rs, Element.YUV(rs))
yuvTypeBuilder.setX(mImageSize.width)
yuvTypeBuilder.setY(mImageSize.height)
yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888)
mInputAllocation = Allocation.createTyped(
rs, yuvTypeBuilder.create(),
Allocation.USAGE_IO_INPUT or Allocation.USAGE_SCRIPT)
/*
* The RGB type is also the same size as the input image. Other examples write this as
* an int but I don't see a reason why you wouldn't be more explicit about it to make
* the code more readable.
*/
val rgbType = Type.createXY(rs, Element.RGBA_8888(rs), mImageSize.width, mImageSize.height)
mScriptAllocation = Allocation.createTyped(
rs, rgbType,
Allocation.USAGE_SCRIPT)
mOutputAllocation = Allocation.createTyped(
rs, rgbType,
Allocation.USAGE_IO_OUTPUT or Allocation.USAGE_SCRIPT)
/*
* Finally, set up an allocation to which we will write our cropped image. The
* dimensions of this one are (wantx,wanty)
*/
val rgbCroppedType = Type.createXY(rs, Element.RGBA_8888(rs), wantx, wanty)
mOutputAllocationRGB = Allocation.createTyped(
rs, rgbCroppedType,
Allocation.USAGE_SCRIPT)
}
最后,由于您正在裁剪,因此需要在调用之前告诉脚本要做什么。如果图像大小没有改变,您可以通过移动 LaunchOptions 和变量设置来优化它,使它们只出现一次(而不是每次),但我将它们留在这里作为我的示例以使其更清楚。
override fun onBufferAvailable(a: Allocation) {
// Get the new frame into the input allocation
mInputAllocation!!.ioReceive()
// Run processing pass if we should send a frame
val current = System.currentTimeMillis()
if (current - mLastProcessed >= mFrameEveryMs) {
val lo = Script.LaunchOptions()
/*
* These coordinates are the portion of the original image that we want to
* include. Because we're rotating (in this case) x and y are reversed
* (but still offset from the actual center of each dimension)
*/
lo.setX(starty, endy)
lo.setY(startx, endx)
mScriptHandle.set_xStart(lo.xStart.toLong())
mScriptHandle.set_yStart(lo.yStart.toLong())
mScriptHandle.set_outputWidth(wantx.toLong())
mScriptHandle.set_outputHeight(wanty.toLong())
mScriptHandle.forEach_yuv2rgbFrames(mScriptAllocation, mOutputAllocation, lo)
val output = Bitmap.createBitmap(
wantx, wanty,
Bitmap.Config.ARGB_8888
)
mOutputAllocationRGB!!.copyTo(output)
/* Do something with the resulting bitmap */
listener?.invoke(output)
mLastProcessed = current
}
}
所有这一切可能看起来有点多,但它非常快 - 比在 java/kotlin 端进行旋转快得多,并且由于 RenderScript 能够在图像的子集上运行内核函数,它比创建的开销更少一个位图,然后创建第二个裁剪后的位图。
对我来说,所有的旋转都是必要的,因为 RenderScript 看到的图像从相机旋转了 90 度。有人告诉我这是拥有三星手机的某种特殊性。
RenderScript 起初令人生畏,但一旦您习惯了它的功能,它就没那么糟糕了。我希望这对某人有帮助。
关于renderscript - 如何在 Android RenderScript 中同时缩放、裁剪和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54310749/
OpenCL doesn't support recursion . CUDA does, but only from a certain version .初始搜索表明 RenderScript 确
是否可以使用 RenderScript 拍摄 Y'UV 格式的相机图像: 将其转换为 RGBA 裁剪到特定区域 必要时旋转它 最佳答案 是的!我想出了如何并认为我会与他人分享。 RenderScrip
我希望从我的 renderscript 内核返回一个结构数组。我的问题是,虽然我可以使用生成的代码在 java 中创建一个结构数组,并通过获取由此生成的数组的分配将其传递给我的根方法,但我无法将数组返
我似乎找不到任何关于如何检查 RenderScript 是否真正并行化代码的文档。我想知道是否正在使用 CPU 或 GPU 以及调度的线程数。 我唯一发现的是这个错误报告: http://code.g
我想用 renderscript 做一些实验,所以我从 sdk 附带的示例开始,但不幸的是我无法编译它。是否有任何我可能需要的额外工具来编译和构建该示例,我曾尝试阅读文档。但他们什么也没提到。 示例项
使用 gradle:3.3.0-alpha06 更新到 Android Studio 3.3 Canary 6 后,我开始收到错误 Cannot find file sdk\build-tools\2
我编写了以下渲染脚本: ushort* curve_hth; ushort* curve_hts; ushort* curve_htv; ushort* curve_sth; ushort* curv
我开始探索 renderscript 的强大功能。 尝试使用 2D 图像数据,我可以将像素转换为其他像素。但是,如何从输入分配中获取相邻像素? 我想知道例如内置的 convolve3x3 过滤器是如何
renderscript 做矩阵计算很强大,但是如果我想处理当前帧和前一帧之间的差异。 正确的做法是什么: 在 renderscript 中保存前一帧,然后在新帧进来后进行计算。 在 java 中保存
如何在调用 rsgDrawText 之前设置字体特性?我对能够设置字体大小特别感兴趣。 最佳答案 这是一个多部分的过程。在用于控制 RenderScript 运行时(通常拥有脚本的文件)的文件中,在初
我想知道是否可以使用 RenderScript 引擎来优化算法。 该算法对图像进行迭代处理,直到满足某些条件。在处理所需的迭代次数之前无法知道。但我只看到 RenderScript 示例在具有 N 个
我注意到我实际上可以对 ScriptIntrinsicBlur 的输入和输出使用相同的分配。 .由于我对未过滤的分配不感兴趣,因此这种方法不需要创建另一个分配并且在内存方面更好。 但是,它安全吗?我在
我可以在适用于 android 2.2 及更高版本的应用程序中使用 renderscript 吗?因为我知道它已被用于墙纸,但仅限于内部。那改变了吗,因为 renderscript 现在是公开的?我正
我找到了这个 link我想试试轮播的例子。我将所有类和 rs 文件(轮播示例中的所有内容)复制到我自己的项目中,并尝试在实际设备(摩托罗拉 xoom)上构建它。但我一直在强制关闭。 logcat 中的
是否可以在 RenderScript for Android 中使用二维数组?分配只允许我将一个向量(一维)数组放入分配中。但我不知道如何生成二维数组。我 found a google groups
我正在编写一小段 Renderscript 来动态拍摄图像并根据每个像素的 RGB 值将像素分类到“桶”中。桶的数量可能会有所不同,所以我的直觉是创建一个数组列表。显然,这在 Renderscript
我想更好地了解内存分配在 Renderscript 中的工作原理。 首先,我想确认内存是在运行时还是编译时分配的。 文档指出: Non-static, global variables that yo
为了对齐两个灰度图像的强度值(作为进一步处理的第一步),我编写了一个 Java 方法: 将两个图像的位图转换为两个包含位图强度的 int[] 数组(我在这里只取红色分量,因为它是灰度,即 r=g=b
无法理解,因为当我尝试将 Allocation 用作输入和输出时,程序崩溃了。 我已经检查过是否由于 android list 中的某种类型的权限,但都没有。 project.properties如下
我正在编写渲染脚本代码,我需要一些实用函数。但是当我编写这些函数并进行编译时,我收到一条错误消息,指出可调用函数必须返回 void。他们正在强制执行此操作,以便所有渲染脚本可调用函数都可以异步运行。但
我是一名优秀的程序员,十分优秀!