我试图将图像从URL提取到位图中,然后使用位图中的原始数据来尝试创建CCSprite。这里的问题是Sprite的边界宽度和高度显示为零。
我从URL提取图像的代码是:
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);
}
最佳答案
我将使用 cocos2d::network::HttpRequest 来获取Facebook个人资料图像。您不必重写用于不同平台的图像获取代码。
void MyLayer::getFacebookPicture()
{
_url = "https://graph.facebook.com/1536842063253312/picture?type=square"; // std::string _url
cocos2d::network::HttpRequest *_httpRequest = new cocos2d::network::HttpRequest();
_httpRequest->setUrl(url.c_str());
_httpRequest->setRequestType(cocos2d::network::HttpRequest::Type::GET);
_httpRequest->setResponseCallback( CC_CALLBACK_2(MyLayer::imageDownloadedCallback, this) );
cocos2d::network::HttpClient::getInstance()->send(_httpRequest);
}
void MyLayer::imageDownloadedCallback(cocos2d::network::HttpClient *client, cocos2d::network::HttpResponse *response)
{
if(!response->isSucceed()){
return;
}
std::vector<char> *buffer = response->getResponseData();
// Use the response data to create an image.
cocos2d::Image *img = new cocos2d::Image;
if(img->initWithImageData((unsigned char*)&(buffer->front()), buffer->size()) == false){
return;
}
auto textureCache = cocos2d::Director::getInstance()->getTextureCache();
// Add into the texture cache
cocos2d::Texture2D* texture = textureCache->addImage(img, _url);
img->release();
_spriteFBProfile->setTexture(texture); // Sprite *_spriteFBProfile
}
关于android - [cocos2dx android]使用位图中的原始数据渲染Sprite,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29026384/