Android jpeg pictureCallback grayscale Opencv Mat

I am trying to decode an android pictureCallback picture in a grayscale rug:

At first I tried using the rawPicture callback, but always get a null pointer.

mCamera.takePicture(null, mPicture, null); 

JpegCalback gives me not a null [] byte, but the cvtColor conversion does not work.

 mCamera.takePicture(null, null, mPicture); 

My view callback is

 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 found the answers, but none of them work.

I write an image on an SD card due to testing, but it writes an empty image of 0 bytes.

So, does anyone know how to get jpeg pixels in gray format?

+5
source share
1 answer

Maybe getarraylenght is not working properly and returns 0. I usually use the length as the width x height, the parameters provided to calculate the size of the array. This will correctly copy the data from the camera buffer to memory.

Warning: if something is wrong with the size, you will get a Signal 11 exception!

+1
source

All Articles