cocos获取图片像素
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                cocos获取图片像素
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            //一下是獲取某個像素的透明值,以此類推可以回去r,g,b的值
Image *myImage = new Image();
myImage->initWithImageFile("test.png");
unsigned char *data = myImage->getData();  //這里就是圖片數據了
//根據剛剛計算的相對坐標值,計算出觸摸點代表了哪一個像素點      然后再提取出該像素點的alpha值
//注意:因為圖片坐標(0,0)是在左上角,所以要和觸摸點的Y轉換一下,也就是“(myImage->getHeight() - (int)(ps.y) - 1)”
//該data值是把二維數組展開成一個一維數組,因為每個像素值由RGBA組成,所以每隔4個char為一個RGBA,并且像素以橫向排列
int pa = 4 * ((myImage->getHeight() - (int)(ps.y) - 1) * myImage->getWidth() + (int)(ps.x)) + 3;
unsigned int ap = data[pa]; 
在需要圖片像素值的時候,將這張圖片使用 FrameBuffer 重新繪制成像素; 獲得相關像素的顏色值; 刪除已經獲得的像素。 
如果要取得的像素圖片巨大,可能對性能有影響; 每次的數據沒有緩存,頻繁執行的話性能消耗巨大。 
                        
                        
                        轉:https://blog.zengrong.net/post/2104.html
?
我采用的是這種辦法。流程如下:
這種方法的弊端如下:
當然,如果確實需要在同一張圖片上多次操作,緩存可以程序員自己來做。
為了實現這個流程,我修改了 CCImage.h,增加了兩個方法?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更新:上面的代碼沒有考慮越界問題,在傳遞的坐標不在圖像中時,程序會崩潰。
最新的代碼改正了問題,請參考?github?。
由于 CCImage 是跨平臺實現的,所以放在頭文件中比放在實現文件中要方便許多。否則,就需要在 CCImage 的若干個平臺相關實現中分別執行實現了。
下面是 quick-cocos2d-x 中的實現代碼,我將其放在了 CCSpriteExtned.lua 框架中,這樣能讓所有的 CCSprite 實例都支持這個方法。
具體的實現請看代碼,不解釋了。
| 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 | 
這個方法已經合并進入?quick-cocos2d-x 的 develop 分支。
2014-10-24更新:由于 newCCImage 方法在 C++ 中是請求堆內存并返回一個指針。因此必須手動釋放。上面的代碼沒有考慮釋放問題,將會導致內存泄露。
最新的代碼改正了問題,請參考?github?。
總結
以上是生活随笔為你收集整理的cocos获取图片像素的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 百度推出清风算法,SEO的标题该如何优化
- 下一篇: Task 05:样式色彩秀芳华
