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

/**
* 視頻順時(shí)針旋轉(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人臉識(shí)別Demo豎屏YUV方向調(diào)整和圖片保存(分享)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android canvas畫(huà)圖操作之切割畫(huà)布實(shí)現(xiàn)方法(clipRect)
這篇文章主要介紹了Android canvas畫(huà)圖操作之切割畫(huà)布實(shí)現(xiàn)方法,通過(guò)clipRect方法實(shí)現(xiàn)canvas畫(huà)布的切割操作,需要的朋友可以參考下2016-10-10
Android registerForActivityResult動(dòng)態(tài)申請(qǐng)權(quán)限案例詳解
這篇文章主要介紹了Android registerForActivityResult動(dòng)態(tài)申請(qǐng)權(quán)限案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
TableLayout(表格布局)基礎(chǔ)知識(shí)點(diǎn)詳解
在本文里我們給大家分享了關(guān)于TableLayout(表格布局)的相關(guān)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02
[Alibaba-ARouter]淺談簡(jiǎn)單好用的Android頁(yè)面路由框架
這篇文章主要介紹了[Alibaba-ARouter]淺談簡(jiǎn)單好用的Android頁(yè)面路由框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android復(fù)選框CheckBox與開(kāi)關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
這篇文章主要介紹了Android復(fù)選框CheckBox與開(kāi)關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
淺談Android手機(jī)聯(lián)系人開(kāi)發(fā)之增刪查改功能
這篇文章主要介紹了Android手機(jī)聯(lián)系人開(kāi)發(fā)之增刪查改功能,需要的朋友可以參考下2017-05-05

