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 !!!
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,
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
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).