本博客包含三个常用方法,用于盛开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()); } }
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔