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
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 1.)说明.本项目是来自github上的一个项目roottools (https://github.com/Stericson/RootTo
- 前言Android Studio是Google开发的一款面向Android开发者的IDE,支持Windows、Mac、Linux等操作系统,
- 本文实例为大家分享了Android Walker登录记住密码页面的具体代码,供大家参考,具体内容如下目标效果: 这一次修改的不多,
- 前段时间由于VSS上的一个项目要给2个公司开发使用,而2个公司的需求不同 就把该项目复制到VSS上的另外一个目录,结果在别人的电脑取出并打开
- 网上android播放器虽然挺多,感觉提供的歌词显示功能比较死板,要么搜索给的条件死死的,要么放置sdcard内部的歌词格式需要统一,应该提
- 本文实例讲述了Android实现向Launcher添加快捷方式的方法。分享给大家供大家参考。具体如下:当我们在应用程序Launcher的桌面
- For-Each循环For-Each循环也叫增强型的for循环,或者叫foreach循环。For-Each循环是JDK5.0的新特性(其他新
- 为什么要限流在保证可用的情况下尽可能多增加进入的人数,其余的人在排队等待,或者返回友好提示,保证里面的进行系统的用户可以正常使用,防止系统雪
- 1.java后台(1)使用BigDecimal类方式一:String str=new BigDecimal(num+""
- 本文实例讲述了Android桌面插件App Widget用法。分享给大家供大家参考,具体如下:应用程序窗口小部件App Widgets应用程
- 在官方的这篇文档中为大家介绍了如何使用Java开启Azure Windows虚拟机的诊断设置这篇文章呢,为大家介绍一下如何使用Java开启L
- ////////////////////////////
- TabControl控件中TabPage选项卡切换时的触发事件选项卡切换触发的是TabControl控件的SelectedIndexChan
- 本文实例讲述了C#设置软件开机自动运行的方法。分享给大家供大家参考,具体如下:#region/// <summary>/// 开
- 1、fragment简介我对fragment的理解是基于activity的,对于大多数的基本开始发时,我们最先遇到的就是用activity来
- 前言关键字Final不仅可以用来修饰变量,而且对类及其方法的继承也有很大的影响,本文将从类与方法两个方面介绍final关键字的功能。Fina
- 日期类计算器的模拟实现::1.获取某年某月的天数int GetMonthDay(int year, int month){static in
- 和Android UI layout一样,我们也可以在XML中定义应用程序的菜单。通过在菜单的onCreateOptionsMenu方法中膨
- 混合开发简介使用Flutter从零开始开发App是一件轻松惬意的事情,但对于一些成熟的产品来说,完全摒弃原有App的历史沉淀,全面转向Flu
- 以下实例演示了如何通过 Collections 类的 Collections.max() 和 Collections.min() 方法来查找