Android开发之虹软人脸识别活体检测SDK包Bitmap转NV21方法

/**
* Bitmap 转化为 ARGB 数据,再转化为 NV21 数据
*
*  @param src 传入的 Bitmap,格式为 Bitmap.Config.ARGB_8888
*  @param width NV21 图像的宽度
*  @param height NV21 图像的高度
*  @return nv21 数据
*/
public static byte[] bitmapToNv21(Bitmap src, int width, int height)
{
if (src != null && src.getWidth() >= width && src.getHeight() >=
height) {
int[] argb = new int[width * height];
src.getPixels(argb, 0, width, 0, 0, width, height);
return  argbToNv21 (argb, width, height);
} else {
return null;
}
}
/**
* ARGB 数据转化为 NV21 数据
*
*  @param argb argb 数据
*  @param width 宽度
*  @param height 高度
*  @return nv21 数据
*/
private static byte[] argbToNv21(int[] argb, int width, int height) {
int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int index = 0;
byte[] nv21 = new byte[width * height * 3 / 2];
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
int R = (argb[index] & 0xFF0000) >> 16;
int G = (argb[index] & 0x00FF00) >> 8;
int B = argb[index] & 0x0000FF;
int Y = (66 * R + 129 * G + 25 * B + 128 >> 8) + 16;
int U = (-38 * R - 74 * G + 112 * B + 128 >> 8) + 128;
int V = (112 * R - 94 * G - 18 * B + 128 >> 8) + 128;
nv21[yIndex++] = (byte) (Y < 0 ? 0 : (Y > 255 ? 255 :
Y));
if (j % 2 == 0 && index % 2 == 0 && uvIndex < nv21.length
- 2) {
nv21[uvIndex++] = (byte) (V < 0 ? 0 : (V > 255 ?
255 : V));
nv21[uvIndex++] = (byte) (U < 0 ? 0 : (U > 255 ?
255 : U));
}
++index;
}
}
return nv21;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值