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

作者:iblade 时间:2022-11-24 00:47:42 

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

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

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

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


/**
 * 视频顺时针旋转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());
 }
}

来源:http://blog.csdn.net/iblade/article/details/78835467

标签:Android,人脸识别,图片保存
0
投稿

猜你喜欢

  • 举例讲解Java的Spring框架中AOP程序设计方式的使用

    2022-11-21 21:22:15
  • android RecyclerView的一些优化点介绍

    2021-08-21 06:27:08
  • 在编程语言中怎样定义队列及其使用(C++)

    2021-05-28 14:00:43
  • SpringBoot参数校验Validator框架详解

    2023-09-22 07:08:40
  • springboot自动扫描添加的BeanDefinition源码实例详解

    2023-11-24 15:15:22
  • WPF如何利用附加属性修改ShowGridLines效果详解

    2023-04-01 06:32:04
  • Android通讯录开发之删除功能的实现方法

    2021-07-06 10:43:53
  • java实现在线预览--poi实现word、excel、ppt转html的方法

    2022-09-29 20:29:41
  • Netty分布式高性能工具类recycler的使用及创建

    2022-03-04 17:57:32
  • Android按钮单击事件的四种常用写法总结

    2023-07-15 09:05:18
  • Java实战之医院管理系统的实现

    2022-04-13 17:39:27
  • Android TextView控件文字添加下划线的实现方法

    2022-03-11 06:01:43
  • unity实现简单贪吃蛇游戏

    2023-05-25 23:59:53
  • 100行C#代码实现经典扫雷游戏

    2023-12-05 16:33:30
  • Java中tomcat memecached session 共享同步问题的解决办法

    2021-12-26 14:22:54
  • C#针对xml基本操作及保存配置文件应用实例

    2022-11-24 05:51:17
  • Android实战教程第七篇之如何在内存中存储用户名和密码

    2021-07-15 15:43:14
  • Maven工程打包jar的多种方式

    2022-12-15 06:54:46
  • Android Activity 不能被截屏的解决方法

    2021-06-28 08:37:35
  • JUnit 5中扩展模型的深入理解

    2023-05-13 05:27:23
  • asp之家 软件编程 m.aspxhome.com