Android人脸识别Demo竖屏YUV方向调整和图片保存
来源:互联网 发布:windows 驱动 路径 编辑:程序博客网 时间:2024/06/05 05:56
本博客包含三个常用方法,用于盛开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()); } }
阅读全文
1 0
- Android人脸识别Demo竖屏YUV方向调整和图片保存
- android 通过Camera和Surfaceview实现照相 图片预览 保存图片到本地 手动聚焦 FaceDetectionListener实现人脸识别
- Android中使用MediaCodec硬件解码,高效率得到YUV格式帧,快速保存JPEG图片(不使用OpenGL)(附Demo)
- Android静态图片人脸识别的完整demo(附完整源码)
- Android静态图片人脸识别的完整demo(附完整源码)
- Android保存和识别手势
- android:调整摄像头方向
- 人脸识别DEMO
- 人脸识别demo
- Android图片处理:识别图像方向并显示
- Android图片处理:识别图像方向并显示
- Android图片处理:识别图像方向并显示
- Android图片处理:识别图像方向并显示
- Android5.1显示YUV格式图片 Demo
- Android5.1显示YUV格式图片 Demo
- OpenCV-2.4.6-android-sdk 人脸识别demo搭建
- 保存YUV和保存bmp文件
- iOS活体人脸识别的Demo和一些思路
- DNS笔记
- 最近工作好忙啊!!!
- JS点击事件删除单行数据
- mac常用终端命令
- [Python]
- Android人脸识别Demo竖屏YUV方向调整和图片保存
- SDUT 2711 4-2 电子时钟中的运算符重载
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE
- jdk8 类和接口的多继承详解
- java线程池之小结
- 百度编辑器idea中 jsp运用
- monkeyrunnerdevice api
- Linux终极shell-Z Shell--用强大的zsh & oh-my-zsh把Bash换掉
- git mac自动补全