English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This blog includes three commonly used methods for sending YUV data when the Android version of the face recognition demo is used in portrait mode, but it has always been unable to recognize.
1.First, you can try to rotate clockwise90° or270°, then send it into the recognition SDK.
2.If the rotation direction is still not recognizable, you can try saveImg() to save the local image for inspection to see if it meets the requirements.
/** * Video is rotated clockwise90 * This method is only used when the screen is in portrait mode * */ 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 image data * @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 { //Convert yuv to 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()); //Save bitmap to local fOut = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); bmp.recycle(); stream.close(); } Log.e("Sys", "Error:") + ex.getMessage()); } }
The above Android face recognition Demo vertical screen YUV direction adjustment and picture saving (share) is all the content that the editor shares with everyone. I hope it can provide a reference for everyone, and also hope everyone will support the呐喊 tutorial more.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)