5
votes

I'm trying to decode android pictureCallback to grayscale mat:

Firstly I tried to use rawPicture callback but always I get null pointer.

 mCamera.takePicture(null, mPicture, null);

JpegCalback gives me not null byte[] array, but cvtColor conversion doesn't work.

mCamera.takePicture(null, null, mPicture);

My preview callback is this

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
        public void onPictureTaken(byte[] data, Camera camera) {

        Toast.makeText(getApplicationContext(),"picture taken",Toast.LENGTH_SHORT);
        int res=JniManager.setImage(data,m_width,m_height);
        mCamera.stopPreview();
        mCamera.startPreview();
    }
};

c++ JNI function:

    Java_pidaas_vicomtech_org_facerec_viulib_JniManager_setImage(JNIEnv *env,
                                                    jobject instance,
                                                    jbyteArray image,
                                                    jint width,
                                                    jint height)
     jbyte* _yuv  = env->GetByteArrayElements(image, 0);
     int len = env->GetArrayLength (image);
     unsigned char* buf = new unsigned char[len];
     env->GetByteArrayRegion (image, 0, len, reinterpret_cast<jbyte*>(buf));
     Mat rawData (cv::Size(width,height), CV_8UC3, buf );
            cv::Mat test =imdecode(rawData,CV_LOAD_IMAGE_COLOR);
            cv::Mat grey;
            cv::cvtColor(test, grey, cv::COLOR_BGR2GRAY);
            bool flag = imwrite("/sdcard/graypg.jpg", grey);
    env->ReleaseByteArrayElements(image, _yuv, 0);
}

I have found some answers but none of them works.

I write image on sdcard because of testing, but it writes an empty image of 0 bytes.

So, anybody knows how do I should get jpeg pixels as gray format?

1
What value contains len? I think I have a similary problem in the past because getarraylenght sometimes returns 0, the correct len could be calculate int multiplication of width x heightvgonisanz
You are right, the len is 0, I didn't pay attention because the image was corrupt but, I couldn't see any SIGSEV error. Thanks.uelordi

1 Answers

1
votes

Maybe the getarraylenght is not working propertly and is returning 0. I usually use lenght as width x height, parameters provided to calculate the array size. That will copy propertly the data from camera buffer to memory.

Warning: If something is wrong with the size, you will have an Signal 11 exception!