android rgb转yuv 接口,yuv转rgba接口说明.md

该博客介绍了如何通过SDK提供的接口实现YUV数据到RGB的转换,以便进行外部数据接收和自定义渲染。示例代码展示了如何设置UcloudRTCDataReceiver接口,用于接收转换后的RGBA数据,并将其保存为JPEG图片。同时,提供了开启外部输出的配置方法,关闭默认的显示方式。
摘要由CSDN通过智能技术生成

sdk 内部拉流数据的yuv 转rgb 数据接口 ,用来实现扩展的外部数据接收,自定义渲染。

```

public interface UcloudRTCDataReceiver {

//0-3 表示转换类型

//4-7 表示rgba_stride的宽度的倍数

//8-11 表示yuv_stride宽度移位数

//12-15 表示uv左移位数

public static final int I420_TO_ABGR = 0x01001040;

public static final int I420_TO_RGBA = 0x01001041;

/**

* 经过转换后输出到外部扩展的数据

* @param rgbBuffer 输出的数据

* @param width 输出数据的宽

* @param height 输出数据的高

*/

void onRecevieRGBAData(ByteBuffer rgbBuffer,int width ,int height);

/**

* 提供给sdk转换器的回调,告诉sdk需要转换成什么数据格式

* @return

*/

int getType();

/**

* sdk需要一块内存来缓存转换后的数据

* @return 存放转换数据的buffer

*/

ByteBuffer getCacheBuffer();

/**

* 释放缓存

*/

void releaseBuffer();

}

```

使用方式,打开外部输出方式,注释掉默认输出(显示到列表)方式

```

//外部扩展输出,和默认输出二选一

UCloudRtcSdkSurfaceVideoView videoViewCallBack = new UCloudRtcSdkSurfaceVideoView(getApplicationContext());

videoViewCallBack.setFrameCallBack(mUcloudRTCDataReceiver);

videoViewCallBack.init(false);

sdkEngine.startRemoteView(info, videoViewCallBack);

//默认输出,和外部输出代码二选一

// if (mVideoAdapter != null) {

// mVideoAdapter.addStreamView(mkey, vinfo, info);

// }

// if (vinfo != null && videoView != null) {

// sdkEngine.startRemoteView(info, videoView);

// videoView.refreshRemoteOp(View.VISIBLE);

// }

```

App调用者实现的接收数据的对象,只是一个参考,具体实现根据调用方的具体需求而来

```

private UcloudRTCDataReceiver mUcloudRTCDataReceiver = new UcloudRTCDataReceiver() {

private int limit = 0;

private ByteBuffer cache;

@Override

public void onRecevieRGBAData(ByteBuffer rgbBuffer, int width, int height) {

final Bitmap bitmap = Bitmap.createBitmap(width * 1, height * 1, Bitmap.Config.ARGB_8888);

bitmap.copyPixelsFromBuffer(rgbBuffer);

String name = "/mnt/sdcard/yuvrgba"+ limit+".jpg";

if (limit++ < 5) {

File file = new File(name);

try {

FileOutputStream out = new FileOutputStream(file);

if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {

out.flush();

out.close();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

@Override

public int getType() {

return UcloudRTCDataReceiver.I420_TO_ABGR;

}

@Override

public ByteBuffer getCacheBuffer() {

if(cache == null){

//根据需求来,设置最大的可能用到的buffersize,后续回调会复用这块存

int size = 4096*2160*4;

cache = sdkEngine.getNativeOpInterface().

createNativeByteBuffer(4096*2160*4);

}

cache.clear();

return cache;

}

@Override

public void releaseBuffer() {

if(cache != null)

sdkEngine.getNativeOpInterface().realeaseNativeByteBuffer(cache);

cache = null;

}

};

```

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,使用 Camera API 获取的图像数据通常是以 YUV 格式传输的。要将 YUV 数据换为 RGB,可以使用以下方法: 1. 使用 Android 提供的 YUV 换工具类:YuvImage 和 BitmapFactory。这种方法的缺点是比较耗时,而且需要将 YUV 数据先换为 JPEG 或 PNG 格式,然后再换为 RGB。代码示例: ``` YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out); byte[] imageBytes = out.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); ``` 2. 使用 RenderScript 进行 YUV 换。RenderScript 是 Android 提供的高性能计算引擎,可以在 GPU 上进行并行计算,比 CPU 要快很多。代码示例: ``` RenderScript rs = RenderScript.create(context); ScriptIntrinsicYuvToRGB yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length); Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); in.copyFrom(data); 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); yuvToRgb.setInput(in); yuvToRgb.forEach(out); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); out.copyTo(bitmap); rs.destroy(); ``` 注意:以上代码示例中的参数 data 是 byte[] 类型的 YUV 数据,width 和 height 分别是图像宽度和高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值