Skip to content

Instantly share code, notes, and snippets.

@jav974
Last active January 4, 2023 13:11
Show Gist options
  • Save jav974/072425f14927e6ca2c7a4439d8ac5457 to your computer and use it in GitHub Desktop.
Save jav974/072425f14927e6ca2c7a4439d8ac5457 to your computer and use it in GitHub Desktop.
This bash script compiles openalpr for android. You just need to provide the path to Android SDK and Android NDK in the first lines of this code and execute it. Tested on ubuntu 17.04 64bit
#!/bin/bash
# You should tweak this section to adapt the paths to your need
export ANDROID_HOME=/home/jeremy/Android/Sdk
export NDK_ROOT=/home/jeremy/Android/Sdk/ndk-bundle
ANDROID_PLATFORM="android-21"
# In my case, FindJNI.cmake does not find java, so i had to manually specify these
# You could try without it and remove the cmake variable specification at the bottom of this file
JAVA_HOME=/usr/lib/jvm/oracle-java8-jdk-amd64
JAVA_AWT_LIBRARY=$JAVA_HOME/jre/lib/amd64
JAVA_JVM_LIBRARY=$JAVA_HOME/jre/lib/amd64
JAVA_INCLUDE_PATH=$JAVA_HOME/include
JAVA_INCLUDE_PATH2=$JAVA_HOME/include/linux
JAVA_AWT_INCLUDE_PATH=$JAVA_HOME/include
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
####################################################################
# Prepare Tesseract and Leptonica, using rmtheis/tess-two repository
####################################################################
git clone --recursive https://github.com/rmtheis/tess-two.git tess2
cd tess2
echo "sdk.dir=$ANDROID_HOME
ndk.dir=$NDK_ROOT" > local.properties
./gradlew assemble
cd ..
####################################################################
# Download and extract OpenCV4Android
####################################################################
wget -O opencv-3.2.0-android-sdk.zip -- https://sourceforge.net/projects/opencvlibrary/files/opencv-android/3.2.0/opencv-3.2.0-android-sdk.zip/download
unzip opencv-3.2.0-android-sdk.zip
rm opencv-3.2.0-android-sdk.zip
####################################################################
# Download and configure openalpr from jav974/openalpr forked repo
####################################################################
git clone https://github.com/jav974/openalpr.git openalpr
mkdir openalpr/android-build
TESSERACT_SRC_DIR=$SCRIPTPATH/tess2/tess-two/jni/com_googlecode_tesseract_android/src
rm -rf openalpr/src/openalpr/ocr/tesseract
mkdir openalpr/src/openalpr/ocr/tesseract
shopt -s globstar
cd $TESSERACT_SRC_DIR
cp **/*.h $SCRIPTPATH/openalpr/src/openalpr/ocr/tesseract
cd $SCRIPTPATH
declare -a ANDROID_ABIS=("armeabi"
"armeabi-v7a"
"armeabi-v7a with NEON"
"arm64-v8a"
"mips"
"mips64"
"x86"
"x86_64"
)
cd openalpr/android-build
for i in "${ANDROID_ABIS[@]}"
do
if [ "$i" == "armeabi-v7a with NEON" ]; then abi="armeabi-v7a"; else abi="$i"; fi
TESSERACT_LIB_DIR=$SCRIPTPATH/tess2/tess-two/libs/$abi
if [[ "$i" == armeabi* ]];
then
arch="arm"
lib="lib"
elif [[ "$i" == arm64-v8a ]];
then
arch="arm64"
lib="lib"
elif [[ "$i" == mips ]] || [[ "$i" == x86 ]];
then
arch="$i"
lib="lib"
elif [[ "$i" == mips64 ]] || [[ "$i" == x86_64 ]];
then
arch="$i"
lib="lib64"
fi
echo "
######################################
Generating project for arch $i
######################################
"
rm -rf "$i" && mkdir "$i"
cd "$i"
cmake \
-DANDROID_TOOLCHAIN=clang \
-DCMAKE_TOOLCHAIN_FILE=$NDK_ROOT/build/cmake/android.toolchain.cmake \
-DANDROID_NDK=$NDK_ROOT \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_PLATFORM=$ANDROID_PLATFORM \
-DANDROID_ABI="$i" \
-DANDROID_STL=gnustl_static \
-DANDROID_CPP_FEATURES="rtti exceptions" \
-DTesseract_INCLUDE_BASEAPI_DIR=$TESSERACT_SRC_DIR/api \
-DTesseract_INCLUDE_CCSTRUCT_DIR=$TESSERACT_SRC_DIR/ccstruct \
-DTesseract_INCLUDE_CCMAIN_DIR=$TESSERACT_SRC_DIR/ccmain \
-DTesseract_INCLUDE_CCUTIL_DIR=$TESSERACT_SRC_DIR/ccutil \
-DTesseract_LIB=$TESSERACT_LIB_DIR/libtess.so \
-DLeptonica_LIB=$TESSERACT_LIB_DIR/liblept.so \
-DOpenCV_DIR=$SCRIPTPATH/OpenCV-android-sdk/sdk/native/jni \
-DJAVA_AWT_LIBRARY=$JAVA_AWT_LIBRARY \
-DJAVA_JVM_LIBRARY=$JAVA_JVM_LIBRARY \
-DJAVA_INCLUDE_PATH=$JAVA_INCLUDE_PATH \
-DJAVA_INCLUDE_PATH2=$JAVA_INCLUDE_PATH2 \
-DJAVA_AWT_INCLUDE_PATH=$JAVA_AWT_INCLUDE_PATH \
-DPngt_LIB=$TESSERACT_LIB_DIR/libpngt.so \
-DJpgt_LIB=$TESSERACT_LIB_DIR/libjpgt.so \
-DJnigraphics_LIB=$NDK_ROOT/platforms/$ANDROID_PLATFORM/arch-$arch/usr/$lib/libjnigraphics.so \
-DANDROID_ARM_MODE=arm \
../../src/
cmake --build . -- -j 8
cd ..
done
echo "
All done !!!"
@RobertSasak
Copy link

I started to migrate this gist into a proper repo with a Travis for building. So that we can easy reproduce building every time.

Repository https://github.com/RobertSasak/openalpr-android
Travis: https://travis-ci.com/RobertSasak/openalpr-android

Currently I have two issues:

  1. Error when building Tess-two. I believe this is issue with the latest AndroidX changes. I am quite sure that I need to downgrade something, but I am not sure what and to what version.
error: package android.support.annotation does not exist import android.support.annotation.Size;
  1. In cmake. I think this can also be resolved by downgrading NDK. What version I should use?
CMake Error at /usr/local/android-sdk/ndk-bundle/build/cmake/android.toolchain.cmake:250 (message):
  gnustl_static is no longer supported.  Please switch to either c++_shared
  or c++_static.  See
  https://developer.android.com/ndk/guides/cpp-support.html for more
  information.

We are using OpenAlpr in react-native projects https://github.com/RobertSasak/react-native-openalpr . This work very well. However Google Play requires 64bits libraries in order to publish app. So we are in desperate need to OpenAlpr 64bit libraries.

@karan-patel61
Copy link

I am having trouble generating the library files using this script but I am using:

Windows 10
Ubuntu 18.04

Which compiler do I need to use in order to generate the .so files Windows Visual Studio, Ninja, Unix Makefiles, or another compiler?

@AbhishekHirapara
Copy link

  • I mailed you the .so files
  • Use OpenCV 4.1.1 for android then you can link it in your project static
  • To check your code doesn't make sense, as everything is happening in the library and the return looks good, as it executes and takes time. I still think it can be your images or your configs are not right.
  • My build.gradle:
...
android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        ...
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.02"
        ndk {
            abiFilters "arm64-v8a","armeabi-v7a"
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
...
  • I mailed you the .so files
  • Use OpenCV 4.1.1 for android then you can link it in your project static
  • To check your code doesn't make sense, as everything is happening in the library and the return looks good, as it executes and takes time. I still think it can be your images or your configs are not right.
  • My build.gradle:
...
android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        ...
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.02"
        ndk {
            abiFilters "arm64-v8a","armeabi-v7a"
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
...

@cefaci Can you send me .so file for 64 bit? My mail is : [email protected]

@momodeveloper
Copy link

@AbhishekHirapara can you mail me the .so files please if you managed to get them ?, my mail is [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment