人臉識(shí)別開發(fā)

注意點(diǎn):
1.使用camera時(shí),畫面始終無法旋轉(zhuǎn)。
有可能是自己還不太熟悉的原因。
最終使用了camera1+TextureView,
因?yàn)檫@個(gè)Camera.setDisplayOrientationAPI很香啊。哈哈

2.onPreviewFrame得到的數(shù)據(jù) 要進(jìn)行翻轉(zhuǎn)的。

public class NV21Utils {
    //如果進(jìn)行編碼,注意90、270時(shí),寬高要進(jìn)行對(duì)換。
    public static byte[] NV21_rotate_to_270(byte[] nv21_data, int width, int height) {
        int y_size = width * height;
        int buffser_size = y_size * 3 / 2;
        byte[] nv21_rotated = new byte[buffser_size];
        int i = 0;

// Rotate the Y luma
        for (int x = width - 1; x >= 0; x--) {
            int offset = 0;
            for (int y = 0; y < height; y++) {
                nv21_rotated[i] = nv21_data[offset + x];
                i++;
                offset += width;
            }
        }

// Rotate the U and V color components
        i = y_size;
        for (int x = width - 1; x > 0; x = x - 2) {
            int offset = y_size;
            for (int y = 0; y < height / 2; y++) {
                nv21_rotated[i] = nv21_data[offset + (x - 1)];
                i++;
                nv21_rotated[i] = nv21_data[offset + x];
                i++;
                offset += width;
            }
        }
        return nv21_rotated;
    }

    public static byte[] NV21_rotate_to_180(byte[] nv21_data, int width, int height) {
        int y_size = width * height;
        int buffser_size = y_size * 3 / 2;
        byte[] nv21_rotated = new byte[buffser_size];
        int i = 0;
        int count = 0;
        for (i = y_size - 1; i >= 0; i--) {
            nv21_rotated[count] = nv21_data[i];
            count++;
        }
        for (i = buffser_size - 1; i >= y_size; i -= 2) {
            nv21_rotated[count++] = nv21_data[i - 1];
            nv21_rotated[count++] = nv21_data[i];
        }
        return nv21_rotated;
    }

    public static byte[] NV21_rotate_to_90(byte[] nv21_data, int width, int height) {
        int y_size = width * height;
        int buffser_size = y_size * 3 / 2;
        byte[] nv21_rotated = new byte[buffser_size];
// Rotate the Y luma
        int i = 0;
        int startPos = (height - 1) * width;
        for (int x = 0; x < width; x++) {
            int offset = startPos;
            for (int y = height - 1; y >= 0; y--) {
                nv21_rotated[i] = nv21_data[offset + x];
                i++;
                offset -= width;
            }
        }

// Rotate the U and V color components
        i = buffser_size - 1;
        for (int x = width - 1; x > 0; x = x - 2) {
            int offset = y_size;
            for (int y = 0; y < height / 2; y++) {
                nv21_rotated[i] = nv21_data[offset + x];
                i--;
                nv21_rotated[i] = nv21_data[offset + (x - 1)];
                i--;
                offset += width;
            }
        }
        return nv21_rotated;
    }
}

需要注意的是。如果使用了之前攝像頭翻轉(zhuǎn)了90或者270,那么這里的寬高需要對(duì)換
也就是說,編碼出來的byte[],你如果想要轉(zhuǎn)換成bitmap,那么你之前的width和height需要調(diào)換位置。
3.onPreviewFrame出來的數(shù)據(jù)太快了。靈魂追不上
我使用了rxjava的方式進(jìn)行攔截,1S執(zhí)行一次

 Observable.create(ObservableOnSubscribe<ByteArray> { emitter ->
            sender = emitter
        }).throttleLast(1, TimeUnit.SECONDS)
            .subscribe(sendDataObserver!!)
override fun onPreviewFrame(data: ByteArray, camera: Camera) {
        val params = camera.parameters.previewSize;
        if (mWidth != params.width) {
            Logger.i("尺寸不一樣,重新進(jìn)行校正")
            mWidth = params.width;
            mHeight = params.height;
        }
        sender?.onNext(data);
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容