Log in or register to post comments

shortArray to byteArray

February 8, 2011 - 5:33am #1

Hi Kim

In the below mentioned thread
https://ar.qualcomm.com/arforums/showthread.php?t=218

You told me a way to capture frame in native code.In that code you made a short array of the frame captured.Can it be possible to get the byte array of the frame as i need that for further image processing in java code.

I tried replacing jshortArray with jbyteArray,NewShortArray with NewByteArray and NewShortArrayRegion with NewByteArrayRegion...

But its not working and giving error.:(

Does it have to do with the image format specified???

Re: shortArray to byteArray

February 13, 2011 - 10:27pm #7

Hi kim

yeah i already did the same way!!!:)

Re: shortArray to byteArray

February 9, 2011 - 7:30am #6

I don't think you will be able to use a BitmapFactory here, since you only have the raw pixel data without any headers to describe the width and height of the image. Instead, try creating an rgb565 Bitmap of the correct size, and use the copyPixelsFromBuffer method to copy the pixel data in. Here's a helpful thread:

http://stackoverflow.com/questions/3956770/how-to-load-rgb565-buffer-to-imageview

You should be able to use something like this to pass a byte array from native:

const short* pixels = (const short*) image->getPixels();

int width = image->getWidth();
int height = image->getHeight();
int numPixels = width * height;

jbyteArray pixelArray = env->NewByteArray(numPixels * 2);
env->SetByteArrayRegion(pixelArray, 0, numPixels * 2, (const jbyte*) pixels);

jclass javaClass = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(javaClass, "setRGB565CameraImage", "([BII)V");
env->CallObjectMethod(obj, method, pixelArray, width, height);

env->DeleteLocalRef(pixelArray);

Just make sure that your Java method takes a byte array instead of a short array.

- Kim

Re: shortArray to byteArray

February 8, 2011 - 9:08pm #5

Hi i need urgent helpppp........Iam able to get the byte array of the frameee......but how to create the bitmap of it in java code not able to get.......

Using that byte array if i try to create the bitmap using function BitmapFactory.decodeByteArray...my bitmap is null dnt know why....:(:(:(

This is my native code for byte array creation..

if (image1->getFormat() == QCAR::RGB565)
{
// for rbg565, the pixels are stored as an array of shorts
const char* pixels = (const char*) image1->getPixels();

int width = image1->getWidth();
int height = image1->getHeight();
int numPixels = width * height;

jbyteArray pixelArray = env->NewByteArray(numPixels);
env->SetByteArrayRegion(pixelArray, 0, numPixels,(jbyte*)pixels);

Re: shortArray to byteArray

February 8, 2011 - 6:26am #4

Hi Kim

As just now I saw Bitmap supports RGB565 picture format......

But then how to create bitmap from this short array...

Also i didnt get where to change the size of the array???

const byte* pixels = (const byte*) image1->getPixels();

int width = image1->getWidth();
int height = image1->getHeight();
int numPixels = width * height;

jbyteArray pixelArray = env->NewByteArray(numPixels);
env->SetByteArrayRegion(pixelArray, 0, numPixels, pixels);

This is my code.....

Re: shortArray to byteArray

February 8, 2011 - 6:11am #3

Actually I need to create the bitmap of the frame passed to java code.

BitmapFactory.decode*** has method to except byte array only and not short array...

So this is the proble Iam facing..............

Re: shortArray to byteArray

February 8, 2011 - 5:56am #2

What error is it giving? You can probably create a byte array using the data, just keep in mind that a short equals two bytes here, so you need to double the length of the array.

It's important to understand why the pixel data is stored as a short array though. Each pixel is represented as an RGB565 value, so it takes a full short (16 bits) to describe one pixel of color. What pixel format is your Java code expecting?

- Kim

Log in or register to post comments