Android人脸识别Demo竖屏YUV方向调整和图片保存(分享)
作者:iblade 发布时间:2022-11-24 00:47:42
标签:Android,人脸识别,图片保存
本博客包含三个常用方法,用于盛开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());
}
}
来源:http://blog.csdn.net/iblade/article/details/78835467


猜你喜欢
- 以下实例演示了如何使用 retainAll () 方法来计算两个数组的交集:Main.java 文件:import java.util.Ar
- 本文实例讲述了WPF弹出自定义窗口的方法。分享给大家供大家参考,具体如下:测试环境:[1]VS2010SP1[2]WPF(.NET Fram
- 本文实例讲述了winform实现限制及解除鼠标移动范围的方法。分享给大家供大家参考。具体如下:限制鼠标的移动范围:// this.Curso
- 上一节我们完成了使用DataGrid显示所有商品信息,这节我们开始添加几个功能:添加、更新、删除和查询。首先我们实现下前台的显示,然后再做后
- b/s系统中对http请求数据的校验多数在客户端进行,这也是出于简单及用户体验性上考虑,但是在一些安全性要求高的系统中服务端校验是不可缺少的
- C#中List<T>中泛型T如果是一个对象的话,则利用Find函数返回的将是这个对象的指针,对其返回对象的属性进行操作,也会影响
- 一款书籍阅读器,需要以下功能才能说的上比较完整:文字页面展示,即书页;页面之间的跳转动画,即翻页动作;能够在每一页上记录阅读进度,即书签;能
- 首先我们看看为什么添加Watch。ZooKeeper是用来协调(同步)分布式进程的服务,提供了一个简单高性能的协调内核,用户可以在此之上构建
- Kotlin基础教程之数据类型一切都是对象.在Kotlin中一切都是对象.Kotlin有一些基本类型Boolean,Byte,Shot,In
- 一、三方库介绍地址:https://github.com/youth5201314/banner介绍:从学习 Android 开始到现在工作
- 在多线程对一个整数进行自增操作时,需要用synchronized进行同步。然而,如果synchronized的对象选取的不合适的话,就无法实
- 使用INI配置文件,简单便捷。该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引
- 1.下载JDK查看最新:http://www.oracle.com/technetwork/java/javase/downloads/in
- Java 中的运算符与 C 语言基本一致。1、算术运算符操作符描述例子+加法 : 相加运算符两侧的值A + B 等于 30-减法 : 左操作
- 一、Java并发是什么?用学术定义来说就是并发:同一时间段,多个任务都在执行 (单位时间内不一定同时执行);简单来说就是,同一个时间段,让计
- 1、Json的制作package com.example.usingjson2; import org.json.
- WPF实现滚动条还是比较方便的,只要在控件外围加上ScrollViewer即可,但美中不足的是:滚动的时候没有动画效果。在滚动的时候添加过渡
- 本文实例为大家分享了java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下完成人机猜拳互动游戏的开发,用户通过控制台输入实现出拳,电脑
- 前言支持圆形裁剪框,裁剪后生成圆形图案。代码基于开源项目修改,github上项目链接:https://github.com/shengge/
- 一、MyBatis缓存介绍正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持1.一级缓存:基于PerpetualC