Search code examples
javaandroidpdftesseracttess-two

Tesseract pdf renderer with 24 bit depth jpg image


I've to create a searchable pdf from multiple 24 bit depth jpg images. I'm using tess-two which by default comes with libpng. The problem is that tesseracts output a corrupt pdf! The images are not present in the pdf. The text is still present in the pdf.

I've no problems when using png files however the input is a jpg image. Converting jpg's to png with the following code is very time consuming:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap bitmap = BitmapFactory.decodeFile("myimage.jpg", options);

File file = new File("myoutputimage.png");
FileOutputStream fOut;
try
{
    fOut = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
    fOut.flush();
    fOut.close();
}
catch (Exception e)
{
    e.printStackTrace();
}

On my machine it takes 2 seconds to create a png file.

I already compiled tess-two with libjpeg but this wasn't working either. Is it possible to create a searchable pdf with tesseract with jpg input files?


Solution

  • Finally got it working. Tess-two isn't shipping with libjpeg. If you want to pass jpg files for the pdfrenderer then you've to compile leptonica with libjpeg. I downloaded libjpeg and placed in the jni directory libjpeg.

    Create an empty jconfig.h in the libjpeg directory. Create a Android.mk file with the following contents:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := libjpegt
    
    LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
    
    LOCAL_SRC_FILES := jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jquant1.c jquant2.c jutils.c jmemmgr.c jmemname.c
    
    include $(BUILD_SHARED_LIBRARY)
    

    The final leptonica's Android.mk should look like:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := liblept
    
    # leptonica (minus freetype)
    
    BLACKLIST_SRC_FILES := \
      %endiantest.c \
      %freetype.c \
      %xtractprotos.c
    
    LEPTONICA_SRC_FILES := \
      $(subst $(LOCAL_PATH)/,,$(wildcard $(LEPTONICA_PATH)/src/*.c))
    
    LOCAL_SRC_FILES := \
      $(filter-out $(BLACKLIST_SRC_FILES),$(LEPTONICA_SRC_FILES))
    
    LOCAL_CFLAGS := \
      -DHAVE_CONFIG_H \
      -DHAVE_LIBJPEG
    
    LOCAL_LDLIBS := \
      -lz
    
    # jni
    
    LOCAL_SRC_FILES += \
      box.cpp \
      boxa.cpp \
      pix.cpp \
      pixa.cpp \
      utilities.cpp \
      readfile.cpp \
      writefile.cpp \
      jni.cpp
    
    LOCAL_C_INCLUDES += \
      $(LOCAL_PATH) \
      $(LEPTONICA_PATH)/src \
      $(LIBPNG_PATH) \
      $(LIBJPEG_PATH)
    
    LOCAL_LDLIBS += \
      -ljnigraphics \
      -llog
    
    # common
    LOCAL_SHARED_LIBRARIES := libpngt libjpegt
    LOCAL_PRELINK_MODULE := false
    
    include $(BUILD_SHARED_LIBRARY)
    

    In the java files you should load libjpegt:

    System.loadLibrary("jpegt");
    

    Furthermore you need to add this in Android.mk file in the jni folder:

    LIBJPEG_PATH := $(LOCAL_PATH)/libjpeg
    

    Compiled libjpeg wrong at first but now it's working