海康IPC接入TRTC时,从海康中获取的数据显示时色差不正确 ...

打印 上一主题 下一主题

主题 1048|帖子 1048|积分 3144

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
2021/1
记录海康IPC接入TRTC时的历史日记
 
从海康sdk接口获取数据,举行解码
  1. org.MediaPlayer.PlayM4.Player.T_YV12;
  2.     private void setDecodeCB() {
  3.         Player.getInstance().setDecodeCB(m_iPort, (nPort, data, nDataLen, nWidth, nHeight, nFrameTime, nDataType, arg7) -> {
  4.                 if (nDataType == T_YV12 ) {
  5.                     byte[] i420bytes = swapYV12toI420(data, nWidth, nHeight);
  6.                     callback.afterCall(context, i420bytes.length, i420bytes, nWidth, nHeight, nFrameTime);
  7.                 }
  8.         });
  9.     }
复制代码

解码后如下展示的时候,出现图片偏差
  1.     byte[] nv12bytes = RSNV12ToBitmap.getInstance(context).swapI420toNV12(data, nWidth, nHeight);//额外转换 3ms
  2.     Bitmap buffBitmap = RSNV12ToBitmap.getInstance(context).nv12ToBitmap(nv12bytes, nWidth, nHeight);//20毫秒
复制代码
可能是android.renderscript.ScriptIntrinsicYuvToRGB接受的YUV是nv21而不是nv12
  1.     byte[] nv21bytes = RSNV12ToBitmap.getInstance(context).swapI420toNV21(data, nWidth, nHeight);//额外转换 3ms
  2.     Bitmap buffBitmap = RSNV12ToBitmap.getInstance(context).nv21ToBitmap(nv21bytes, nWidth, nHeight);//20毫秒
复制代码
图像格式转换函数
  1. package com.sunshine.mhs.pension.doctormodule.trct.ipc.util;
  2. /**
  3. * 作者:Administrator on 2020/3/26
  4. * 邮箱:9611153@qq.com
  5. */
  6. import android.content.Context;
  7. import android.graphics.Bitmap;
  8. import android.renderscript.Allocation;
  9. import android.renderscript.Element;
  10. import android.renderscript.RenderScript;
  11. import android.renderscript.ScriptIntrinsicYuvToRGB;
  12. import android.renderscript.Type;
  13. public class RSNV21ToBitmap {
  14.     private static RSNV21ToBitmap instance;
  15.     private RenderScript rs;
  16.     private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
  17.     private byte[] nv21bytes;
  18.     private RSNV21ToBitmap(Context context) {
  19.         nv21bytes = null;
  20.         rs = RenderScript.create(context);
  21.         yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
  22.     }
  23.     public static RSNV21ToBitmap getInstance(Context context) {
  24.         if (instance == null) {
  25.             instance = new RSNV21ToBitmap( context);
  26.         }
  27.         return instance;
  28.     }
  29.     public static void destory() {
  30.          if (instance != null) {
  31.             instance.yuvToRgbIntrinsic.destroy();
  32.             instance.rs.destroy();
  33.             instance = null;
  34.         }
  35.     }
  36.     public Bitmap nv21ToBitmap(byte[] nv21, int width, int height) {
  37.         if (nv21 == null) return null;
  38.         Type.Builder yuvType, rgbaType;
  39.         Allocation in, out;
  40.         yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
  41.         in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
  42.         rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
  43.         out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
  44.         Bitmap bmpout = null;
  45.         try {
  46.             in.copyFrom(nv21);
  47.             yuvToRgbIntrinsic.setInput(in);
  48.             yuvToRgbIntrinsic.forEach(out);
  49.             bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  50.             out.copyTo(bmpout);
  51.         } catch (Exception e) {
  52.             e.printStackTrace();
  53.         }
  54.         return bmpout;
  55.     }
  56.     public byte[] swapI420toNV21(byte[] I420bytes, int width, int height) {
  57.         if (null == nv21bytes) {
  58.             nv21bytes = new byte[I420bytes.length];
  59.         }
  60.         if (I420bytes.length != nv21bytes.length)
  61.             return null;
  62.         int nLenY = width * height;
  63.         int nLenU = nLenY / 4;
  64.         System.arraycopy(I420bytes, 0, nv21bytes, 0, nLenY);
  65.         for (int i = 0; i < nLenU; i++) {
  66.             nv21bytes[nLenY + 2 * i + 1] = I420bytes[nLenY + i];//先v后U
  67.             nv21bytes[nLenY + 2 * i] = I420bytes[nLenY + nLenU + i];
  68.         }
  69.         return nv21bytes;
  70.     }
  71. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表