cocos获取图片像素
轉(zhuǎn):https://blog.zengrong.net/post/2104.html
?
我采用的是這種辦法。流程如下:
這種方法的弊端如下:
當(dāng)然,如果確實(shí)需要在同一張圖片上多次操作,緩存可以程序員自己來(lái)做。
為了實(shí)現(xiàn)這個(gè)流程,我修改了 CCImage.h,增加了兩個(gè)方法?getColor4B?和?getColor4F?:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ccColor4B getColor4B(float x, float y) {ccColor4B color = { 0, 0, 0, 0 };int ix = (int)x - 1;int iy = (int)y - 1;m_pData += (iy*getWidth() + ix) * 4;color.r = *(m_pData++);color.g = *(m_pData++);color.b = *(m_pData++);color.a = *(m_pData++);return color; };ccColor4F getColor4F(float x, float y) {return ccc4FFromccc4B(getColor4B(x, y)); }; |
2014-10-24更新:上面的代碼沒(méi)有考慮越界問(wèn)題,在傳遞的坐標(biāo)不在圖像中時(shí),程序會(huì)崩潰。
最新的代碼改正了問(wèn)題,請(qǐng)參考?github?。
由于 CCImage 是跨平臺(tái)實(shí)現(xiàn)的,所以放在頭文件中比放在實(shí)現(xiàn)文件中要方便許多。否則,就需要在 CCImage 的若干個(gè)平臺(tái)相關(guān)實(shí)現(xiàn)中分別執(zhí)行實(shí)現(xiàn)了。
下面是 quick-cocos2d-x 中的實(shí)現(xiàn)代碼,我將其放在了 CCSpriteExtned.lua 框架中,這樣能讓所有的 CCSprite 實(shí)例都支持這個(gè)方法。
具體的實(shí)現(xiàn)請(qǐng)看代碼,不解釋了。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | -- NOTE!!! The method is very slowly! Please use it in carefully. -- @param __point A coordinate for color. -- @param __convertToNodeSpace Optional, default is true, convert a coordinate to node space from world space. -- @param __isFloat Optional, default is false, convert a coordinate to node space from world space. function CCSpriteExtend:getColor(__point, __convertToNodeSpace, __isFloat)if __convertToNodeSpace == nil then__convertToNodeSpace = trueendif __convertToNodeSpace then__point = self:convertToNodeSpace(__point)end-- Create a new Texture to get the pixel datas.local __size = self:getContentSize()local __rt = CCRenderTexture:create(__size.width, __size.height)-- Hold the old anchor and position to restore it late on.local __oldAnchor = self:getAnchorPoint()local __oldPos = self:getPositionInCCPoint()-- Move the sprite to left bottom.self:align(display.LEFT_BOTTOM, 0,0)--print("getColor:", __point.x, __point.y, __size.width, __size.height)-- Render the sprite to get a new texture.__rt:begin();self:visit()__rt:endToLua();-- Restore the original anchor and position.self:setAnchorPoint(__oldAnchor)self:setPosition(__oldPos)local __img = __rt:newCCImage(false)local __color = nilif __isFloat then__color = __img:getColor4F(__point.x, __point.y)else__color = __img:getColor4B(__point.x, __point.y)endreturn __color, __rt end-- Only get a alpha value. function CCSpriteExtend:getColorAlpha(__point, __convertToNodeSpace, __isFloat)local color = self:getColor(__point, __convertToNodeSpace, __isFloat)return color.a en |
這個(gè)方法已經(jīng)合并進(jìn)入?quick-cocos2d-x 的 develop 分支。
2014-10-24更新:由于 newCCImage 方法在 C++ 中是請(qǐng)求堆內(nèi)存并返回一個(gè)指針。因此必須手動(dòng)釋放。上面的代碼沒(méi)有考慮釋放問(wèn)題,將會(huì)導(dǎo)致內(nèi)存泄露。
最新的代碼改正了問(wèn)題,請(qǐng)參考?github?。
總結(jié)
以上是生活随笔為你收集整理的cocos获取图片像素的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 百度推出清风算法,SEO的标题该如何优化
- 下一篇: Task 05:样式色彩秀芳华