Create Sprite from Raw Data

Create Sprite from Raw Data
0.0 0


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 = “”+user.getId()+“/picture?type=large”;
Log.i(“facebook”, “url is” + urlString);
Bitmap pic = null;
pic = BitmapFactory
.decodeStream((InputStream) new URL(
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) {
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;
>picWidth = width;
void HelloWorld::addSprite
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);
spriteToAdd = CCSprite::createWithTexture;

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 (random image) and the result is attached in the screenshot


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
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 =;
int green =;
int blue =;
finalArray[count] = red;
finalArray[count] = green;
finalArray[count] = blue;
finalArray[count] = alpha;

void HelloWorld::addSprite(float time)
CCImage ccimg = new CCImage;
ccimg~~>initWithImageData 3, picWidth, picHeight, 8);
CCTexture2D *tex = new CCTexture2D;
bool val = tex~~>initWithImage;
spriteToAdd = CCSprite::createWithTexture;
spriteToAdd~~>setPosition(ccp(500, 300));


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 =[i]);
int green =[i]);
int blue =[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.


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.



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.



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.