8 |
9 | #define LOG_TAG "Applog"
10 | #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
11 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
12 |
13 | extern "C"
14 | {
15 | //store
16 | JNIEXPORT jobject JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniStoreBitmapData(
17 | JNIEnv * env, jobject obj, jobject bitmap);
18 | //get
19 | JNIEXPORT jobject JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniGetBitmapFromStoredBitmapData(
20 | JNIEnv * env, jobject obj, jobject handle);
21 | //free
22 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFreeBitmapData(
23 | JNIEnv * env, jobject obj, jobject handle);
24 | //rotate 90 degrees CCW
25 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniRotateBitmapCcw90(
26 | JNIEnv * env, jobject obj, jobject handle);
27 | //rotate 90 degrees CW
28 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniRotateBitmapCw90(
29 | JNIEnv * env, jobject obj, jobject handle);
30 | //rotate 180 degrees
31 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniRotateBitmap180(
32 | JNIEnv * env, jobject obj, jobject handle);
33 | //crop
34 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniCropBitmap(
35 | JNIEnv * env, jobject obj, jobject handle, uint32_t left,
36 | uint32_t top, uint32_t right, uint32_t bottom);
37 | //scale using nearest neighbor
38 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniScaleNNBitmap(
39 | JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
40 | uint32_t newHeight);
41 |
42 | //scale using Bilinear Interpolation
43 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniScaleBIBitmap(
44 | JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
45 | uint32_t newHeight);
46 |
47 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFlipBitmapHorizontal(
48 | JNIEnv * env, jobject obj, jobject handle);
49 |
50 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFlipBitmapVertical(
51 | JNIEnv * env, jobject obj, jobject handle);
52 | }
53 |
54 | class JniBitmap
55 | {
56 | public:
57 | uint32_t* _storedBitmapPixels;
58 | AndroidBitmapInfo _bitmapInfo;
59 | JniBitmap()
60 | {
61 | _storedBitmapPixels = NULL;
62 | }
63 | };
64 |
65 | typedef struct
66 | {
67 | uint8_t alpha, red, green, blue;
68 | } ARGB;
69 |
70 | int32_t convertArgbToInt(ARGB argb)
71 | {
72 | return (argb.alpha) | (argb.red << 24) | (argb.green << 16)
73 | | (argb.blue << 8);
74 | }
75 |
76 | void convertIntToArgb(uint32_t pixel, ARGB* argb)
77 | {
78 | argb->red = ((pixel >> 24) & 0xff);
79 | argb->green = ((pixel >> 16) & 0xff);
80 | argb->blue = ((pixel >> 8) & 0xff);
81 | argb->alpha = (pixel & 0xff);
82 | }
83 |
84 | /**crops the bitmap within to be smaller. note that no validations are done*/ //
85 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniCropBitmap(
86 | JNIEnv * env, jobject obj, jobject handle, uint32_t left, uint32_t top,
87 | uint32_t right, uint32_t bottom)
88 | {
89 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
90 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
91 | return;
92 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
93 | uint32_t oldWidth = jniBitmap->_bitmapInfo.width;
94 | uint32_t newWidth = right - left, newHeight = bottom - top;
95 | uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
96 | uint32_t* whereToGet = previousData + left + top * oldWidth;
97 | uint32_t* whereToPut = newBitmapPixels;
98 | for (int y = top; y < bottom; ++y)
99 | {
100 | memcpy(whereToPut, whereToGet, sizeof(uint32_t) * newWidth);
101 | whereToGet += oldWidth;
102 | whereToPut += newWidth;
103 | }
104 | //done copying , so replace old data with new one
105 | delete[] previousData;
106 | jniBitmap->_storedBitmapPixels = newBitmapPixels;
107 | jniBitmap->_bitmapInfo.width = newWidth;
108 | jniBitmap->_bitmapInfo.height = newHeight;
109 | }
110 |
111 | /**rotates the inner bitmap data by 90 degrees counter clock wise*/ //
112 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniRotateBitmapCcw90(
113 | JNIEnv * env, jobject obj, jobject handle)
114 | {
115 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
116 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
117 | return;
118 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
119 | uint32_t newWidth = jniBitmap->_bitmapInfo.height;
120 | uint32_t newHeight = jniBitmap->_bitmapInfo.width;
121 | jniBitmap->_bitmapInfo.width = newWidth;
122 | jniBitmap->_bitmapInfo.height = newHeight;
123 | uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
124 | int whereToGet = 0;
125 | // XY. ... ... ..X
126 | // ...>Y..>...>..Y
127 | // ... X.. .YX ...
128 | for (int x = 0; x < newWidth; ++x)
129 | for (int y = newHeight - 1; y >= 0; --y)
130 | {
131 | //take from each row (up to bottom), from left to right
132 | uint32_t pixel = previousData[whereToGet++];
133 | newBitmapPixels[newWidth * y + x] = pixel;
134 | }
135 | delete[] previousData;
136 | jniBitmap->_storedBitmapPixels = newBitmapPixels;
137 | }
138 |
139 | /**rotates the inner bitmap data by 90 degrees clock wise*/ //
140 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniRotateBitmapCw90(
141 | JNIEnv * env, jobject obj, jobject handle)
142 | {
143 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
144 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
145 | return;
146 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
147 | uint32_t newWidth = jniBitmap->_bitmapInfo.height;
148 | uint32_t newHeight = jniBitmap->_bitmapInfo.width;
149 | jniBitmap->_bitmapInfo.width = newWidth;
150 | jniBitmap->_bitmapInfo.height = newHeight;
151 | uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
152 | int whereToGet = 0;
153 | // XY. ..X ... ...
154 | // ...>..Y>...>Y..
155 | // ... ... .YX X..
156 | jniBitmap->_storedBitmapPixels = newBitmapPixels;
157 | for (int x = newWidth - 1; x >= 0; --x)
158 | for (int y = 0; y < newHeight; ++y)
159 | {
160 | //take from each row (up to bottom), from left to right
161 | uint32_t pixel = previousData[whereToGet++];
162 | newBitmapPixels[newWidth * y + x] = pixel;
163 | }
164 | delete[] previousData;
165 | }
166 |
167 | /**rotates the inner bitmap data by 180 degrees (*/ //
168 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniRotateBitmap180(
169 | JNIEnv * env, jobject obj, jobject handle)
170 | {
171 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
172 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
173 | return;
174 | uint32_t* pixels = jniBitmap->_storedBitmapPixels;
175 | uint32_t* pixels2 = jniBitmap->_storedBitmapPixels;
176 | uint32_t width = jniBitmap->_bitmapInfo.width;
177 | uint32_t height = jniBitmap->_bitmapInfo.height;
178 | //no need to create a totally new bitmap - it's the exact same size as the original
179 | // 1234 fedc
180 | // 5678>ba09
181 | // 90ab>8765
182 | // cdef 4321
183 | int whereToGet = 0;
184 | for (int y = height - 1; y >= height / 2; --y)
185 | for (int x = width - 1; x >= 0; --x)
186 | {
187 | //take from each row (up to bottom), from left to right
188 | uint32_t tempPixel = pixels2[width * y + x];
189 | pixels2[width * y + x] = pixels[whereToGet];
190 | pixels[whereToGet] = tempPixel;
191 | ++whereToGet;
192 | }
193 | //if the height isn't even, flip the middle row :
194 | if (height % 2 == 1)
195 | {
196 | int y = height / 2;
197 | whereToGet = width * y;
198 | int lastXToHandle = width % 2 == 0 ? (width / 2) : (width / 2) - 1;
199 | for (int x = width - 1; x >= lastXToHandle; --x)
200 | {
201 | uint32_t tempPixel = pixels2[width * y + x];
202 | pixels2[width * y + x] = pixels[whereToGet];
203 | pixels[whereToGet] = tempPixel;
204 | ++whereToGet;
205 | }
206 | }
207 | }
208 |
209 | /**free bitmap*/ //
210 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFreeBitmapData(
211 | JNIEnv * env, jobject obj, jobject handle)
212 | {
213 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
214 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
215 | return;
216 | delete[] jniBitmap->_storedBitmapPixels;
217 | jniBitmap->_storedBitmapPixels = NULL;
218 | delete jniBitmap;
219 | }
220 |
221 | /**restore java bitmap (from JNI data)*/ //
222 | JNIEXPORT jobject JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniGetBitmapFromStoredBitmapData(
223 | JNIEnv * env, jobject obj, jobject handle)
224 | {
225 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
226 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
227 | {
228 | LOGD("no bitmap data was stored. returning null...");
229 | return NULL;
230 | }
231 | //
232 | //creating a new bitmap to put the pixels into it - using Bitmap Bitmap.createBitmap (int width, int height, Bitmap.Config config) :
233 | //
234 | jclass bitmapCls = env->FindClass("android/graphics/Bitmap");
235 | jmethodID createBitmapFunction = env->GetStaticMethodID(bitmapCls,
236 | "createBitmap",
237 | "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
238 | jstring configName = env->NewStringUTF("ARGB_8888");
239 | jclass bitmapConfigClass = env->FindClass("android/graphics/Bitmap$Config");
240 | jmethodID valueOfBitmapConfigFunction = env->GetStaticMethodID(
241 | bitmapConfigClass, "valueOf",
242 | "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;");
243 | jobject bitmapConfig = env->CallStaticObjectMethod(bitmapConfigClass,
244 | valueOfBitmapConfigFunction, configName);
245 | jobject newBitmap = env->CallStaticObjectMethod(bitmapCls,
246 | createBitmapFunction, jniBitmap->_bitmapInfo.width,
247 | jniBitmap->_bitmapInfo.height, bitmapConfig);
248 | //
249 | // putting the pixels into the new bitmap:
250 | //
251 | int ret;
252 | void* bitmapPixels;
253 | if ((ret = AndroidBitmap_lockPixels(env, newBitmap, &bitmapPixels)) < 0)
254 | {
255 | LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
256 | return NULL;
257 | }
258 | uint32_t* newBitmapPixels = (uint32_t*) bitmapPixels;
259 | int pixelsCount = jniBitmap->_bitmapInfo.height
260 | * jniBitmap->_bitmapInfo.width;
261 | memcpy(newBitmapPixels, jniBitmap->_storedBitmapPixels,
262 | sizeof(uint32_t) * pixelsCount);
263 | AndroidBitmap_unlockPixels(env, newBitmap);
264 | //LOGD("returning the new bitmap");
265 | return newBitmap;
266 | }
267 |
268 | /**store java bitmap as JNI data*/ //
269 | JNIEXPORT jobject JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniStoreBitmapData(
270 | JNIEnv * env, jobject obj, jobject bitmap)
271 | {
272 | AndroidBitmapInfo bitmapInfo;
273 | uint32_t* storedBitmapPixels = NULL;
274 | //LOGD("reading bitmap info...");
275 | int ret;
276 | if ((ret = AndroidBitmap_getInfo(env, bitmap, &bitmapInfo)) < 0)
277 | {
278 | LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
279 | return NULL;
280 | }
281 | //LOGD("width:%d height:%d stride:%d", bitmapInfo.width, bitmapInfo.height, bitmapInfo.stride);
282 | if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
283 | {
284 | LOGE("Bitmap format is not RGBA_8888!");
285 | return NULL;
286 | }
287 | //
288 | //read pixels of bitmap into native memory :
289 | //
290 | //LOGD("reading bitmap pixels...");
291 | void* bitmapPixels;
292 | if ((ret = AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels)) < 0)
293 | {
294 | LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
295 | return NULL;
296 | }
297 | uint32_t* src = (uint32_t*) bitmapPixels;
298 | storedBitmapPixels = new uint32_t[bitmapInfo.height * bitmapInfo.width];
299 | int pixelsCount = bitmapInfo.height * bitmapInfo.width;
300 | memcpy(storedBitmapPixels, src, sizeof(uint32_t) * pixelsCount);
301 | AndroidBitmap_unlockPixels(env, bitmap);
302 | JniBitmap *jniBitmap = new JniBitmap();
303 | jniBitmap->_bitmapInfo = bitmapInfo;
304 | jniBitmap->_storedBitmapPixels = storedBitmapPixels;
305 | return env->NewDirectByteBuffer(jniBitmap, 0);
306 | }
307 |
308 | /**scales the image using the fastest, simplest algorithm called "nearest neighbor" */ //
309 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniScaleNNBitmap(
310 | JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
311 | uint32_t newHeight)
312 | {
313 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
314 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
315 | return;
316 | uint32_t oldWidth = jniBitmap->_bitmapInfo.width;
317 | uint32_t oldHeight = jniBitmap->_bitmapInfo.height;
318 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
319 | uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
320 | int x2, y2;
321 | int whereToPut = 0;
322 | for (int y = 0; y < newHeight; ++y)
323 | {
324 | for (int x = 0; x < newWidth; ++x)
325 | {
326 | x2 = x * oldWidth / newWidth;
327 | if (x2 < 0)
328 | x2 = 0;
329 | else if (x2 >= oldWidth)
330 | x2 = oldWidth - 1;
331 | y2 = y * oldHeight / newHeight;
332 | if (y2 < 0)
333 | y2 = 0;
334 | else if (y2 >= oldHeight)
335 | y2 = oldHeight - 1;
336 | newBitmapPixels[whereToPut++] = previousData[(y2 * oldWidth) + x2];
337 | //same as : newBitmapPixels[(y * newWidth) + x] = previousData[(y2 * oldWidth) + x2];
338 | }
339 | }
340 |
341 | delete[] previousData;
342 | jniBitmap->_storedBitmapPixels = newBitmapPixels;
343 | jniBitmap->_bitmapInfo.width = newWidth;
344 | jniBitmap->_bitmapInfo.height = newHeight;
345 | }
346 |
347 | /**scales the image using a high-quality algorithm called "Bilinear Interpolation"
348 | * code is based on old university code I've made in Java: http://stackoverflow.com/questions/23230047/trying-to-convert-bilinear-interpolation-code-from-java-to-c-c-on-android/23302384#23302384
349 | * */ //
350 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniScaleBIBitmap(
351 | JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
352 | uint32_t newHeight)
353 | {
354 |
355 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
356 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
357 | return;
358 | uint32_t oldWidth = jniBitmap->_bitmapInfo.width;
359 | uint32_t oldHeight = jniBitmap->_bitmapInfo.height;
360 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
361 | uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
362 | // position of the top left pixel of the 4 pixels to use interpolation on
363 | int xTopLeft, yTopLeft;
364 | int x, y, lastTopLefty;
365 | float xRatio = (float) newWidth / (float) oldWidth, yratio =
366 | (float) newHeight / (float) oldHeight;
367 | // Y color ratio to use on left and right pixels for interpolation
368 | float ycRatio2 = 0, ycRatio1 = 0;
369 | // pixel target in the src
370 | float xt, yt;
371 | // X color ratio to use on left and right pixels for interpolation
372 | float xcRatio2 = 0, xcratio1 = 0;
373 | ARGB rgbTopLeft, rgbTopRight, rgbBottomLeft, rgbBottomRight, rgbTopMiddle,
374 | rgbBottomMiddle, result;
375 | for (x = 0; x < newWidth; ++x)
376 | {
377 | xTopLeft = (int) (xt = x / xRatio);
378 | // when meeting the most right edge, move left a little
379 | if (xTopLeft >= oldWidth - 1)
380 | xTopLeft--;
381 | if (xt <= xTopLeft + 1)
382 | {
383 | // we are between the left and right pixel
384 | xcratio1 = xt - xTopLeft;
385 | // color ratio in favor of the right pixel color
386 | xcRatio2 = 1 - xcratio1;
387 | }
388 | for (y = 0, lastTopLefty = -30000; y < newHeight; ++y)
389 | {
390 | yTopLeft = (int) (yt = y / yratio);
391 | // when meeting the most bottom edge, move up a little
392 | if (yTopLeft >= oldHeight - 1)
393 | --yTopLeft;
394 | if (lastTopLefty == yTopLeft - 1)
395 | {
396 | // we went down only one rectangle
397 | rgbTopLeft = rgbBottomLeft;
398 | rgbTopRight = rgbBottomRight;
399 | rgbTopMiddle = rgbBottomMiddle;
400 | //rgbBottomLeft=startingImageData[xTopLeft][yTopLeft+1];
401 | convertIntToArgb(
402 | previousData[((yTopLeft + 1) * oldWidth) + xTopLeft],
403 | &rgbBottomLeft);
404 | //rgbBottomRight=startingImageData[xTopLeft+1][yTopLeft+1];
405 | convertIntToArgb(
406 | previousData[((yTopLeft + 1) * oldWidth)
407 | + (xTopLeft + 1)], &rgbBottomRight);
408 | rgbBottomMiddle.alpha = rgbBottomLeft.alpha * xcRatio2
409 | + rgbBottomRight.alpha * xcratio1;
410 | rgbBottomMiddle.red = rgbBottomLeft.red * xcRatio2
411 | + rgbBottomRight.red * xcratio1;
412 | rgbBottomMiddle.green = rgbBottomLeft.green * xcRatio2
413 | + rgbBottomRight.green * xcratio1;
414 | rgbBottomMiddle.blue = rgbBottomLeft.blue * xcRatio2
415 | + rgbBottomRight.blue * xcratio1;
416 | }
417 | else if (lastTopLefty != yTopLeft)
418 | {
419 | // we went to a totally different rectangle (happens in every loop start,and might happen more when making the picture smaller)
420 | //rgbTopLeft=startingImageData[xTopLeft][yTopLeft];
421 | convertIntToArgb(previousData[(yTopLeft * oldWidth) + xTopLeft],
422 | &rgbTopLeft);
423 | //rgbTopRight=startingImageData[xTopLeft+1][yTopLeft];
424 | convertIntToArgb(
425 | previousData[(yTopLeft* oldWidth) + xTopLeft + 1],
426 | &rgbTopRight);
427 | rgbTopMiddle.alpha = rgbTopLeft.alpha * xcRatio2
428 | + rgbTopRight.alpha * xcratio1;
429 | rgbTopMiddle.red = rgbTopLeft.red * xcRatio2
430 | + rgbTopRight.red * xcratio1;
431 | rgbTopMiddle.green = rgbTopLeft.green * xcRatio2
432 | + rgbTopRight.green * xcratio1;
433 | rgbTopMiddle.blue = rgbTopLeft.blue * xcRatio2
434 | + rgbTopRight.blue * xcratio1;
435 | //rgbBottomLeft=startingImageData[xTopLeft][yTopLeft+1];
436 | convertIntToArgb(
437 | previousData[((yTopLeft + 1) * oldWidth) + xTopLeft],
438 | &rgbBottomLeft);
439 | //rgbBottomRight=startingImageData[xTopLeft+1][yTopLeft+1];
440 | convertIntToArgb(
441 | previousData[((yTopLeft + 1) * oldWidth)
442 | + (xTopLeft + 1)], &rgbBottomRight);
443 | rgbBottomMiddle.alpha = rgbBottomLeft.alpha * xcRatio2
444 | + rgbBottomRight.alpha * xcratio1;
445 | rgbBottomMiddle.red = rgbBottomLeft.red * xcRatio2
446 | + rgbBottomRight.red * xcratio1;
447 | rgbBottomMiddle.green = rgbBottomLeft.green * xcRatio2
448 | + rgbBottomRight.green * xcratio1;
449 | rgbBottomMiddle.blue = rgbBottomLeft.blue * xcRatio2
450 | + rgbBottomRight.blue * xcratio1;
451 | }
452 | lastTopLefty = yTopLeft;
453 | if (yt <= yTopLeft + 1)
454 | {
455 | // color ratio in favor of the bottom pixel color
456 | ycRatio1 = yt - yTopLeft;
457 | ycRatio2 = 1 - ycRatio1;
458 | }
459 | // prepared all pixels to look at, so finally set the new pixel data
460 | result.alpha = rgbTopMiddle.alpha * ycRatio2
461 | + rgbBottomMiddle.alpha * ycRatio1;
462 | result.blue = rgbTopMiddle.blue * ycRatio2
463 | + rgbBottomMiddle.blue * ycRatio1;
464 | result.red = rgbTopMiddle.red * ycRatio2
465 | + rgbBottomMiddle.red * ycRatio1;
466 | result.green = rgbTopMiddle.green * ycRatio2
467 | + rgbBottomMiddle.green * ycRatio1;
468 | newBitmapPixels[(y * newWidth) + x] = convertArgbToInt(result);
469 | }
470 | }
471 | //get rid of old data, and replace it with new one
472 | delete[] previousData;
473 | jniBitmap->_storedBitmapPixels = newBitmapPixels;
474 | jniBitmap->_bitmapInfo.width = newWidth;
475 | jniBitmap->_bitmapInfo.height = newHeight;
476 | }
477 |
478 | /**flips a bitmap horizontally, as such:
479 | *
480 | * 123 321
481 | * 456 => 654
482 | * 789 987
483 | *
484 | * */ //
485 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFlipBitmapHorizontal(
486 | JNIEnv * env, jobject obj, jobject handle)
487 | {
488 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
489 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
490 | return;
491 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
492 | int width = jniBitmap->_bitmapInfo.width, middle = width / 2, height =
493 | jniBitmap->_bitmapInfo.height;
494 | for (int y = 0; y < height; ++y)
495 | {
496 | //for each row, switch between the first pixels and the last ones
497 | uint32_t* idx1 = previousData + width * y;
498 | uint32_t* idx2 = previousData + width * (y + 1) - 1;
499 | for (int x = 0; x < middle; ++x)
500 | {
501 | uint32_t pixel = *idx1; //pixel= previousData[rowStart + x];
502 | *idx1 = *idx2; //previousData[rowStart + x] =previousData[rowStart + (width - x - 1)];
503 | *idx2 = pixel; //previousData[rowStart + (width - x - 1)] = pixel;
504 | ++idx1;
505 | --idx2;
506 | }
507 | }
508 | }
509 |
510 | /**flips a bitmap vertically, as such:
511 | *
512 | * 123 789
513 | * 456 => 456
514 | * 789 123
515 | *
516 | * */ //
517 | JNIEXPORT void JNICALL Java_com_jni_bitmap_1operations_JniBitmapHolder_jniFlipBitmapVertical(
518 | JNIEnv * env, jobject obj, jobject handle)
519 | {
520 | JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
521 | if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
522 | return;
523 | uint32_t* previousData = jniBitmap->_storedBitmapPixels;
524 | int width = jniBitmap->_bitmapInfo.width, height =
525 | jniBitmap->_bitmapInfo.height, middle = height / 2;
526 | for (int y = 0; y < middle; ++y)
527 | {
528 | //for each row till the middle row, switch its pixels with the one at the bottom
529 | uint32_t* idx1 = previousData + width * y;
530 | uint32_t* idx2 = previousData + width * (height - y - 1);
531 | for (int x = 0; x < width; ++x)
532 | {
533 | uint32_t pixel =*idx1;
534 | *idx1=*idx2;
535 | *idx2=pixel;
536 | ++idx2;
537 | ++idx1;
538 | }
539 | }
540 | }
541 |
--------------------------------------------------------------------------------
/JniBitmapOperationsLibrary/jni/do_not_delete_me_i_am_workaround.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/JniBitmapOperationsLibrary/jni/do_not_delete_me_i_am_workaround.c
--------------------------------------------------------------------------------
/JniBitmapOperationsLibrary/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/JniBitmapOperationsLibrary/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 | android.library=true
16 |
--------------------------------------------------------------------------------
/JniBitmapOperationsLibrary/src/com/jni/bitmap_operations/JniBitmapHolder.java:
--------------------------------------------------------------------------------
1 | package com.jni.bitmap_operations;
2 | import java.nio.ByteBuffer;
3 | import android.graphics.Bitmap;
4 | import android.util.Log;
5 |
6 | public class JniBitmapHolder
7 | {
8 | ByteBuffer _handler =null;
9 | static
10 | {
11 | System.loadLibrary("JniBitmapOperationsLibrary");
12 | }
13 |
14 | public enum ScaleMethod
15 | {
16 | NearestNeighbour,BilinearInterpolation
17 | }
18 |
19 | private native ByteBuffer jniStoreBitmapData(Bitmap bitmap);
20 |
21 | private native Bitmap jniGetBitmapFromStoredBitmapData(ByteBuffer handler);
22 |
23 | private native void jniFreeBitmapData(ByteBuffer handler);
24 |
25 | private native void jniRotateBitmapCcw90(ByteBuffer handler);
26 |
27 | private native void jniRotateBitmapCw90(ByteBuffer handler);
28 |
29 | private native void jniRotateBitmap180(ByteBuffer handler);
30 |
31 | private native void jniCropBitmap(ByteBuffer handler,final int left,final int top,final int right,final int bottom);
32 |
33 | private native void jniScaleNNBitmap(ByteBuffer handler,final int newWidth,final int newHeight);
34 |
35 | private native void jniScaleBIBitmap(ByteBuffer handler,final int newWidth,final int newHeight);
36 |
37 | private native void jniFlipBitmapHorizontal(ByteBuffer handler);
38 |
39 | private native void jniFlipBitmapVertical(ByteBuffer handler);
40 |
41 | public JniBitmapHolder()
42 | {}
43 |
44 | public JniBitmapHolder(final Bitmap bitmap)
45 | {
46 | storeBitmap(bitmap);
47 | }
48 |
49 | public void storeBitmap(final Bitmap bitmap)
50 | {
51 | if(_handler!=null)
52 | freeBitmap();
53 | _handler=jniStoreBitmapData(bitmap);
54 | }
55 |
56 | public void rotateBitmapCcw90()
57 | {
58 | if(_handler==null)
59 | return;
60 | jniRotateBitmapCcw90(_handler);
61 | }
62 |
63 | public void rotateBitmapCw90()
64 | {
65 | if(_handler==null)
66 | return;
67 | jniRotateBitmapCw90(_handler);
68 | }
69 |
70 | public void rotateBitmap180()
71 | {
72 | if(_handler==null)
73 | return;
74 | jniRotateBitmap180(_handler);
75 | }
76 |
77 | public void cropBitmap(final int left,final int top,final int right,final int bottom)
78 | {
79 | if(_handler==null)
80 | return;
81 | jniCropBitmap(_handler,left,top,right,bottom);
82 | }
83 |
84 | public Bitmap getBitmap()
85 | {
86 | if(_handler==null)
87 | return null;
88 | return jniGetBitmapFromStoredBitmapData(_handler);
89 | }
90 |
91 | public Bitmap getBitmapAndFree()
92 | {
93 | final Bitmap bitmap=getBitmap();
94 | freeBitmap();
95 | return bitmap;
96 | }
97 |
98 | public void scaleBitmap(final int newWidth,final int newHeight,final ScaleMethod scaleMethod)
99 | {
100 | if(_handler==null)
101 | return;
102 | switch(scaleMethod)
103 | {
104 | case BilinearInterpolation:
105 | jniScaleBIBitmap(_handler,newWidth,newHeight);
106 | break;
107 | case NearestNeighbour:
108 | jniScaleNNBitmap(_handler,newWidth,newHeight);
109 | break;
110 | }
111 | }
112 |
113 | /**
114 | * flips a bitmap horizontally, as such:
115 | *
116 | *
117 | * 123 321
118 | * 456 => 654
119 | * 789 987
120 | *
121 | */
122 | //
123 | public void flipBitmapHorizontal()
124 | {
125 | if(_handler==null)
126 | return;
127 | jniFlipBitmapHorizontal(_handler);
128 | }
129 |
130 | /**
131 | * Flips the bitmap on the vertically, as such:
132 | *
133 | *
134 | * 123 789
135 | * 456 => 456
136 | * 789 123
137 | *
138 | */
139 | public void flipBitmapVertical()
140 | {
141 | if(_handler==null)
142 | return;
143 | jniFlipBitmapVertical(_handler);
144 | }
145 |
146 | public void freeBitmap()
147 | {
148 | if(_handler==null)
149 | return;
150 | jniFreeBitmapData(_handler);
151 | _handler=null;
152 | }
153 |
154 | @Override
155 | protected void finalize() throws Throwable
156 | {
157 | super.finalize();
158 | if(_handler==null)
159 | return;
160 | Log.w("DEBUG","JNI bitmap wasn't freed nicely.please remember to free the bitmap as soon as you can");
161 | freeBitmap();
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AndroidJniBitmapOperations
2 |
3 | Allows to perform various simple operations on bitmaps via JNI , while also providing some protection against OOM using the native Java environment on Android
4 |
5 | Some of the operations are:
6 | - store/restore bitmaps to/from JNI.
7 | - rotate CW/CCW 90,180,270 degrees.
8 | - crop image.
9 | - flip image horizontally/vertically .
10 | - scale image using either "Nearest-Neighbor" algorithm or "Bilinear-Interpolation" algorithm.
11 | 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.
12 | However, it cause the output image to be a bit softer/blurry.
13 | More information about those algorithms here:
14 | http://en.wikipedia.org/wiki/Image_scaling
15 |
16 | As the resizing algorithms deal with colors, they also show how to create your own algorithms for handling pixels.
17 | You can make filters and implement other ways to resize images. Please consider contributing your own code for such operations.
18 |
19 | This library was first introduced via StackOverflow, and many of the notes written there still hold now.
20 | Please read it here:
21 | http://stackoverflow.com/questions/18250951/jni-bitmap-operations-for-helping-to-avoid-oom-when-using-large-images/18250952?noredirect=1
22 |
23 | 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) :
24 | https://developer.android.com/ndk/guides/image-decoder
25 |
26 | ## Screenshot
27 | Here's a sample of what can be done:
28 |
29 | 
30 |
31 | ## Known issues
32 | 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.
33 |
34 | ## Missing features (TODO)
35 |
36 | The things I think this library should have :
37 |
38 | 1. using matrices for manipulating of the images.
39 | 2. decode the image directly within JNI, instead of giving it from the Java "world". This should be very handy.
40 | 3. use different bitmap formats. Also think how to manage them nicely.
41 | 4. get current bitmap info.
42 | 5. face detection
43 | 6. rotation by any angle.
44 | 7. other basic operations that are available on the Android framework.
45 | 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**][3] for more information.
46 |
47 | ## How to import the library project
48 | ### Eclipse
49 |
50 | 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**][1]) , the steps are:
51 |
52 | 1. in case the library has a ".cproject" file , delete it.
53 | 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.
54 | 3. in case the library has "cnature" or "ccnature" entries in the ".project" file, delete them, which look like:
55 |
56 | > org.eclipse.cdt.core.cnature
57 | > org.eclipse.cdt.core.ccnature
58 | > org.eclipse.cdt.managedbuilder.core.managedBuildNature
59 | > org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
60 | Also, you might need to delete those whole "buildCommand" tags (and their children) :
61 |
62 | > org.eclipse.cdt.managedbuilder.core.genmakebuilder
63 | > org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder
64 |
65 | 4. 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**][2] , or you won't be able to do it correctly.
66 | 5. build&compile the library.
67 | 6. you are ready to go.
68 |
69 |
70 | 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.
71 |
72 | ### Android studio
73 |
74 | Precondition: make sure that you have [**NDK**][2] installed and you either have this line in your `local.properties`
75 |
76 | `ndk.dir=/path/to/ndk`
77 |
78 | or you have `ANDROID_NDK_HOME` environment variable set.
79 |
80 | #### Getting started
81 |
82 | Just import the whole cloned project and run the sample.
83 |
84 | #### Further configuring and using as the library
85 |
86 | #### Option 1
87 | 1. Add this repository as a git submodule. For these instructions we added in a folder named `AndroidJniBitmapOperations`
88 | 2. Add the following lines to your `settings.gradle` file
89 |
90 | ```
91 | include ':JniBitmapOperationsLibrary'
92 | project(':JniBitmapOperationsLibrary').projectDir = new File(rootProject.getProjectDir(), 'AndroidJniBitmapOperations/JniBitmapOperationsLibrary')
93 | ```
94 | 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.
95 |
96 | ```
97 | // Variables for JniBitmapOperationsLibrary
98 | ext.propCompileSdkVersion = 23
99 | ext.propBuildToolsVersion = "27.0.3"
100 | ```
101 | 4. Add the following lines to your app `build.gradle` file inside the `dependancies` section
102 |
103 | ```
104 | implementation project(':JniBitmapOperationsLibrary')
105 | ```
106 |
107 | #### Option 2
108 |
109 | 1. Copy `JniBitmapOperationsLibrary.cpp` into `src/main/jni` directory:
110 |
111 | 
112 | 2. Add this minimum NDK config to your `build.gradle`
113 |
114 | ```
115 | android {
116 | ...
117 | defaultConfig {
118 | ...
119 | ndk {
120 | moduleName "JniBitmapOperationsLibrary"
121 | ldLibs "log", "jnigraphics"
122 | //optional: filter abis to compile for: abiFilters "x86", "armeabi-v7a"
123 | //otherwise it will compile for all abis: "armeabi", "armeabi-v7a", "x86", and "mips"
124 | }
125 | }
126 | }
127 | ```
128 |
129 | 3. Copy `JniBitmapHolder` into the project, putting it into the same package (`com.jni.bitmap_operations`).
130 |
131 | You now should be able to use `JniBitmapHolder` to process images on NDK side.
132 |
133 | ## Similar libraries
134 |
135 | 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:
136 |
137 | - https://github.com/suckgamony/RapidDecoder
138 | - https://github.com/facebook/fresco
139 | - https://android-arsenal.com/tag/63
140 |
141 |
142 | [1]: http://stackoverflow.com/questions/22263253/how-to-correctly-import-an-android-library-with-jni-code/22956790?noredirect=1#comment35057887_22956790
143 |
144 | [2]: https://developer.android.com/tools/sdk/ndk/index.html
145 |
146 | [3]: http://www.powershow.com/view/29fcd-NjRmN/Fast_matrix_multiplication_Cache_usage_powerpoint_ppt_presentation
147 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | //task wrapper(type: Wrapper) {
4 | // gradleVersion = '2.2'
5 | //}
6 |
7 | buildscript {
8 | repositories {
9 | mavenCentral()
10 | google()
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:3.2.0'
14 |
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 |
20 | allprojects {
21 | repositories {
22 | mavenCentral()
23 | }
24 | }
25 |
26 | ext {
27 | propBuildToolsVersion = '23.0.2'
28 | propCompileSdkVersion = 23
29 | }
30 |
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/demo.gif
--------------------------------------------------------------------------------
/extra/BilinearInterpolation.java:
--------------------------------------------------------------------------------
1 | package com.example.jnibitmapoperationstest;
2 | import android.graphics.Bitmap;
3 | import android.graphics.Color;
4 |
5 | /** class for resizing imageData using the Bilinear Interpolation method */
6 | public class BilinearInterpolation
7 | {
8 | /** the method for resizing the imageData using the Bilinear Interpolation algorithm */
9 | public static void resize(final Bitmap input,final Bitmap output)
10 | {
11 | final int oldHeight=input.getHeight(),oldWidth=input.getWidth();
12 | final int newHeight=output.getHeight(),newWidth=output.getWidth();
13 | // position of the top left pixel of the 4 pixels to use interpolation on
14 | int xTopLeft,yTopLeft;
15 | int x,y,lastTopLefty;
16 | final float xRatio=(float)newWidth/(float)oldWidth,yratio=(float)newHeight/(float)oldHeight;
17 | // Y color ratio to use on left and right pixels for interpolation
18 | float ycRatio2=0,ycRatio1=0;
19 | // pixel target in the src
20 | float xt,yt;
21 | // X color ratio to use on left and right pixels for interpolation
22 | float xcRatio2=0,xcratio1=0;
23 | int rgbTopLeft=0,rgbTopRight=0,rgbBottomLeft=0,rgbBottomRight=0,rgbTopMiddle=0,rgbBottomMiddle=0;
24 | // do the resizing:
25 | for(x=0;x=oldWidth-1)
30 | xTopLeft--;
31 | if(xt<=xTopLeft+1)
32 | {
33 | // we are between the left and right pixel
34 | xcratio1=xt-xTopLeft;
35 | // color ratio in favor of the right pixel color
36 | xcRatio2=1-xcratio1;
37 | }
38 | for(y=0,lastTopLefty=Integer.MIN_VALUE;y=oldHeight-1)
43 | yTopLeft--;
44 | // we went down only one rectangle
45 | if(lastTopLefty==yTopLeft-1)
46 | {
47 | rgbTopLeft=rgbBottomLeft;
48 | rgbTopRight=rgbBottomRight;
49 | rgbTopMiddle=rgbBottomMiddle;
50 | rgbBottomLeft=input.getPixel(xTopLeft,yTopLeft+1);
51 | rgbBottomRight=input.getPixel(xTopLeft+1,yTopLeft+1);
52 | rgbBottomMiddle=Color.argb((int)(Color.alpha(rgbBottomLeft)*xcRatio2+Color.alpha(rgbBottomRight)*xcratio1),//
53 | (int)(Color.red(rgbBottomLeft)*xcRatio2+Color.red(rgbBottomRight)*xcratio1),//
54 | (int)(Color.green(rgbBottomLeft)*xcRatio2+Color.green(rgbBottomRight)*xcratio1),//
55 | (int)(Color.blue(rgbBottomLeft)*xcRatio2+Color.blue(rgbBottomRight)*xcratio1));
56 | }
57 | else if(lastTopLefty!=yTopLeft)
58 | {
59 | // we went to a totally different rectangle (happens in every loop start,and might happen more when making the picture smaller)
60 | rgbTopLeft=input.getPixel(xTopLeft,yTopLeft);
61 | rgbTopRight=input.getPixel(xTopLeft+1,yTopLeft);
62 | rgbTopMiddle=Color.argb((int)(Color.alpha(rgbTopLeft)*xcRatio2+Color.alpha(rgbTopRight)*xcratio1),//
63 | (int)(Color.red(rgbTopLeft)*xcRatio2+Color.red(rgbTopRight)*xcratio1),//
64 | (int)(Color.green(rgbTopLeft)*xcRatio2+Color.green(rgbTopRight)*xcratio1),//
65 | (int)(Color.blue(rgbTopLeft)*xcRatio2+Color.blue(rgbTopRight)*xcratio1));
66 | rgbBottomLeft=input.getPixel(xTopLeft,yTopLeft+1);
67 | rgbBottomRight=input.getPixel(xTopLeft+1,yTopLeft+1);
68 | rgbBottomMiddle=Color.argb((int)(Color.alpha(rgbBottomLeft)*xcRatio2+Color.alpha(rgbBottomRight)*xcratio1),//
69 | (int)(Color.red(rgbBottomLeft)*xcRatio2+Color.red(rgbBottomRight)*xcratio1),//
70 | (int)(Color.green(rgbBottomLeft)*xcRatio2+Color.green(rgbBottomRight)*xcratio1),//
71 | (int)(Color.blue(rgbBottomLeft)*xcRatio2+Color.blue(rgbBottomRight)*xcratio1));
72 | }
73 | lastTopLefty=yTopLeft;
74 | if(yt<=yTopLeft+1)
75 | {
76 | // color ratio in favor of the bottom pixel color
77 | ycRatio1=yt-yTopLeft;
78 | ycRatio2=1-ycRatio1;
79 | }
80 | // prepared all pixels to look at, so finally set the new pixel data
81 | output.setPixel(x,y,Color.argb(//
82 | (int)(Color.alpha(rgbTopMiddle)*ycRatio2+Color.alpha(rgbBottomMiddle)*ycRatio1),//
83 | (int)(Color.red(rgbTopMiddle)*ycRatio2+Color.red(rgbBottomMiddle)*ycRatio1),//
84 | (int)(Color.green(rgbTopMiddle)*ycRatio2+Color.green(rgbBottomMiddle)*ycRatio1),//
85 | (int)(Color.blue(rgbTopMiddle)*ycRatio2+Color.blue(rgbBottomRight)*ycRatio1)));
86 | }
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/extra/old original university code for resizing images/BilinearInterpolation.java:
--------------------------------------------------------------------------------
1 | package ex1;
2 | import org.eclipse.swt.graphics.ImageData;
3 | import org.eclipse.swt.graphics.RGB;
4 |
5 | /** class for resizing imageData using the Bilinear Interpolation method */
6 | public class BilinearInterpolation
7 | {
8 | static ImageData lastImageData =null; // the last imageData that we used. changes only when we handle a new image, so it stays the same as long as we handle the same image
9 | static RGB[][] startingImageData;
10 |
11 | /** the method for resizing the imageData using the Bilinear Interpolation algorithm */
12 | public static void resize(ImageData inputImageData,ImageData newImageData,int oldWidth,int oldHeight,int newWidth,int newHeight)
13 | {
14 | boolean gotNewImage=inputImageData!=lastImageData;
15 | lastImageData=inputImageData;
16 | int xTopLeft,yTopLeft; // position of the top left pixel of the 4 pixels to use interpolation on
17 | int x,y,lastTopLefty;
18 | float xRatio=(float)newWidth/(float)oldWidth,yratio=(float)newHeight/(float)oldHeight;
19 | float ycRatio2=0,ycRatio1=0; // Y color ratio to use on left and right pixels for interpolation
20 | float xt,yt; // pixel target in the src
21 | float xcRatio2=0,xcratio1=0; // X color ratio to use on left and right pixels for interpolation
22 | RGB rgbTopLeft,rgbTopRight,rgbBottomLeft=null,rgbBottomRight=null,rgbTopMiddle=null,rgbBottomMiddle=null;
23 | if(gotNewImage)
24 | {
25 | startingImageData=new RGB[oldWidth][oldHeight];
26 | for(x=0;x=oldWidth-1) xTopLeft--;// when meeting the most right edge, move left a little
37 | if(xt<=xTopLeft+1)// we are between the left and right pixel
38 | {
39 | xcratio1=xt-xTopLeft;// color ratio in favor of the right pixel color
40 | xcRatio2=1-xcratio1;
41 | }
42 | for(y=0,lastTopLefty=Integer.MIN_VALUE;y=oldHeight-1) yTopLeft--;// when meeting the most bottom edge, move up a little
46 | if(lastTopLefty==yTopLeft-1)// we went down only one rectangle
47 | {
48 | rgbTopLeft=rgbBottomLeft;
49 | rgbTopRight=rgbBottomRight;
50 | rgbTopMiddle=rgbBottomMiddle;
51 | rgbBottomLeft=startingImageData[xTopLeft][yTopLeft+1];
52 | rgbBottomRight=startingImageData[xTopLeft+1][yTopLeft+1];
53 | rgbBottomMiddle=new RGB((int)(rgbBottomLeft.red*xcRatio2+rgbBottomRight.red*xcratio1),(int)(rgbBottomLeft.green*xcRatio2+rgbBottomRight.green*xcratio1),(int)(rgbBottomLeft.blue*xcRatio2+rgbBottomRight.blue*xcratio1));
54 | }
55 | else if(lastTopLefty!=yTopLeft)
56 | { // we went to a totally different rectangle (happens in every loop start,and might happen more when making the picture smaller)
57 | rgbTopLeft=startingImageData[xTopLeft][yTopLeft];
58 | rgbTopRight=startingImageData[xTopLeft+1][yTopLeft];
59 | rgbTopMiddle=new RGB((int)(rgbTopLeft.red*xcRatio2+rgbTopRight.red*xcratio1),(int)(rgbTopLeft.green*xcRatio2+rgbTopRight.green*xcratio1),(int)(rgbTopLeft.blue*xcRatio2+rgbTopRight.blue*xcratio1));
60 | rgbBottomLeft=startingImageData[xTopLeft][yTopLeft+1];
61 | rgbBottomRight=startingImageData[xTopLeft+1][yTopLeft+1];
62 | rgbBottomMiddle=new RGB((int)(rgbBottomLeft.red*xcRatio2+rgbBottomRight.red*xcratio1),(int)(rgbBottomLeft.green*xcRatio2+rgbBottomRight.green*xcratio1),(int)(rgbBottomLeft.blue*xcRatio2+rgbBottomRight.blue*xcratio1));
63 | }
64 | lastTopLefty=yTopLeft;
65 | if(yt<=yTopLeft+1)
66 | {
67 | ycRatio1=yt-yTopLeft;// color ratio in favor of the bottom pixel color
68 | ycRatio2=1-ycRatio1;
69 | }
70 | newImageData.setPixel(x,y,inputImageData.palette.getPixel(new RGB((int)(rgbTopMiddle.red*ycRatio2+rgbBottomMiddle.red*ycRatio1),(int)(rgbTopMiddle.green*ycRatio2+rgbBottomMiddle.green*ycRatio1),(int)(rgbTopMiddle.blue*ycRatio2+rgbBottomMiddle.blue*ycRatio1))));
71 | }
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/extra/old original university code for resizing images/NearestNeighbor.java:
--------------------------------------------------------------------------------
1 | package ex1;
2 | import org.eclipse.swt.graphics.ImageData;
3 |
4 | public class NearestNeighbor
5 | {
6 | public static void resize(ImageData originalImageData,ImageData newImageData,int oldwidth,int oldheight,int newWidth,int newHeight)
7 | {
8 | float x,y,xratio=(float)newWidth/(float)oldwidth,yratio=(float)newHeight/(float)oldheight;
9 | for(x=0.0f;x \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/sample/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/sample/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | JniBitmapOperationsTest
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/sample/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion propCompileSdkVersion
5 | buildToolsVersion propBuildToolsVersion
6 |
7 | sourceSets {
8 | main {
9 | manifest.srcFile 'AndroidManifest.xml'
10 | java.srcDirs = ['src']
11 | resources.srcDirs = ['src']
12 | aidl.srcDirs = ['src']
13 | renderscript.srcDirs = ['src']
14 | res.srcDirs = ['res']
15 | assets.srcDirs = ['assets']
16 | }
17 | }
18 |
19 | dependencies {
20 | compile project(':JniBitmapOperationsLibrary')
21 | }
22 | }
--------------------------------------------------------------------------------
/sample/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/ic_launcher-web.png
--------------------------------------------------------------------------------
/sample/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/sample/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/sample/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 | android.library.reference.1=../JniBitmapOperationsLibrary
16 |
--------------------------------------------------------------------------------
/sample/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable-xhdpi/test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/res/drawable-xhdpi/test.png
--------------------------------------------------------------------------------
/sample/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidDeveloperLB/AndroidJniBitmapOperations/c6b7109dd963cba0e67ed071a2408b92ecb6a03a/sample/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
12 |
13 |
14 |
15 |
20 |
21 |
26 |
27 |
28 |
29 |
34 |
35 |
40 |
41 |
42 |
43 |
48 |
49 |
54 |
55 |
56 |
57 |
62 |
63 |
68 |
69 |
70 |
71 |
76 |
77 |
82 |
83 |
84 |
85 |
90 |
91 |
96 |
97 |
98 |
99 |
104 |
105 |
110 |
111 |
112 |
113 |
118 |
119 |
124 |
125 |
126 |
127 |
132 |
133 |
138 |
139 |
140 |
--------------------------------------------------------------------------------
/sample/res/menu/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
21 |
--------------------------------------------------------------------------------
/sample/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/sample/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
10 |
--------------------------------------------------------------------------------
/sample/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/sample/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/sample/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | JniBitmapOperationsTest
5 | Settings
6 | Repository website
7 | All my repositories
8 | All my apps
9 | More info
10 |
11 |
--------------------------------------------------------------------------------
/sample/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/sample/src/com/example/jnibitmapoperationstest/BilinearInterpolation.java:
--------------------------------------------------------------------------------
1 | package com.example.jnibitmapoperationstest;
2 | import android.graphics.Bitmap;
3 | import android.graphics.Color;
4 |
5 | /**
6 | * class for resizing imageData using the Bilinear Interpolation method . Should
7 | * work fine for Android. needs some work with Alphe handling:
8 | * http://stackoverflow.com/questions/23231252/implementing-bilinear-interpolation-on-android-with-support-to-alpha-values
9 | */
10 | public class BilinearInterpolation
11 | {
12 | /** the method for resizing the imageData using the Bilinear Interpolation algorithm */
13 | public static void resize(final Bitmap input,final Bitmap output)
14 | {
15 | final int oldHeight=input.getHeight(),oldWidth=input.getWidth();
16 | final int newHeight=output.getHeight(),newWidth=output.getWidth();
17 | // position of the top left pixel of the 4 pixels to use interpolation on
18 | int xTopLeft,yTopLeft;
19 | int x,y,lastTopLefty;
20 | final float xRatio=(float)newWidth/(float)oldWidth,yratio=(float)newHeight/(float)oldHeight;
21 | // Y color ratio to use on left and right pixels for interpolation
22 | float ycRatio2=0,ycRatio1=0;
23 | // pixel target in the src
24 | float xt,yt;
25 | // X color ratio to use on left and right pixels for interpolation
26 | float xcRatio2=0,xcratio1=0;
27 | int rgbTopLeft=0,rgbTopRight=0,rgbBottomLeft=0,rgbBottomRight=0,rgbTopMiddle=0,rgbBottomMiddle=0;
28 | // do the resizing:
29 | for(x=0;x=oldWidth-1)
34 | xTopLeft--;
35 | if(xt<=xTopLeft+1)
36 | {
37 | // we are between the left and right pixel
38 | xcratio1=xt-xTopLeft;
39 | // color ratio in favor of the right pixel color
40 | xcRatio2=1-xcratio1;
41 | }
42 | for(y=0,lastTopLefty=Integer.MIN_VALUE;y=oldHeight-1)
47 | yTopLeft--;
48 | // we went down only one rectangle
49 | if(lastTopLefty==yTopLeft-1)
50 | {
51 | rgbTopLeft=rgbBottomLeft;
52 | rgbTopRight=rgbBottomRight;
53 | rgbTopMiddle=rgbBottomMiddle;
54 | rgbBottomLeft=input.getPixel(xTopLeft,yTopLeft+1);
55 | rgbBottomRight=input.getPixel(xTopLeft+1,yTopLeft+1);
56 | rgbBottomMiddle=Color.argb((int)(Color.alpha(rgbBottomLeft)*xcRatio2+Color.alpha(rgbBottomRight)*xcratio1),//
57 | (int)(Color.red(rgbBottomLeft)*xcRatio2+Color.red(rgbBottomRight)*xcratio1),//
58 | (int)(Color.green(rgbBottomLeft)*xcRatio2+Color.green(rgbBottomRight)*xcratio1),//
59 | (int)(Color.blue(rgbBottomLeft)*xcRatio2+Color.blue(rgbBottomRight)*xcratio1));
60 | }
61 | else if(lastTopLefty!=yTopLeft)
62 | {
63 | // we went to a totally different rectangle (happens in every loop start,and might happen more when making the picture smaller)
64 | rgbTopLeft=input.getPixel(xTopLeft,yTopLeft);
65 | rgbTopRight=input.getPixel(xTopLeft+1,yTopLeft);
66 | rgbTopMiddle=Color.argb((int)(Color.alpha(rgbTopLeft)*xcRatio2+Color.alpha(rgbTopRight)*xcratio1),//
67 | (int)(Color.red(rgbTopLeft)*xcRatio2+Color.red(rgbTopRight)*xcratio1),//
68 | (int)(Color.green(rgbTopLeft)*xcRatio2+Color.green(rgbTopRight)*xcratio1),//
69 | (int)(Color.blue(rgbTopLeft)*xcRatio2+Color.blue(rgbTopRight)*xcratio1));
70 | rgbBottomLeft=input.getPixel(xTopLeft,yTopLeft+1);
71 | rgbBottomRight=input.getPixel(xTopLeft+1,yTopLeft+1);
72 | rgbBottomMiddle=Color.argb((int)(Color.alpha(rgbBottomLeft)*xcRatio2+Color.alpha(rgbBottomRight)*xcratio1),//
73 | (int)(Color.red(rgbBottomLeft)*xcRatio2+Color.red(rgbBottomRight)*xcratio1),//
74 | (int)(Color.green(rgbBottomLeft)*xcRatio2+Color.green(rgbBottomRight)*xcratio1),//
75 | (int)(Color.blue(rgbBottomLeft)*xcRatio2+Color.blue(rgbBottomRight)*xcratio1));
76 | }
77 | lastTopLefty=yTopLeft;
78 | if(yt<=yTopLeft+1)
79 | {
80 | // color ratio in favor of the bottom pixel color
81 | ycRatio1=yt-yTopLeft;
82 | ycRatio2=1-ycRatio1;
83 | }
84 | // prepared all pixels to look at, so finally set the new pixel data
85 | output.setPixel(x,y,Color.argb(//
86 | (int)(Color.alpha(rgbTopMiddle)*ycRatio2+Color.alpha(rgbBottomMiddle)*ycRatio1),//
87 | (int)(Color.red(rgbTopMiddle)*ycRatio2+Color.red(rgbBottomMiddle)*ycRatio1),//
88 | (int)(Color.green(rgbTopMiddle)*ycRatio2+Color.green(rgbBottomMiddle)*ycRatio1),//
89 | (int)(Color.blue(rgbTopMiddle)*ycRatio2+Color.blue(rgbBottomRight)*ycRatio1)));
90 | }
91 | }
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/sample/src/com/example/jnibitmapoperationstest/MainActivity.java:
--------------------------------------------------------------------------------
1 |
2 | package com.example.jnibitmapoperationstest;
3 |
4 | import android.annotation.TargetApi;
5 | import android.app.Activity;
6 | import android.content.Intent;
7 | import android.graphics.Bitmap;
8 | import android.graphics.BitmapFactory;
9 | import android.net.Uri;
10 | import android.os.Build;
11 | import android.os.Bundle;
12 | import android.view.Menu;
13 | import android.view.MenuItem;
14 | import android.widget.ImageView;
15 | import com.jni.bitmap_operations.JniBitmapHolder;
16 | import com.jni.bitmap_operations.JniBitmapHolder.ScaleMethod;
17 |
18 | /** just a demo activity to show some of the features of the library */
19 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
20 | public class MainActivity extends Activity
21 | {
22 | private static final int IMAGE_RESID_TO_TEST = R.drawable.test;
23 | JniBitmapHolder bitmapHolder = new JniBitmapHolder();
24 |
25 | @Override
26 | protected void onCreate(final Bundle savedInstanceState)
27 | {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_main);
30 | //
31 | // original
32 | //
33 | final ImageView imageViewOriginal = (ImageView)findViewById(R.id.imageViewOriginal);
34 | final Bitmap b = BitmapFactory.decodeResource(getResources(), IMAGE_RESID_TO_TEST);
35 | imageViewOriginal.setImageBitmap(b);
36 | //
37 | // rotated 90 degrees CCW
38 | //
39 | final ImageView imageViewRotated90degreesCcw = (ImageView)findViewById(R.id.imageViewRotated90degreesCcw);
40 | bitmapHolder.storeBitmap(b);
41 | bitmapHolder.rotateBitmapCcw90();
42 | imageViewRotated90degreesCcw.setImageBitmap(bitmapHolder.getBitmapAndFree());
43 | //
44 | // rotated 90 degrees CW
45 | //
46 | final ImageView imageViewRotated90degreesCw = (ImageView)findViewById(R.id.imageViewRotated90degreesCw);
47 | bitmapHolder.storeBitmap(b);
48 | bitmapHolder.rotateBitmapCw90();
49 | imageViewRotated90degreesCw.setImageBitmap(bitmapHolder.getBitmapAndFree());
50 | //
51 | // rotate 180
52 | //
53 | final ImageView imageViewRotated180degreesCw = (ImageView)findViewById(R.id.imageViewRotated180degrees);
54 | bitmapHolder.storeBitmap(b);
55 | bitmapHolder.rotateBitmap180();
56 | ;
57 | imageViewRotated180degreesCw.setImageBitmap(bitmapHolder.getBitmapAndFree());
58 | //
59 | // cropped
60 | //
61 | final ImageView imageViewCropped = (ImageView)findViewById(R.id.imageViewCropped);
62 | bitmapHolder.storeBitmap(b);
63 | bitmapHolder.cropBitmap(b.getWidth() / 4, b.getHeight() / 4, b.getWidth() * 3 / 4,
64 | b.getHeight() * 3 / 4);
65 | imageViewCropped.setImageBitmap(bitmapHolder.getBitmapAndFree());
66 | //
67 | // scaled using nearest neighbor algorithm (which is fast, simple, yet
68 | // it sometimes has aliases problems)
69 | //
70 | final ImageView imageViewScaledUsingNearestNeighbour = (ImageView)findViewById(R.id.imageViewScaledUsingNearestNeighbour);
71 | bitmapHolder.storeBitmap(b);
72 | bitmapHolder.scaleBitmap(b.getWidth() * 2, b.getHeight() * 2, ScaleMethod.NearestNeighbour);
73 | final Bitmap scaledBitmapNN = bitmapHolder.getBitmapAndFree();
74 | imageViewScaledUsingNearestNeighbour.setImageBitmap(scaledBitmapNN);
75 | //
76 | // scaled using nearest neighbor algorithm (which is relatively high
77 | // quality resizing and it handles aliases nicely)
78 | //
79 | final ImageView imageViewScaledUsingBilinearInterpolation = (ImageView)findViewById(R.id.imageViewScaledUsingBilinearInterpolation);
80 | bitmapHolder.storeBitmap(b);
81 | bitmapHolder.scaleBitmap(b.getWidth() * 2, b.getHeight() * 2,
82 | ScaleMethod.BilinearInterpolation);
83 | final Bitmap scaledBitmapBI = bitmapHolder.getBitmapAndFree();
84 | imageViewScaledUsingBilinearInterpolation.setImageBitmap(scaledBitmapBI);
85 | //
86 | // flipped on the vertical
87 | //
88 | final ImageView imageViewFlippedVertical = (ImageView)findViewById(R.id.imageViewFlippedVertical);
89 | bitmapHolder.storeBitmap(b);
90 | bitmapHolder.flipBitmapVertical();
91 | imageViewFlippedVertical.setImageBitmap(bitmapHolder.getBitmapAndFree());
92 | //
93 | // rotated 90 degrees CCW
94 | //
95 | final ImageView imageViewFlippedHorizontal = (ImageView)findViewById(R.id.imageViewFlippedHorizontal);
96 | bitmapHolder.storeBitmap(b);
97 | bitmapHolder.flipBitmapHorizontal();
98 | imageViewFlippedHorizontal.setImageBitmap(bitmapHolder.getBitmapAndFree());
99 | }
100 |
101 | @Override
102 | public boolean onCreateOptionsMenu(final Menu menu)
103 | {
104 | getMenuInflater().inflate(R.menu.activity_main, menu);
105 | return super.onCreateOptionsMenu(menu);
106 | }
107 |
108 | @Override
109 | public boolean onOptionsItemSelected(final MenuItem item)
110 | {
111 | String url = null;
112 | switch (item.getItemId())
113 | {
114 | case R.id.menuItem_all_my_apps:
115 | url = "https://play.google.com/store/apps/developer?id=AndroidDeveloperLB";
116 | break;
117 | case R.id.menuItem_all_my_repositories:
118 | url = "https://github.com/AndroidDeveloperLB";
119 | break;
120 | case R.id.menuItem_current_repository_website:
121 | url = "https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations";
122 | break;
123 | }
124 | if (url == null)
125 | return true;
126 | final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
127 | intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
128 | | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
129 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
130 | startActivity(intent);
131 | return true;
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':JniBitmapOperationsLibrary', ':sample'
--------------------------------------------------------------------------------