AndroidJniBitmapOperations


Source link: https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations

AndroidJniBitmapOperations

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

Some of the operations are:

  • store/restore bitmaps to/from JNI.
  • rotate CW/CCW 90,180,270 degrees.
  • crop image.
  • flip image horizontally/vertically .
  • scale image using either "Nearest-Neighbor" algorithm or "Bilinear-Interpolation" algorithm. The first is fast but might cause aliasing artifacts on some cases, and the other is a bit slower but resizes the images nicely and avoids having aliasing artifacts. However, it cause the output image to be a bit softer/blurry. More information about those algorithms here: http://en.wikipedia.org/wiki/Image_scaling

As the resizing algorithms deal with colors, they also show how to create your own algorithms for handling pixels. You can make filters and implement other ways to resize images. Please consider contributing your own code for such operations.

This library was first introduced via StackOverflow, and many of the notes written there still hold now. Please read it here: http://stackoverflow.com/questions/18250951/jni-bitmap-operations-for-helping-to-avoid-oom-when-using-large-images/18250952?noredirect=1

Screenshot

Here's a sample of what can be done:

Known issues

Android-Studio still doesn't support C/C++ code well. It's easy to import the project and try it, but I think it's quite hard to do it for your own project.

Missing features (TODO)

The things I think this library should have :

  1. using matrices for manipulating of the images.
  2. decode the image directly within JNI, instead of giving it from the Java "world". This should be very handy.
  3. use different bitmap formats. Also think how to manage them nicely.
  4. get current bitmap info.
  5. face detection
  6. rotation by any angle.
  7. other basic operations that are available on the Android framework.
  8. Make more optimizations, perhaps by investigating the numebr of cache-misses, which is the biggest "enemy" for image manipulations in case of large bitmap. See this link for more information.

How to import the library project

Eclipse

Since ADT (at least till v22.6.2) still has problems importing Android libraries that have C/C++ code (made a post about it here) , the steps are:

  1. in case the library has a ".cproject" file , delete it.
  2. delete folders "libs","gen","bin",obj" from the library folder. In case you have libraries, just remove the files you didn't add yourself.
  3. in case the library has "cnature" or "ccnature" entries in the ".project" file, delete them, which look like:
<nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature> <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature> <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature> 

Also, you might need to delete those whole "buildCommand" tags (and their children) :

org.eclipse.cdt.managedbuilder.core.genmakebuilder org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 
  1. right click the library, choose "add native support..." via the "android tools" context menu. make sure the name of the suggested file is the same as your C/C++ file. Make sure that it's being built using the NDK , or you won't be able to do it correctly.
  2. build&compile the library.
  3. you are ready to go.

For now, I've handled steps 1-2 (I just made Git to ignore those files), so all you need to do is the rest of the steps.

Android studio

Precondition: make sure that you have NDK installed and you either have this line in your local.properties

ndk.dir=/path/to/ndk

or you have ANDROID_NDK_HOME environment variable set.

Getting started

Just import the whole cloned project and run the sample.

Further configuring and using as the library

  1. Copy JniBitmapOperationsLibrary.cpp into src/main/jni directory:

  2. Add this minimum NDK config to your build.gradle

     android {
      ...
    
    defaultConfig {
    
    ...
    
     ndk {
    
      moduleName "JniBitmapOperationsLibrary"
    
      ldLibs "log",  "jnigraphics"
    
      //optional: filter abis to compile for: abiFilters "x86", "armeabi-v7a"
    
      //otherwise it will compile for all abis: "armeabi", "armeabi-v7a", "x86", and "mips"
    
     
    }
    
    }
      
    }
     
  3. Copy JniBitmapHolder into the project, putting it into the same package ( com.jni.bitmap_operations).

You now should be able to use JniBitmapHolder to process images on NDK side.

Similar libraries

If you are interested in more features, and don't want to modify the code of this library, you could try out those similar libraries:

Resources

Easy way to hide Android keyboard on background tap without any effort.

A simple library that provides an AES encrypted version of the Android shared preferences.

The easiest way to handle android runtime permissions. This project is a helper class to do the same.

Loading effect for the child views in Adapter View before binding data from service.

Set of helpful utils for the Android platform.

This plugin provides a task to generate a HTML license report based on the configuration. (eg. licenseDebugReport for all debug dependencies in an Android project).

Applying this to an Android or Java project will generate a the license file (open_source_licenses.html) in the <project>/build/reports/licenses/.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes