Sir zach

专注于算法,AI, Android以及音视频领域 欢迎关注我的最新博客: zachliu.cn

导航

【原创】freetype android交叉编译

Posted on 2021-06-25 11:59  SirZach  阅读(1545)  评论(0编辑  收藏  举报

项目中Opencv需要显示中文,由于本身并不支持,所以需要借助第三方的库freetype来实现。这个库虽然android本身也有使用,但并没有暴露接口给外部使用。
freetype官网

方式1 脚本编译

编译环境

  • wsl ubuntu 20.04
  • freetype 2.10.4
  • ndk-r21e

freetype下载地址,这里使用最新版本2.10.4
image

arm64-v8a编译脚本

#!/bin/bash

API=21

NDK=/mnt/e/Android/android-ndk-r21e
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
SYSROOT=$TOOLCHAIN/sysroot

export CC=$TOOLCHAIN/bin/aarch64-linux-android${API}-clang
export CFLAGS="-g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -O2 -DNDEBUG  -fPIC  --target=aarch64-none-linux-android21 --gcc-toolchain=$TOOLCHAIN "

CPU=armv64-v8a
PREFIX=$(pwd)/android/$CPU

function build
{
./configure \
--host=aarch64-linux-android \
--prefix=$PREFIX \
--enable-shared \
--enable-static \
--with-zlib=no \
--with-bzip2=no \
--with-png=no \
--with-harfbuzz=no \
--with-sysroot=$SYSROOT \

make -j8
make install
}

build

编译过程:
image

注意别忘了加make install, 否则不会把生成的库拷贝到Prefix目录中。

编译生成结果:
image

方式2 ndk-build

编译环境

  • windows ndk-bundle
  • freetype 2.10.4

Android Mk Application Mk make makefile笔记

首先把ndk-build配置到系统环境变量,即添加android sdk下的ndk-bundle到系统PATH变量。然后参考 OpenGL ES 文字渲染方式有几种? 配置编译mk, 目前我只能成功编译静态库.a, 动态库编译失败。
参考 Google的编译脚本

编译结果会生成在当前目录的obj.local中
image

集成到项目中

【原创】Android 移植 freetype 应用实例

最后,如果需要精简字体,可以使用fontmin, 使用方法

其他参考

WaterMarkAndroid
NDK 交叉编译工具链使用
android studio 编译freeType
Android Freetype的学习过程总结
Android使用freetype