• Stars
    star
    540
  • Rank 82,257 (Top 2 %)
  • Language
    C++
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

Starting from Android 11 (R - API 30), it seems to be possible to also decode the bitmaps right in JNI, so this might be handy to perform operations on it right away (though not sure how to do it) : https://developer.android.com/ndk/guides/image-decoder

Screenshot

Here's a sample of what can be done:

animated demo

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

Option 1

  1. Add this repository as a git submodule. For these instructions we added in a folder named AndroidJniBitmapOperations

  2. Add the following lines to your settings.gradle file

    include ':JniBitmapOperationsLibrary'
    project(':JniBitmapOperationsLibrary').projectDir = new File(rootProject.getProjectDir(), 'AndroidJniBitmapOperations/JniBitmapOperationsLibrary')
    
  3. Add the following lines to your top level build.gradle file inside the buildscript section. Replace the versions with whatever your project is using as needed.

    // Variables for JniBitmapOperationsLibrary
    ext.propCompileSdkVersion = 23
    ext.propBuildToolsVersion = "27.0.3"
    
  4. Add the following lines to your app build.gradle file inside the dependancies section

    implementation project(':JniBitmapOperationsLibrary')
    

Option 2

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

    Studio folder structure

  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:

More Repositories

1

AutoFitTextView

A TextView that automatically fit its font and line count based on its available size and content
Kotlin
912
star
2

LollipopContactsRecyclerViewFastScroller

A sample of how to mimic the way that the contacts app handles a Fast-Scroller for a RecyclerView
Java
542
star
3

MaterialPreferenceLibrary

Allows to have an ActionBar on PreferenceActivity
Java
408
star
4

ListViewVariants

Provides special ways to handle ListViews, including PinnedHeaderListView in Lollipop's Contacts' app style
Java
336
star
5

ChipsLibrary

A fork to Google's Gmail/Hangouts chips library, with some extra features
Java
301
star
6

VideoTrimmer

Allows to trim videos on Android, including UI
Kotlin
144
star
7

RootHelper

An extension to use libsuperuser library more easily
Kotlin
123
star
8

ThreePhasesBottomSheet

A bottom sheet sample that's similar to how Google Maps treat it
Java
110
star
9

WebpifyYourAndroidApp

A small tool to convert your app's png&jpg image files into WebP when possible
Java
105
star
10

LB-Launcher

A truly open sourced launcher app, based on the same launcher app that comes with Android
Java
49
star
11

PhoneCallRecorder

A POC of recording calls
Kotlin
41
star
12

ParallaxViewPagers

A POC to show how to have multiple ViewPagers work in Parallax effect with each other
Java
26
star
13

apk-parser

Java
20
star
14

MultiTouchPlaceholderView

Gesture-based image layer, to move it into placeholders (empty content within a bitmap)
Java
19
star
15

CommonUtils

Just a set of Android classes and functions that I commonly use in various projects
Kotlin
16
star
16

ExoPlayerCacheSample

A sample to demonstrate how to use cache on ExoPlayer, and to investigate the best way to use the cached file later
Kotlin
13
star
17

FastScrollerAndRecyclerViewFixes

A collection of fixes for FastScroller
Java
12
star
18

DayAndNightDetector

Detects if it's now day or night using the TwilightCalculator class by Google
Kotlin
9
star
19

AsyncTaskEx

A modified version of AsyncTask, in Kotlin, with some things removed and some added
Kotlin
7
star
20

AndroidVersionsStats

Gets updated information about the version statistics of Android distribution, as found from Android Studio code
Kotlin
7
star
21

DialogShard

An alternative to DialogFragment, that's intended to overcome fragments exceptions
Java
7
star
22

ScreenshotSample

shows a sample of how to take a screenshot on Android
Java
7
star
23

WallpaperPicker

A port of Android's Wallpaper-picker, as used in the launcher itself
Java
6
star
24

UserActivityRecognitionSample

A sample to show how to handle User-Activity-Recognition, based on https://github.com/jarroyoesp/TransitionRecognitionApp
Kotlin
4
star
25

FloatingActionButtonEx

A modified version of FloatingActionButton library, that better handles various issues
Java
4
star
26

GifLiveWallpaper

An example of showing GIF animation using Movie class of Android, based on : https://stackoverflow.com/a/51127570/878126
Kotlin
4
star
27

FullSizePopupSpinner

A spinner-like view, which shows all items below, including indication of which item is selected
Java
4
star
28

SdkTest

Just some test for Jitpack, to see if it can handle aar files in Github
3
star
29

VideoAndAudioMux

A POC about how to mux (join) video and audio files together into a single video file that has the audio in it
Kotlin
3
star
30

RecyclerViewDragAndDropTest

A sample to show how to have drag&drop functionality on RecyclerView (and optionally also swipe-to-dismiss)
Kotlin
3
star
31

customized-popup-window-sample

A sample of having a customized popup window, that should work like context menu
Kotlin
2
star
32

ExoPlayerScaleCrop

POC to show how to scale-crop at any point of the video, and not just like center-crop of ImageView
Kotlin
2
star
33

DocumentsProviderSample

A very small sample of how to implement DocumentsProvider, to have new items of Android's file-picker
Kotlin
2
star
34

AutomatedTextSwitcher

A TextSwitcher that allows automatic switching of texts within itself
Kotlin
1
star
35

AlarmClockSample

Just a test to show how to set an alarm on Android
Kotlin
1
star
36

GetPlayStoreInstalledAppsHistory

A POC to get history of installed apps from the Play Store ("library"), based on Aurora Store source code
Kotlin
1
star
37

OpenGlVideoLiveWallpaper

A minimized sample of showing a video inside a live wallpaper using OpenGl, based on alynx-live-wallpaper repository
Kotlin
1
star
38

ZipFileProviderSample

Demonstrates how to share a zip file that contains multiple files, without actually creating the zipped file
Kotlin
1
star
39

VectorChildFinder

A repository to allows finding inner parts of VectorDrawable, and perform operations on them, based on VectorChildFinder repository
Java
1
star
40

ApkManifestFetcher

A sample of how to try to parse all kinds of APK manifest content , including ability to parse via InputStream
Kotlin
1
star
41

MovieLiveWallpaper

Sample app to show how to view a video in a live wallpaper app on Android
Kotlin
1
star