Android- convert ARGB_8888 bitmap to 3BYTE_BGR

后端 未结 3 1396
广开言路
广开言路 2021-01-14 08:03

I get the pixel data of my ARGB_8888 bitmap by doing this:

public void getImagePixels(byte[] pixels, Bitmap image) {
    // calculate how many bytes our imag         


        
3条回答
  •  情书的邮戳
    2021-01-14 08:33

    With OpenCV library and you getting pixels via different method (see below) you can replace java function with native calls and it is ~4x more fastest:

    In summary:

    // reading bitmap from java side:
    Mat mFrame = Mat(height,width,CV_8UC4,pFrameData).clone();
    Mat mout;
    cvtColor(mFrame, mout,CV_RGB2GRAY); // or CV_RGB2XXX (3BGR)
    

    Complete example:

    Java side:

        Bitmap bitmap = mTextureView.getBitmap(mWidth, mHeight);
        int[] argb = new int[mWidth * mHeight];
        // get ARGB pixels and then proccess it with 8UC4 opencv convertion
        bitmap.getPixels(argb, 0, mWidth, 0, 0, mWidth, mHeight);
        // native method (NDK or CMake)
        processFrame8UC4(argb, mWidth, mHeight);
    

    Native side (NDK):

    JNIEXPORT jint JNICALL com_native_detector_Utils_processFrame8UC4
        (JNIEnv *env, jobject object, jint width, jint height, jintArray frame) {
    
        jint *pFrameData = env->GetIntArrayElements(frame, 0);
        // it is the line:
        Mat mFrame = Mat(height,width,CV_8UC4,pFrameData).clone();
        // the next only is a extra example to gray convertion:
        Mat mout;
        cvtColor(mFrame, mout,CV_RGB2GRAY); // or CV_RGB2XXX
        // your code from here, the next is a example:
        int objects = face_detection(env, mout);
        // release object
        env->ReleaseIntArrayElements(frame, pFrameData, 0);
        return objects;
    }
    

提交回复
热议问题