[cocos2dx机器人]使用从位图的原始数据呈现CCSprite [英] [cocos2dx android]Rendering CCSprite using raw data from Bitmap

查看:191
本文介绍了[cocos2dx机器人]使用从位图的原始数据呈现CCSprite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来从URL的图像成位图,然后使用从位图的原始数据想创建一个CCSprite。这里的问题是,当我显示精灵的形象已损坏。我创建了一个独立的Andr​​oid应用程序只(不cocos2dx),并用同样的code抓取并显示位图,它正确显示。任何原因图像不被正确地cocos2dx呈现?

I am trying to fetch an image from a URL into a Bitmap and then using the raw data from the Bitmap am trying to create a CCSprite. The issue here is that the image is corrupted when I display the sprite. I created a standalone android only application(no cocos2dx) and used the same code to fetch and display the Bitmap and its displayed correctly. Any reason why the image is not being properly rendered in cocos2dx?

我的code从URL抓取图像:

My code to fetch the image from the URL is:

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 ++函数是:

The function "nativeFbUserName" is a call to a native c++ function which is :

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);
}

编辑: 所以,我发现这个链接<一个href="http://stackoverflow.com/questions/5010545/access-to-raw-data-in-argb-8888-android-bitmap?answertab=votes#tab-top">Access在ARGB_8888 Android的位图的原始数据,指出这可能是一个错误。有没有人找到一个解决的办法?

So I found this link Access to raw data in ARGB_8888 Android Bitmap that states that it might be a bug. Has anyone found a solution to this?

推荐答案

修改 所以,我只注意到图像的腐败对image.I的右下角我不知道为什么发生这种情况以及如何解决它。任何想法?

EDIT So I just noticed a corruption of images on the lower right corner of the image.I am not sure why this is happening and how to fix it. Any ideas?

编辑结束

回答我的问题,我得从位图的字节数组使用:

Answering my own question, I obtained a byte array from the bitmap using:

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

然后通过该字节数组到本机code。

And then passed this byte array to the native code.

这篇关于[cocos2dx机器人]使用从位图的原始数据呈现CCSprite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆