上面文章说过了YUV格式拼接,拼接的数据就需要提前对图像进行缩放的功能
海康摄像头获取到了图像数据解码后,分辨率随着设想头的不同,会造成YUV格式图片分辨率不同,就需要对图片进行缩放,YUV的缩放就用到LibYuv.dll动态库,动态库下提供个Scale方法,此方法为C#的导入方法
[DllImport(_path, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int Scale(byte* src_y,
byte* src_u,
byte* src_v,
int src_stride_y,
int src_stride_u,
int src_stride_v,
int src_width,
int src_height,
byte* dst_y,
byte* dst_u,
byte* dst_v,
int dst_stride_y,
int dst_stride_u,
int dst_stride_v,
int dst_width,
int dst_height,
int interpolate);
C#下封装的缩放方法,缩放需要对YUV数据的分量进行分解后,获得放大或缩小后待使用YUV数据
/// <summary>
/// YUV缩放
/// </summary>
/// <param name="_syuv"></param>
/// <param name="_dyuv"></param>
/// <param name="_swidth"></param>
/// <param name="_sheight"></param>
/// <param name="_dwidth"></param>
/// <param name="_dheight"></param>
public void YuvScale(byte[] _syuv, ref byte[] _dyuv, int _swidth, int _sheight,
int _dwidth, int _dheight)
{
try
{
int s_ylen = _swidth * _sheight;
int s_ulen = Convert.ToInt32(s_ylen * 0.25);
int s_vlen = s_ulen;
byte[] s_ydata = new byte[s_ylen];
Array.ConstrainedCopy(_syuv, 0, s_ydata, 0, s_ylen);
byte[] s_udata = new byte[s_ulen];
Array.ConstrainedCopy(_syuv, s_ylen, s_udata, 0, s_ulen);
byte[] s_vdata = new byte[s_vlen];
Array.ConstrainedCopy(_syuv, s_ylen + s_ulen, s_vdata, 0, s_vlen);
int s_ystride = _swidth;
int s_ustride = (_swidth + 1) / 2;
int s_vstride = s_ustride;
int d_ylen = _dwidth * _dheight;
int d_ulen = d_ylen / 4;
int d_vlen = d_ulen;
byte[] d_ydata = new byte[d_ylen];
byte[] d_udata = new byte[d_ulen];
byte[] d_vdata = new byte[d_vlen];
Array.ConstrainedCopy(_syuv, s_ylen + s_ulen, s_vdata, 0, s_vlen);
int d_ystride = _dwidth;
int d_ustride = (_dwidth + 1) / 2;
int d_vstride = d_ustride;
fixed (byte* bs_ydata = s_ydata)
fixed (byte* bs_udata = s_udata)
fixed (byte* bs_vdata = s_vdata)
fixed (byte* bd_ydata = d_ydata)
fixed (byte* bd_udata = d_udata)
fixed (byte* bd_vdata = d_vdata)
{
LibYuv.Scale(bs_ydata, bs_udata, bs_vdata, s_ystride, s_ustride, s_vstride, _swidth, _sheight,
bd_ydata, bd_udata, bd_vdata, d_ystride, d_ustride, d_vstride, _dwidth, _dheight, 0);
}
_dyuv = new byte[_dwidth * _dheight * 3 / 2];
Array.ConstrainedCopy(d_ydata, 0, _dyuv, 0, d_ylen);
Array.ConstrainedCopy(d_udata, 0, _dyuv, d_ylen, d_ulen);
Array.ConstrainedCopy(d_vdata, 0, _dyuv, d_ylen + d_ulen, d_vlen);
}
catch (Exception ex)
{
}
}
文章评论