1

Am using libjpeg-turbo from here to compress images using JPEG compression, since

Bitmap.compress(Bitmap.CompressFormat.JPEG, quality, ByteArrayOutputStream);

causes OOM for big Images in my Android Project.

Previously i was using libjpeg-turbo from here which caused me a lot of trouble so i shifted to the above Github by sorccu (I believe this was the latest or working fine library).

Android Studio - 1.4 RC1 Android Ndk - r10e Android SDK min = 14, compiled = 22

Also with Benjamin code i was trying with old Gradle 1.3.0 which caused lot of errors, after few weeks of trying i changed to gradle-experimental:0.2.0 and everything compiled Good.

Since i feel sorccu Github easy to integrate and use, shifted to it. then used the turbojpeg-jni.c in the library along with the java code for integration in android (both available in the library).

I was able to integrate and compile using ndk-build to build the .so file and copied to lib folder and was able to use it in the java code.

module name in gradle = jpegturbo module name in android.mk = jpegturbo

Built it as a shared library

library loading code in java = System.loadLibrary("jpegturbo"); (org.libjpegturbo.turbojpeg.TJLoader.java)

There are few problems i face now, please help me to resolve it,

1.The main issue i can't compress the image using it, the compressed image is corrupted may be am doing it wrong, below is the java code

static {
    try {
        tj = new TJCompressor();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

File file = new File(pathName);
int actualBytes = (int) file.length();

byte[] srcBuf = new byte[actualBytes];
byte[] dstBuf;

FileInputStream fIn;
try {
    fIn = new FileInputStream(file);
    fIn.read(srcBuf);
    fIn.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

tj.setSourceImage(srcBuf, 0, 0, options.outWidth, 0, options.outHeight, TJ.PF_RGB);
tj.setJPEGQuality(quality);
tj.setSubsamp(TJ.SAMP_444);
dstBuf =  tj.compress(TJ.FLAG_ACCURATEDCT, options.outWidth, options.outHeight);

int size = tj.getCompressedSize();

tj.close();

FileOutputStream fos;
   try {
       fos = new FileOutputStream(file1);
       fos.write(dstBuf);
       fos.close();
   } catch (FileNotFoundException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }catch (OutOfMemoryError e) {
       e.printStackTrace();
   }

Log.e("SIZE ", String.valueOf(size));

May be the approach or logic in the above block can be wrong, help me !!!

  1. i didnt change the application.mk from the github,

APP_ABI := armeabi armeabi-v7a armeabi-v7a-hard arm64-v8a x86 x86_64

i use below command to build .so file

ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk APP_ABI=armeabi-v7a LOCAL_ARM_NEON=true ARCH_ARM_HAVE_NEON=true

it generates .so file for ABI armeabi-v7a alone, i manually copy the.so file for other ABI's. i even tried below commands but its not helping

ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk APP_ABI=all

ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk

Also the final apk works in my mobile Sony Xperia U ST25i, but not in the emulator it says the .so file cannot be found, i didn't check in other devices

Note: - i may be wrong about the library author, but both the mentioned library creators have done a great job

i also referred the following links,

  1. link 1
  2. link 2
  3. link 3

Update solved the issue 2, by changing Application.mk as APP_ABI := armeabi-v7a armeabi-v7a-hard x86 x86_64 arm64-v8a APP_PLATFORM := android-14

here's my ndk-build command ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk NDK_APPLICATION_MK=./Application.mk

After this app works ok in emulator too

0

1 Answer 1

0

Finally i made it work, with the help of this question

Thanks to the user who posted it.

The code exactly meet my requirement and am able to compress images.

Note: but problem for compressing big images (> 5-30 MB)

UPDATE:-

Problem for compressing big images (> 5-30 MB) can be solved by specifying the resizing dimensions as 1/8 of its original width and height (minimum).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Balamurugan, just a quick question. I'm having a problem with Android's default jpeg compression which create artifact, see this SO question for more detail: stackoverflow.com/questions/36487971/… I would like to ask if libjpeg-turbo is able to solve this problem?
That SO question takes a deeper dig into Bitmap.compress, i don't have that much exposure in that, Better you can give it a try to libjpeg-turbo, remember libjpeg-turbo is not exact copy of libjpeg, it has its own customization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.