Git Product home page Git Product logo

android_freetype's Introduction

项目说明

在android中使用CMakeLists.txt编译生成静态库或So动态库

[官方教程地址][https://www.freetype.org/freetype2/docs/tutorial/step1.html]

工程中已经写好了Android.mk与CMakeLists.txt

你只用将该工程文件全部拷到你的工程的main下的cpp目录中,然后在gradle中配制下:

在default中添加:

    externalNativeBuild {
           cmake {
               cppFlags ""
               abiFilters "armeabi-v7a"//"arm64-v8a"
           }
       }

在default外添加:

      externalNativeBuild {
         cmake {
             path "src/main/cpp/CMakeLists.txt"
         }
     }
     

代码调用例子:

    #include "ft2build.h"
    #include FT_FREETYPE_H
    #include <freetype/ftglyph.h>
    #include FT_TRUETYPE_IDS_H
      char16_t* osd = "A";
    int len = 1;
      for ( n = 0; n < len; n++ )
    {
        LOGE("load char 0x%02x",(osd[n]));
        FT_Library  library = NULL;   /* handle to library     */
        FT_Face     fface =NULL ;      /* handle to face object */

        #加载free库
        error = FT_Init_FreeType(&library);

        if(error){
            LOGE("init freetype error");
            continue;
        }
    //    error = FT_New_Face(library,"/system/fonts/DroidSans.ttf",0,&fface);
      //如果小于127则认为是ascii单字节,我们可以加载没有中文的字体,否则加载中文字体
        if(osd[n]>127){
            error = FT_New_Face(library,"/system/fonts/DroidSansChinese.ttf",0,&fface);
        }
        else{
            error = FT_New_Face(library,"/system/fonts/DroidSans.ttf",0,&fface);
        }



        if(error){
            LOGE("init face error");
            continue;
        }
    //选择字符集,我们现在选择unicode,表示将来去查找字形图片都是根据unicode编码去查找
        error = FT_Select_Charmap(fface,FT_ENCODING_UNICODE);

        if(error){
            LOGE("init select charmap error");
            continue;
        }
  //设置生成的字符图像大小
        FT_Set_Pixel_Sizes(fface,30,30);

        FT_GlyphSlot slop = fface->glyph;
//加载,并绘制到内存中
        error= FT_Load_Char(fface, osd[n],  FT_LOAD_RENDER);

        if ( error )
        {
            LOGE(" FT_Load_Char  error");
            continue;  /* ignore errors */
        }

       FT_Bitmap* bitmap = &slop->bitmap;

        LOGE("left:%d,top:%d, lsb_delta:%d ,rsb:%d,",(int)slop->bitmap_left,(int)slop->bitmap_top,slop->lsb_delta,slop->rsb_delta);

        int w  = bitmap->width;
        int h  = bitmap->rows;
//
//        switch (bitmap->pixel_mode)
//        {
//            case FT_PIXEL_MODE_BGRA:
//            {
//                LOGE("FT_PIXEL_MODE_BGRA");
//                break;
//            }
//            case FT_PIXEL_MODE_GRAY:
//            {
//                LOGE("FT_PIXEL_MODE_GRAY");
//                break;
//            }
//            case FT_PIXEL_MODE_MONO:
//            {
//                LOGE("FT_PIXEL_MODE_MONO");
//                break;
//            }
//            case FT_PIXEL_MODE_NONE:
//            {
//                LOGE("FT_PIXEL_MODE_NONE");
//                break;
//            }
//            default:
//            {
//                LOGE("other mode:%d",bitmap->pixel_mode);
//            }
//        }


        uint8 * osdYuv = static_cast<uint8 *>(malloc(w * h * 3 / 2));
// 将rgb数据拷到yuv内存中,只用的了Y分量
        memcpy(osdYuv,bitmap->buffer,w*h);


       //  OsdInfo* info = new OsdInfo((int)slop->bitmap_left,(int)slop->bitmap_top, w, h, osdYuv);



        this->osdList.push_back(info);


        if(fface){
            FT_Done_Face(fface);
        }
        fface = NULL;

        if(library){
            FT_Done_FreeType(library);
        }
        library = NULL;
    }
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.