English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
本博客包含三个常用方法,用于在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をローカルに保存 bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); bmp.recycle(); stream.close(); Log.e("Sys", "Error:\ + ex.getMessage()); } }
以上のAndroid 人間認識Demo 垂直YUV 方向の調整と画像の保存(共有)が編集者が皆さんに提供する全ての内容です。皆さんに参考になれば幸いです。また、皆さんに多くのご支援と応援をお願いします。
声明:この記事の内容はインターネットからネットワークに転載されています。著作権は原著者に帰属します。コンテンツはインターネットユーザーにより自発的に貢献し、アップロードされています。このウェブサイトは所有権を持ちません。人工的な編集は行われていません。また、関連する法的責任を負いません。著作権侵害が疑われる場合は、以下のメールアドレスにご連絡ください:notice#oldtoolbag.com(メール送信時は、#を@に変更してください)で通報し、関連証拠を提供してください。一旦確認がとれましたら、このサイトは直ちに侵害される可能性のある内容を削除します。