Ask Your Question

camera preview gives gray images

asked 2013-01-17 03:51:12 -0600

MoHaKa gravatar image

hi, i am developing android app using android sdk camera. on camera preview i need the frame content so i am using PreviewCallback to return the data in byte array, now my problem is saving the data in mat object the mat return gray images:

public void onPreviewFrame(byte[] data, Camera camera) {
    Mat src = new Mat(previewSize.height, previewSize.width, CvType.CV_8U, new Scalar(255));
    src.put(0, 0, data);
    Highgui.imwrite("/mnt/sdcard/1.jpg", src);

anybody can help me to generate argb images

note: i am using NV21 in preview image format.

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2013-01-17 04:50:10 -0600

rics gravatar image

Extending Alexander's correct answer here is a similar question with code the convert to RGB.

edit flag offensive delete link more


thank you alot your link helped me :)

MoHaKa gravatar imageMoHaKa ( 2013-01-17 06:16:50 -0600 )edit

answered 2013-01-17 04:28:42 -0600

data array is YUV frame. In most cases YUV420sp format is used. You use only Y component of frame. See any of Java OpenCV examples in OpenCV4Android SDK for more details.

edit flag offensive delete link more

answered 2013-01-17 06:26:55 -0600

Your code should look like this:

Rgba = new Mat();
Yuv = new Mat(previewSize.height + previewSize.height / 2, previewSize.width, CvType.CV_8UC1, data);
Gray = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);
Process(Gray, Rgba);
edit flag offensive delete link more


in java, mat contractor dosent contain byte[], so you cant put image data in contractor directly.

MoHaKa gravatar imageMoHaKa ( 2013-01-17 06:32:31 -0600 )edit

I'm sorry, you are right. Additional call Yuv.put(0, 0, data); is needed.

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-01-17 06:36:00 -0600 )edit

yes i have tried it and it worked, anyway thank you for answer!

MoHaKa gravatar imageMoHaKa ( 2013-01-17 06:57:52 -0600 )edit

answered 2013-01-17 05:04:27 -0600

tmanthey gravatar image

what's the scalar doing in the constructor. I am not using the java api, but try something like

Mat src = new Mat(previewSize.height, previewSize.width, CvType.CV_8UC1, data);

notice the C1 at the end.

afaik the yuv frame starts with a black and white image

edit flag offensive delete link more

Question Tools


Asked: 2013-01-17 03:51:12 -0600

Seen: 3,378 times

Last updated: Jan 17 '13