我正在尝试将URL中的图像提取到Bitmap中,然后使用Bitmap中的原始数据尝试创建CCSprite。这里的问题是Sprite显示边界宽度和高度为零。
我从网址获取图片的代码是:
String urlString ="https://graph.facebook.com/1536842063253312/picture?type=square"; //"http://www.mathewingram.com/work/wp-content/themes/thesis/rotator/335f69c5de_small.jpg";//http://graph.facebook.com/"+user.getId()+"/picture?type=large";
Bitmap pic = null;
try {
pic = BitmapFactory.decodeStream((InputStream) new URL(urlString).getContent());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("start fetchImageOfFacebook"+e);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("start fetchImageOfFacebook"+e);
e.printStackTrace();
}
int[] pixels = new int[pic.getWidth() * pic.getHeight()];
pic.getPixels(pixels, 0, pic.getWidth(), 0, 0, pic.getWidth(), pic.getHeight());
int[] finalArray = new int[pic.getWidth() * pic.getHeight()];
int len = 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.
}
onFacebookImageNative(pixels,len,pic.getWidth(), pic.getHeight());
Main cpp...
JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_onFacebookImageNative(JNIEnv* env, jobject,
jintArray pixels, jint dataLen, jint width, jint height, jint bitsPerComponent)
{
jint *jArr = env->GetIntArrayElements(pixels, NULL);
int pixelsInt[dataLen];
for (int i=0; i<dataLen; i++){
pixelsInt[i] = (int)jArr[i];
}
GameManager::sharedGameManager()->onFacebookImage(pixelsInt, (int) dataLen, (int) width, (int) height);
}
GameManager.cpp Class
void GameManager:: onFacebookImage(int pixelsInt[], int picLen, int picWidth, int picHeight)
{
CCTexture2D *tex = new CCTexture2D();
bool val = tex->initWithData(pixelsInt,picLen,(Texture2D::PixelFormat)0,picWidth,picHeight, Size(picWidth,picHeight));
CCSprite *spriteToAdd = CCSprite::createWithTexture(tex);
CCLog("spriteToAdd %f %f",spriteToAdd->getBoundingBox().size.width,spriteToAdd->getBoundingBox().size.height);
}