// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto m_imgMan = CCSprite::create("red.png");
m_imgMan->setPosition(ccp(100, 100));
m_imgMan->setAnchorPoint(Vec2(0.5f, 0.5f));
addChild(m_imgMan, 1);
//這里參數(shù)很固定 沒有什么可改的余地
//參數(shù)1 你需要渲染出來(lái)區(qū)域的寬
//參數(shù)2 你需要渲染出來(lái)區(qū)域的高
//參數(shù)3 必須是這個(gè)模式 否則后面復(fù)制內(nèi)存好像會(huì)有問題Texture2D::PixelFormat::RGBA8888
auto m_pRenderTexture = RenderTexture::create(m_imgMan->getContentSize().width, m_imgMan->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
m_pRenderTexture->setPosition(ccp(100, 100));
//m_pRenderTexture->setAnchorPoint(Vec2(0, 0));//設(shè)置錨點(diǎn)無(wú)效 錨點(diǎn)默認(rèn)0.5f, 0.5f
addChild(m_pRenderTexture, 2);
Color4B color4B = { 0, 0, 0, 0 };
//開始準(zhǔn)備繪制
m_pRenderTexture->begin();
//繪制使用的臨時(shí)精靈,與原圖是同一圖片
CCSprite* pTempSpr = CCSprite::createWithSpriteFrame(m_imgMan->displayFrame());
pTempSpr->setPosition(ccp(pTempSpr->getContentSize().width / 2, pTempSpr->getContentSize().height / 2));
//繪制
pTempSpr->visit();
m_pRenderTexture->setAnchorPoint(Vec2(0, 0));
//結(jié)束繪制
m_pRenderTexture->end();
//解決方案原文 找了一下午原因 竟是這樣 http://blog.csdn.net/super_level/article/details/41707687
Director::getInstance()->getRenderer()->render();//在3.0此處必須寫上這個(gè),否則newImage整張圖片都為黑色,或者在下一幀獲取
//通過(guò)畫布拿到這張畫布上每個(gè)像素點(diǎn)的信息,封裝到CCImage中
Image* pImage = m_pRenderTexture->newImage();
//獲取像素?cái)?shù)據(jù)
unsigned char* data_ = pImage->getData();
unsigned int *pixel = (unsigned int *)data_;
//pixel = pixel + (y * (int)pTempSpr->getContentSize().width) * 1 + x * 1;//改變這里去調(diào)節(jié)其他像素
//R通道
color4B.r = *pixel & 0xff;
//G通道
color4B.g = (*pixel >> 8) & 0xff;
//B通道
color4B.b = (*pixel >> 16) & 0xff;
//Alpha通道,我們有用的就是Alpha
color4B.a = (*pixel >> 24) & 0xff;
CCLOG("cur first color: alpha = %d", color4B.a);//
if (color4B.a > 50)
{
CCLOG("cur first color: alpha > 50");
}
return true;
}

image.png

image.png

red.png