Coder Social home page Coder Social logo

androiddeveloperlb / androidjnibitmapoperations Goto Github PK

View Code? Open in Web Editor NEW
541.0 541.0 154.0 3.43 MB

Allows to perform various simple operations on bitmaps via JNI , while also providing some protection against OOM using the native Java environment on Android

License: Apache License 2.0

Makefile 1.24% C++ 60.94% Java 37.82%

androidjnibitmapoperations's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

androidjnibitmapoperations's Issues

Save a Bitmap as a Byte array or image File

Your algorithm helps me a lot saving memory in my app, Thank you; I used your algorithm like this:

public Bitmap getRotatedBitmap(Bitmap bitmap)
{
    JniBitmapHolder bitmapHolder = new JniBitmapHolder();
    bitmapHolder.storeBitmap(bitmap);
    bitmapHolder.rotateBitmapCw90();
    bitmap.recycle();
    return bitmapHolder.getBitmapAndFree();
}

Now, the next step is save this bitmap without lose quality. I'll try this:

public void saveBitmap1(Bitmap bitmap)
{
    bitmap = getRotatedBitmap(bitmap);
    String name = "desiredFilename.png";
    FileOutputStream fos = openFileOutput(name, Context.MODE_PRIVATE);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
}

The algorithm works fine, but this is too slow when the bitmap is compressing in PNG or WEBP. JPEG with 100 quality it's faster, anyway, the bitmap will lost quality. I'll try this too

public void saveBitmap2(Bitmap bitmap)
{
    bitmap = getRotatedBitmap(bitmap);
    int bytes = bitmap.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(bytes);
    bitmap.copyPixelsToBuffer(buffer);
    byte[] array = buffer.array();
    String name = "desiredFilename.png";
    FileOutputStream fos = openFileOutput(name, Context.MODE_PRIVATE);
    fos.write(array);
    fos.flush();
    fos.close();
}

This solution have problems with big bitmaps, i.e. 5312 x 2988. When the buffer is allocating the bytes throws an OutOfMemoryException. And I can't resize the picture; I must rotate the picture without resize or lost quality.

So, Is there a form to get a byte array from your native library? Or Could you help me how I can save the bitmap as a File, avoiding memory exceptions?

I'll be very grateful if you can solve my problem

Repository problems

Gradle build on https://jitpack.io is not availiable.
I've talked with jitpack support & they told me:
"we don’t have NDK installed on the build servers"
also the issue was opened jitpack/jitpack.io#24

BTW can you pls use another build system before they solve the problem?

java.lang.UnsatisfiedLinkError:

Couldn't load JniBitmapOperationsLibrary from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.myapp-2.apk,libraryPath=/data/app-lib/com.example.myapp-2]: findLibrary returned null at java.lang.Runtime.loadLibrary(Runtime.java:355) at java.lang.System.loadLibrary(System.java:525)

I have been running into this issue. i have followed all your instructions. including the ones you linked to. but non of them fixed the issue for me.
I've spent a ball dropping two days trying to fix this. :(

if you can, can you generate a working jar file for me?
(I will provide you with my package name. if you need it)

Performance

Hi

Thanks for a great lib. It drastically reduced my memory footprint from around 70mb to ~15mb. That being said I still can't see it as a viable option as it has performance implications.

I get around 2-3ms loading time on my Nexus 4 when retrieving a bitmap from an ArrayList, while loading a bitmap from an ArrayList of JniBitmapHolders takes ~70ms.

Is there any way around this?

Application crashes with linker error

The app crashes every time on Lenovo P780 with Android 4.2.1:

  java.lang.UnsatisfiedLinkError: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libgnustl_shared.so" needed by "libJniBitmapOperationsLibrary.so"; caused by load_library(linker.cpp:745): library "libgnustl_shared.so" not found
        at java.lang.Runtime.loadLibrary(Runtime.java:365)
        at java.lang.System.loadLibrary(System.java:514)
        at com.jni.bitmap_operations.JniBitmapHolder.<clinit>(JniBitmapHolder.java:15)

On genymotion with Android 4.2.2 it crashes with this error:

     Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: load_library(linker.cpp:750): library "/system/lib/libhoudini.so" not found
        at java.lang.Runtime.loadLibrary(Runtime.java:371)
        at java.lang.System.loadLibrary(System.java:535)
        at com.jni.bitmap_operations.JniBitmapHolder.<clinit>(JniBitmapHolder.java:15)

Issue with free method?

I'm no expert on JNI (or C++), but I had a concern about the jniFreeBitmapData function.

It seems that it doesn't free the jniBitmap object if _storedBitmapPixels is NULL. I rewrote as follows:

JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFreeBitmapData(
JNIEnv * env, jobject obj, jobject handle)
{
JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
if (jniBitmap == NULL)
    return;
if (jniBitmap->_storedBitmapPixels != NULL) {
    delete[] jniBitmap->_storedBitmapPixels;
    jniBitmap->_storedBitmapPixels = NULL;
}
delete jniBitmap;
}

(Also wondering if there is any reason why you include jni.h twice?)

Cannot get the sample to compile / run

Hi, I am having issues compiling the sample. In particular, I am getting the following

Error:Execution failed for task ':JniBitmapOperationsLibrary:compileReleaseNdk'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android\android-ndk-r10e\ndk-build.cmd'' finished with non-zero exit value 2

Any ideas

Thanks

could not load library "libstlport_shared.so" needed by "libJniBitmapOperationsLib.so"; caused by load_library(linker.cpp:769): too small to be an ELF executable: libstlport_shared.so

load_library(linker.cpp:769): too small to be an ELF executable: libstlport_shared.so
E/linker: soinfo_link_image(linker.cpp:1647): could not load library "libstlport_shared.so" needed by "libJniBitmapOperationsLib.so"; caused by load_library(linker.cpp:769): too small to be an ELF executable: libstlport_shared.so
E/dalvikvm: dlopen("/data/app-lib/com.example.jnibitmapoperationstest-2/libJniBitmapOperationsLib.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1647): could not load library "libstlport_shared.so" needed by "libJniBitmapOperationsLib.so"; caused by load_library(linker.cpp:769): too small to be an ELF executable: libstlport_shared.so
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.UnsatisfiedLinkError: Cannot load library: soinfo_link_image(linker.cpp:1647): could not load library "libstlport_shared.so" needed by "libJniBitmapOperationsLib.so"; caused by load_library(linker.cpp:769): too small to be an ELF executable: libstlport_shared.so
at java.lang.Runtime.loadLibrary(Runtime.java:372)
at java.lang.System.loadLibrary(System.java:514)
at com.jni.bitmap_operations.JniBitmapHolder.(JniBitmapHolder.java:13)
at com.example.jnibitmapoperationstest.MainActivity.(MainActivity.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
12-07 10:59:26.527 529-547/? E/AppErrorDialog: Failed to get ILowStorageHandle instance
12-07 10:59:27.005 529-13755/? E/AEE/LIBAEE: shell: cant create socket with aed: Connection refused

About opacity bitmap

Could you write function set alpha (opacity) for bitmap?
Because In Android I must create bitmap when set opacity for bitmap?

Out of memory?

         dalvikvm-heap  E  Out of memory on a 61968400-byte allocation.
              dalvikvm  I  "AsyncTask #5" prio=5 tid=37 RUNNABLE
                        I    | group="main" sCount=0 dsCount=0 obj=0x44f98008 self=0x7796a680
                        I    | sysTid=26270 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=2018684768
                        I    | state=R schedstat=( 2619259960 691612270 6938 ) utm=219 stm=42 core=2
                        I    at android.graphics.Bitmap.nativeCreate(Native Method)
                        I    at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
                        I    at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
                        I    at android.graphics.Bitmap.createBitmap(Bitmap.java:753)
                        I    at com.jni.bitmap_operations.JniBitmapHolder.jniGetBitmapFromStoredBitmapData(Native Method)
                        I    at com.jni.bitmap_operations.JniBitmapHolder.getBitmap(JniBitmapHolder.java:88)
                        I    at com.jni.bitmap_operations.JniBitmapHolder.getBitmapAndFree(JniBitmapHolder.java:93)

Support for setPixels...

Hi,
I wanted to say thanks for these examples. I need to set offset and stride via setPixels to flip an image. Is there some issue with this using your code? I tried that after the memory was locked and before it is unlocked but it crashes when I execute. What I am doing is executing a glreadpixels call in my C code which flips the image and I need to reflip it before I use the bitmap. I hate to create another array to flip the image.
Thanks!

Crash | UnsatisfiedLinkError | couldn't find "libJniBitmapOperationsLibrary.so"

Hi,
I'm trying to work with the library, but it carshes when I'm trying to call:
JniBitmapHolder jniBitmapHolder = new JniBitmapHolder();

It can be the Architecture ?
Mine is AArch64 / arm64-v8a.

Any help will be appreciated !

Thank you !

StackTrace -
2020-05-13 22:43:47.690 6841-6927/com.example.camera2app E/AndroidRuntime: FATAL EXCEPTION: Camera2Thread
Process: com.example.camera2app, PID: 6841
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.camera2app-OY-F6Bi4RdzSpscSZm7zRg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.camera2app-OY-F6Bi4RdzSpscSZm7zRg==/lib/arm64, /data/app/com.example.camera2app-OY-F6Bi4RdzSpscSZm7zRg==/base.apk!/lib/arm64-v8a, /system/lib64, /system/product/lib64]]] couldn't find "libJniBitmapOperationsLibrary.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
at java.lang.System.loadLibrary(System.java:1667)
at com.example.camera2app.JniBitmapHolder.(JniBitmapHolder.java:17)

bitmap data lay ABGR not ARGB

hi, nice project and I learn how to make android bitmap just in jni.

I use some of the codes to create bitmap and dump my own colors in jni.

just find out that though in ARGB_8888.

int32_t convertArgbToInt(ARGB argb)
{
return (argb.alpha) | (argb.red << 24) | (argb.green << 16)
| (argb.blue << 8);
}
not working normal. and changed to

    return  (argb.alpha << 24) | (argb.blue << 16)
        | (argb.green << 8 | (argb.red));

I find the data actually lays in ABGR.

Thanks again for your code.

Crashes on ART

Hi,
First thanks very much for this excellent library! This really solves my OOM issue.
This library will crash on ART, due to the tighter jni check rule. More specifically, we cannot pass 0 capacity to NewDirectByteBuffer() (around line 305 in JniBitmapOperationsLibrary.cpp).
I simply changed the capacity to 1 and it works. I'm not quite familiar with JNI, so you might better take a look yourself.

Hope this will help.

Best

Couldn't load JniBitmapOperationsLibrary causes java.lang.ExceptionInInitializerError

04-10 13:00:02.414: E/AndroidRuntime(20393): FATAL EXCEPTION: main
04-10 13:00:02.414: E/AndroidRuntime(20393): java.lang.ExceptionInInitializerError
...
04-10 13:00:02.414: E/AndroidRuntime(20393): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load JniBitmapOperationsLibrary: findLibrary returned null
04-10 13:00:02.414: E/AndroidRuntime(20393): at java.lang.Runtime.loadLibrary(Runtime.java:365)
04-10 13:00:02.414: E/AndroidRuntime(20393): at java.lang.System.loadLibrary(System.java:535)
04-10 13:00:02.414: E/AndroidRuntime(20393): at com.jni.bitmap_operations.JniBitmapHolder.(JniBitmapHolder.java:11)
04-10 13:00:02.414: E/AndroidRuntime(20393): ... 16 more

Out of Memory before save image to JNI memory

Hi,first of all,thank you for the greate knowledge about JNI memory.But i just wonder that in your example final Bitmap b = BitmapFactory.decodeResource(getResources(), IMAGE_RESID_TO_TEST); is using the heap memory to load the bitmap 1st and then save the bitmap into the JNI memory.And if the bitmap is huge so it will throw the out of memory exception.I know that it can use BitmapOptions to scale the image to a smaller size,but is there any way to check the bitmap is outof(heap)memory before decode it?

Question about performance

I got these results:
getJniResizedBmp [1498ms]
getResizedBmp [443ms]

(measurements are done using Jake Wharton's hugo library)

This is what getResizedBmp does:
https://gist.github.com/Den-Rimus/0595a88a9424e005c7f2

Image parameters are something around 3000+ x 2000+ downscale to 1365x1024 (height x width)

I understand that Bilinear Interpolation gives better quality that Java's stock methods, but are mine results somehow comparable with this lib author's?

Load Bitmap from Uri or path file.

It's not an issue report.

How can I load a bitmap from a Uri or File path? There is a way to accomplish this with this library or not?
If not, there are a possibility to implement?

Question about thread safety

First off, thanks for the great library!

I was wondering if the library was thread safe. I tried running some operations on a bunch of different bitmaps on different threads and got a crash in the library but I didn't analyze it all that much.

Cheers!
Justin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.