Skia最新“编译”,绘制中文字符串,加载PNG、BMP图片等资料的整理。
生活随笔
收集整理的這篇文章主要介紹了
Skia最新“编译”,绘制中文字符串,加载PNG、BMP图片等资料的整理。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
“編譯”Skia
國內不適合自己編譯。Github 有不止一個自動構建的SKIA二進制build,涵蓋多個操作系統(tǒng),可直接取而用之。
推薦大名鼎鼎的JetBrains維護的倉庫,地址是 https://github.com/JetBrains/skia-build。含靜態(tài)庫與頭文件。release靜態(tài)庫解壓出來180MB,debug解壓出來一兩個G。推薦直接用release版本的。
繪制中文字符串
直接用Skia的SkString就可以,無需多次轉換。如果顯示方框,那說明字體不對,要設置宋體。
auto pFace = SkTypeface::MakeFromName("宋體", SkFontStyle::Normal()); SkFont font; font.setTypeface(pFace);SkString string("加載圖片 #");canvas->drawString(string, 1, 28, font, textpaint);完整代碼見文末的demo
加載并顯示PNG圖片等
百度“Skia繪制圖片”出來的結果大多已經過時。
最新方法,要用 SkCodec::MakeFromData 解析二進制流,得到SkCodec 與 SkImageInfo,中SkImageInfo包括圖片尺寸信息,然后再解碼為 SkBitmap。
DWORD fileLength;char* memFile;... 加載文件到 memFile數(shù)組中sk_sp<SkData> data = SkData::MakeWithoutCopy(memFile, fileLength);auto codec = SkCodec::MakeFromData(data);if (!codec) {LogIs(2, "FAILED DECODING FILE!");}SkImageInfo codecInfo = codec->getInfo();auto alphaType = codecInfo.isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;auto decodeInfo = SkImageInfo::Make(codecInfo.width(), codecInfo.height(), kN32_SkColorType, alphaType);SkBitmap* skBitmap = new SkBitmap();char* pixels = new char[codecInfo.width()*codecInfo.height()*32];skBitmap->setInfo(decodeInfo, codecInfo.width()*32);skBitmap->setPixels(pixels);auto decodeResult = codec->getPixels(decodeInfo, pixels, codecInfo.width()*32);decodeResult 等于零代表成功,其他可能的數(shù)值:
/*** Error codes for various SkCodec methods.*/enum Result {/*** General return value for success.*/kSuccess, 0/*** The input is incomplete. A partial image was generated.*/kIncompleteInput, 1/*** Like kIncompleteInput, except the input had an error.** If returned from an incremental decode, decoding cannot continue,* even with more data.*/kErrorInInput, 2/*** The generator cannot convert to match the request, ignoring* dimensions.*/kInvalidConversion, 3/*** The generator cannot scale to requested size.*/kInvalidScale, 4/*** Parameters (besides info) are invalid. e.g. NULL pixels, rowBytes* too small, etc.*/kInvalidParameters, 5/*** The input did not contain a valid image.*/kInvalidInput, 6/*** Fulfilling this request requires rewinding the input, which is not* supported for this input.*/kCouldNotRewind, 7/*** An internal error, such as OOM.*/kInternalError, 8/*** This method is not implemented by this codec.* FIXME: Perhaps this should be kUnsupported?*/kUnimplemented, 9};完整 Demo 代碼 | 我的Skia測試盒子
運行:
參考資料:《調用Skia內置位圖編解碼器的坑點》
總結
以上是生活随笔為你收集整理的Skia最新“编译”,绘制中文字符串,加载PNG、BMP图片等资料的整理。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用多进程教你下载 m3u8 加密或非加
- 下一篇: Qoo10趣天jp入驻条件VS亚马逊Am