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

所属分类: 软件编程 / Android 阅读数: 71
收藏 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方向调整和图片保存(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

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

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多