Android人脸识别Demo竖屏YUV方向调整和图片保存(分享)

所属分类: 软件编程 / Android 阅读数: 54
收藏 0 赞 0 分享

本博客包含三个常用方法,用于盛开Android版人脸识别Demo中竖屏使用时送入yuv数据,但一直无法识别的情况。

1.首先可以尝试顺时针旋转90°或270°,然后送入识别SDK。

2.旋转方向后依然无法识别时,可以尝试saveImg( ),保存本地检查图片是否符合要求。

 /**
  * 视频顺时针旋转90
  * 该方法仅仅在竖屏时候使用
  * */
 public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth,
          int imageHeight) {
  byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
  int i = 0;
  for (int x = 0; x < imageWidth; x++) {
   for (int y = imageHeight - 1; y >= 0; y--) {
    yuv[i] = data[y * imageWidth + x];
    i++;
   }
  }
  i = imageWidth * imageHeight * 3 / 2 - 1;
  for (int x = imageWidth - 1; x > 0; x = x - 2) {
   for (int y = 0; y < imageHeight / 2; y++) {
    yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
    i--;
    yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth)
      + (x - 1)];
    i--;
   }
  }
  return yuv;
 }

 public static byte[] YUV420spRotate270(byte[] src, int width, int height) {
  int count = 0;
  int uvHeight = height >> 1;
  int imgSize = width * height;
  byte[] des = new byte[imgSize * 3 >> 1];
  //copy y
  for (int j = width - 1; j >= 0; j--) {
   for (int i = 0; i < height; i++) {
    des[count++] = src[width * i + j];
   }
  }
  //u,v
  for (int j = width - 1; j > 0; j -= 2) {
   for (int i = 0; i < uvHeight; i++) {
    des[count++] = src[imgSize + width * i + j - 1];
    des[count++] = src[imgSize + width * i + j];
   }
  }
  return des;
 }
 private int i = 1;
 private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/0Face/";
 private Calendar now = new GregorianCalendar();
 private SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
 private String fileName = simpleDate.format(now.getTime());

 /**
  * @param data yuv图像数据
  * @param width 
  * @param height
  */
 public void saveImg(byte[] data, int width, int height) {
  File dir = new File(path);
  if (!dir.exists()) dir.mkdirs();
  File f = new File(path + (fileName + "-" + i++) + ".jpg");
  FileOutputStream fOut = null;
  try {
   //yuv转成bitmap
   YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null);
   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   image.compressToJpeg(new Rect(0, 0, width, height), 80, stream);
   Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
   //bitmap保存至本地
   fOut = new FileOutputStream(f);
   bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
   fOut.flush();
   fOut.close();
   bmp.recycle();
   stream.close();
  } catch (Exception ex) {
   Log.e("Sys", "Error:" + ex.getMessage());
  }
 }

以上这篇Android人脸识别Demo竖屏YUV方向调整和图片保存(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多