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

所属分类: 软件编程 / Android 阅读数: 39
收藏 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异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多