Android人臉識別Demo豎屏YUV方向調(diào)整和圖片保存(分享)
本博客包含三個常用方法,用于盛開Android版人臉識別Demo中豎屏使用時送入yuv數(shù)據(jù),但一直無法識別的情況。
1.首先可以嘗試順時針旋轉(zhuǎn)90°或270°,然后送入識別SDK。
2.旋轉(zhuǎn)方向后依然無法識別時,可以嘗試saveImg( ),保存本地檢查圖片是否符合要求。

/**
* 視頻順時針旋轉(zhuǎn)90
* 該方法僅僅在豎屏?xí)r候使用
* */
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圖像數(shù)據(jù)
* @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轉(zhuǎn)成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());
}
}
以上這篇Android人臉識別Demo豎屏YUV方向調(diào)整和圖片保存(分享)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程圖片操作類定義與用法示例【拍照,相冊選圖及裁剪】
這篇文章主要介紹了Android編程圖片操作類定義與用法,涉及Android拍照,相冊選圖及裁剪等圖片操作功能及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Android中ViewPager你所不知道的優(yōu)化技巧分享
提到ViewPager想必各位同學(xué)一點(diǎn)都不陌生,它是Android中最常用的組件之一,這篇文章小編就帶大家一起來看看ViewPager一些新的優(yōu)化方式吧2024-04-04
Android動態(tài)表格的實(shí)現(xiàn)代碼(內(nèi)容、樣式可擴(kuò)縮)
這篇文章主要介紹了Android動態(tài)表格的實(shí)現(xiàn)代碼(內(nèi)容、樣式可擴(kuò)縮),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
詳解Android中實(shí)現(xiàn)Redux方法
本篇文章給大家通過代碼實(shí)例教學(xué)Android中實(shí)現(xiàn)Redux的方法,有需要的朋友跟著參考下吧。2018-01-01
Android將Glide動態(tài)加載不同大小的圖片切圓角與圓形的方法
這篇文章主要給大家介紹了關(guān)于Android如何將Glide動態(tài)加載不同大小的圖片切圓角與圓形的方法,文中通過示例代碼介紹的非常吸納關(guān)系,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android開發(fā)之Fragment懶加載的幾種方式及性能對比
這篇文章主要介紹了Android開發(fā)之Fragment懶加載的幾種方式及性能對比的相關(guān)資料,具體詳細(xì)介紹需要的小伙伴可以參考下面文章內(nèi)容2022-05-05
Android基于Sqlite實(shí)現(xiàn)注冊和登錄功能
這篇文章主要為大家詳細(xì)介紹了Android基于Sqlite實(shí)現(xiàn)注冊和登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
flutter?Bloc?add兩次只響應(yīng)一次問題解析
這篇文章主要為大家介紹了flutter?Bloc?add兩次只響應(yīng)一次問題解析記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

