android - 将 Flutter Frame Camera 转换为 android Bitmap
问题描述
我正在使用颤振插件相机(https://pub.dev/packages/camera)为 android 和 ios 开发颤振插件,所以首先我使用这种方法从插件流式传输帧:
controller.startImageStream((CameraImage img) {
runlOnFrame(
bytesList: img.planes.map((plane) {
return plane.bytes;
}).toList(),
imageHeight: img.height,
imageWidth: img.width,
)
}
runlOnFrame 方法是用 java 开发的,它可以将 YUV 字节转换为 RGBA 格式的位图,如下所示:
获取参数函数:
getArgs(HashMap args){
List<byte[]> bytesList = (ArrayList) args.get("bytesList");
int imageHeight = (int) (args.get("imageHeight"));
int imageWidth = (int) (args.get("imageWidth"));
}
runOnFrame(List<byte[]> bytesList, int imageHeight, int imageWidth, ) throws IOException {
int rotation = 90;
ByteBuffer Y = ByteBuffer.wrap(bytesList.get(0));
ByteBuffer U = ByteBuffer.wrap(bytesList.get(1));
ByteBuffer V = ByteBuffer.wrap(bytesList.get(2));
int Yb = Y.remaining();
int Ub = U.remaining();
int Vb = V.remaining();
byte[] data = new byte[Yb + Ub + Vb];
Y.get(data, 0, Yb);
V.get(data, Yb, Vb);
U.get(data, Yb + Vb, Ub);
Bitmap bitmapRaw = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
Allocation bmData = renderScriptNV21ToRGBA888(
mRegistrar.context(),
imageWidth,
imageHeight,
data);
bmData.copyTo(bitmapRaw);
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
Bitmap finalbitmapRaw = Bitmap.createBitmap(bitmapRaw, 0, 0, bitmapRaw.getWidth(), bitmapRaw.getHeight(), matrix, true);
saveBitm(finalbitmapRaw); // Function to save converted bitmap
}
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
// https://stackoverflow.com/a/36409748
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
in.copyFrom(nv21);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
return out;
}
问题是我得到这样的绿色位图图像:
解决方案
推荐阅读
- jquery - 结合 Wow.js 和 Slicebox.js 来创建动画滑块
- reactjs - ReactJS 上下文:通过上下文传递数组
- mysql - mysql中的“只读数据库”与“读写数据库”配置
- awk - unix 删除具有交换名称的重复项
- c# - 如何在ventuz中使用C#包管理器NuGet
- swift - xcassets 的 URL
- javascript - 无法使用 Selenium(无头)检索 Javascript 图表后面的数据/数组
- jquery-ui - JQuery UI 滑块未显示,控制台显示错误消息
- python - 订阅数组 python ros
- r - R计算字符串中的单词并存储在数组中