cocos2d-x -lua 關(guān)于去圖片某個(gè)點(diǎn)的像素及alpha值

// 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容