Create Sprite from Raw Data

Hi,
I am trying to fetch an image from facebook and passing the raw image data to cocos2d to create a sprite. My function to fetch the image from the URL is as follows:

@
String urlString = “http://graph.facebook.com/”+user.getId()+“/picture?type=large”;
Log.i(“facebook”, “url is” + urlString);
Bitmap pic = null;
pic = BitmapFactory
.decodeStream((InputStream) new URL(
urlString)
.getContent());
int[] pixels = new int[pic.getWidth() * pic.getHeight()];
pic.getPixels(pixels, 0, pic.getWidth(), 0, 0,pic.getWidth(),pic.getHeight());
int len = pic.getWidth()
* pic.getHeight();
// Log.i(“colordata”,“length is”+len);
nativeFbUserName(pixels,len,pic.getWidth(), pic.getHeight());@

In the cocos2d side im using the following JNI call, which in turn schedules a function to create a sprite since we cant access the opengl context in this thread (Any better way to do this?)
@
void Java_com_WBS_Test0001_Test0001_nativeFbUserName(JNIEnv env, jobject thiz,
jintArray name, jint len, jint width, jint height) {
CCLog;
jint
jArr = env->GetIntArrayElements(name,NULL);
int username[len];
for (int i=0; i<len; i++){
username[i] = (int)jArr[i];
}
HelloWorld::getShared()->picLen = (int)len;
HelloWorld::getShared()>picHeight = height;
HelloWorld::getShared
>picWidth = width;
HelloWorld::getShared~~>saveArray;
HelloWorld::getShared~~>schedule,0.1);
}
void HelloWorld::addSprite
{
CCLog;
this~~>unschedule);
CCImage *ccimg = new CCImage;
ccimg~~>initWithImageData 3, picWidth, picHeight, 32);
CCTexture2D tex = new CCTexture2D;
bool val = tex~~>initWithImage;
// bool val = tex~~>initWithData0,picWidth,picHeight, CCSizeMake);
CCLog;
CCSprite
spriteToAdd = CCSprite::createWithTexture;
spriteToAdd~~>setPosition);
this~~>addChild(spriteToAdd);
}

void HelloWorld::saveArray(int *arrayToSave)
{
arr = new int[picLen];
for(int i = 0; i < picLen; i++){
arr[i] = arrayToSave[i];
//CCLog(“d d”,i,arr[i]);
}
}

@

This creates the sprite alright, but it has a blue tinge to it. How do i get rid of this tinge?

I tried this with another image of the net also. I used this image http://www.mathewingram.com/work/wp-content/themes/thesis/rotator/335f69c5de_small.jpg (random image) and the result is attached in the screenshot

Thanks.


Screen Shot 2013-02-07 at 6.47.02 PM.png (382.8 KB)

Im having the same problem… Anyone with a solution…

I created a standalone android application and tried displaying the bitmap there and it works fine there. The “corrupted” image is displayed only in cocos2dx. Does this have something to with the order of the RGB values that the Bitmap gives us and the way in which cocos2dx reads them?

Bump! No one???

Is this the problem http://stackoverflow.com/questions/5010545/access-to-raw-data-in-argb-8888-android-bitmap?answertab=votes#tab-top?
I tried extracting the RGBA values from the int array returned like this:

int len = pic.getWidth()
* pic.getHeight();
int []finalArray = new int[len * 4];
int count = 0;
for(int i = 0; i < len; i**)
{
int alpha = Color.alpha;
int red = Color.red;
int green = Color.green;
int blue = Color.blue;
finalArray[count] = red;
count**;
finalArray[count] = green;
count**;
finalArray[count] = blue;
count**;
finalArray[count] = alpha;
count++;
}

void HelloWorld::addSprite(float time)
{
CCLog(“schedule”);
this~~>unschedule);
CCImage ccimg = new CCImage;
ccimg~~>initWithImageData 3, picWidth, picHeight, 8);
CCTexture2D *tex = new CCTexture2D;
bool val = tex~~>initWithImage;
CCLog;
CCSprite
spriteToAdd = CCSprite::createWithTexture;
spriteToAdd~~>setPosition(ccp(500, 300));
this->addChild(spriteToAdd);

}

But I cant see anything on the screen. Any reason why i cant display this image? or how to fix this??

Don’t know if you are still watching this thread, but here is the solution.
@ int[] finalArray = new int[pic.getWidth() * pic.getHeight()];

for(int i = 0; i < len; i++) {

int red = Color.red(pixels[i]);
int green = Color.green(pixels[i]);
int blue = Color.blue(pixels[i]);
finalArray[i] = Color.rgb(blue, green, red);//invert sequence here.
}@

Thats it. You can follow the question on SO to see if anyone posts a better answer.
[http://stackoverflow.com/questions/15700714/inter-change-blue-and-red-channels-in-bitmap-in-android/]

Hi The Devil,

I’m a newbie Cocos2dx. I really need this code. I read your code but i don’t know how to use implement your code.How is HelloWorld::getShared() ? Could you tell me your detail code?
Thank you very much

@ Quan
What are you exactly trying to do? Im at work now. I can post the proper solution to this once I get Home.

@Devil

I’m trying to get a Bitmap from Java and display it on screen. I followed your code and built successfully but the image not show. Please check my attached file.
Thank you very much for your reply.

@Devil

I guess my fault is calling addsprite() method when opengl lib hasn’t loaded yet. When did you download the image in java code ?

In my case, I was trying to retrieve an image from a URL and then pass the raw data from Native Android (Java) to my CPP class so I could process it further.
The steps to so are as follows:

  1. Make call from c*+ to native android code.
  2. In Native android, retrieve the image from the URL
  3. Take the raw data from the bitmap and then pass it to c*+ via JNI call.
  4. Schedule a function to display the sprite on screen.

Can I have a look at the java code that you are using for retrieving the bitmap?

Ok, please check attached file. Because I’m using a framework for downloading bitmap, so you shouldn’t understand it but I’m sure it’s worked. I have just updated my code and everything is ok but I don’t know why the image not show.