Ask Your Question
0

camera preview gives gray images

asked Jan 17 '13

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.

Preview: (hide)

4 answers

Sort by » oldest newest most voted
1

answered Jan 17 '13

rics gravatar image

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

Preview: (hide)

Comments

thank you alot your link helped me :)

MoHaKa gravatar imageMoHaKa (Jan 17 '13)edit
2

answered Jan 17 '13

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.

Preview: (hide)
1

answered Jan 17 '13

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);
Preview: (hide)

Comments

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

MoHaKa gravatar imageMoHaKa (Jan 17 '13)edit

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

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

MoHaKa gravatar imageMoHaKa (Jan 17 '13)edit
0

answered Jan 17 '13

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

Preview: (hide)

Question Tools

Stats

Asked: Jan 17 '13

Seen: 3,512 times

Last updated: Jan 17 '13