2013-02-08 27 views
2

我試圖從URL中將圖像提取到位圖中,然後使用位圖中的原始數據嘗試創建CCSprite。這裏的問題是,當我顯示精靈時,圖像已損壞。我創建了一個獨立的僅適用於android的應用程序(不包含cocos2dx),並使用相同的代碼來獲取並顯示位圖及其正確顯示。任何爲什麼圖像不能在cocos2dx中正確呈現的原因?[cocos2dx android]使用位圖中的原始數據呈現CCSprite

我的代碼從URL抓取圖像:

String urlString = "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; 
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(); 
nativeFbUserName(pixels,len,pic.getWidth(), pic.getHeight()); 

功能 「nativeFbUserName」 是本機C呼叫++函數是:

void Java_com_WBS_Test0001_Test0001_nativeFbUserName(JNIEnv *env, jobject thiz,jintArray name, jint len, jint width, jint height) { 
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 = (int)height; 
HelloWorld::getShared()->picWidth = (int)width; 
HelloWorld::getShared()->saveArray(username); 
HelloWorld::getShared()->schedule(SEL_SCHEDULE(&HelloWorld::addSprite),0.1); 
} 
void HelloWorld::saveArray(int *arrayToSave) 
{ 
arr = new int[picLen]; 
for(int i = 0; i < picLen; i++){ 
    arr[i] = arrayToSave[i]; 
} 
} 
void HelloWorld::addSprite(float time) 
{ 
this->unschedule(SEL_SCHEDULE(&HelloWorld::addSprite)); 
CCTexture2D *tex = new CCTexture2D(); 
bool val = tex->initWithData(arr,(cocos2d::CCTexture2DPixelFormat)0,picWidth,picHeight, CCSizeMake(picWidth,picHeight)); 
CCLog("flag is %d",val); 
CCSprite *spriteToAdd = CCSprite::createWithTexture(tex); 
spriteToAdd->setPosition(ccp(500, 300)); 
this->addChild(spriteToAdd); 
} 

編輯: 所以我發現此鏈接Access to raw data in ARGB_8888 Android Bitmap,指出它可能是一個錯誤。有沒有人找到解決方案?

+0

凹凸!!!沒有人? – TheDevilsFire

回答

0

編輯 所以我只注意到圖像的腐敗對image.I的右下角我不知道爲什麼發生這種情況,以及如何解決它。有任何想法嗎?

編輯結束

回答我的問題,我用得從位圖的字節數組:

byte[] data = null; 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
pic.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
data = baos.toByteArray(); 

,然後傳遞這個字節數組的本地代碼。

+0

我使用相同的代碼來做到這一點,但是紅色和藍色通道是相反的。任何想法爲什麼這可能會發生? – asloob

+0

你使用的是字節數組還是getPixels()方法?我不知道getPixels()方法的問題,但是如果你使用字節數組,那麼數據就沒事了,圖像按預期顯示。 – TheDevilsFire

+0

我剛剛能夠做到這一點。謝謝您的幫助。問題是在這裏http://stackoverflow.com/questions/15700714/inter-change-blue-and-red-channels-in-bitmap-in-android – asloob

相關問題