[Android] accesses and operates bitmap elements via JNI, supporting RGB565 and ARGB8888

Source: Internet
Author: User

 [Android] accesses and operates bitmap elements via JNI, supporting RGB565 and ARGB8888Tags: androidbitmapjni2014-05-09 20:35 2985 People read Comments (1) favorite reports

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A simple JNI example, the input is bitmap (need to be mutable), the result is to turn bitmap into a grayscale image.

In order to look a bit of value, so at the same time support RGB565 and ARGB8888 (囧RZ)

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <jni.h>
  6. #include <android/bitmap.h>
  7. #include <android/log.h>
  8. #ifndef eprintf
  9. #define EPRINTF (...) __android_log_print (Android_log_error, "@", __va_args__)
  10. #endif
  11. #define RGB565_R (P) ((((p) & 0xf800) >> one) << 3)
  12. #define RGB565_G (P) ((((p) & 0X7E0) >> 5) << 2)
  13. #define RGB565_B (P) (((p) & 0x1F) << 3)
  14. #define MAKE_RGB565 (R,g,b) (((R) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
  15. #define RGBA_A (P) (((p) & 0xff000000) >> 24)
  16. #define RGBA_R (P) (((p) & 0x00ff0000) >> 16)
  17. #define RGBA_G (P) (((p) & 0x0000FF00) >> 8)
  18. #define RGBA_B (P) ((p) & 0X000000FF)
  19. #define MAKE_RGBA (R,g,b,a) (((a) << 24) | (r) << 16) | ((g) << 8) | (b))
  20. Jniexport void Jnicall java_com_yxcorp_hello_effect_update
  21. (JNIEnv *env, Jclass clazz, Jobject zbitmap) {
  22. JNIEnv J = *env;
  23. if (Zbitmap = = NULL) {
  24. eprintf ("bitmap is null\n");
  25. return;
  26. }
  27. //Get bitmap Info
  28. Androidbitmapinfo info;
  29. memset (&info, 0, sizeof (info));
  30. Androidbitmap_getinfo (env, Zbitmap, &info);
  31. //Check format, only RGB565 & RGBA is supported
  32. if (info.width <= 0 | | info.height <= 0 | |
  33. (Info.format! = android_bitmap_format_rgb_565 && Info.format! = android_bitmap_format_rgba_8888)) {
  34. eprintf ("Invalid bitmap\n");
  35. J->thrownew (env, J->findclass (env, "Java/io/ioexception"), "Invalid Bitmap");
  36. return;
  37. }
  38. //Lock the bitmap to get the buffer
  39. void * pixels = NULL;
  40. int res = androidbitmap_lockpixels (env, Zbitmap, &pixels);
  41. if (pixels = = NULL) {
  42. eprintf ("fail to lock Bitmap:%d\n", res);
  43. J->thrownew (env, J->findclass (env, "Java/io/ioexception"), "fail to open bitmap");
  44. return;
  45. }
  46. eprintf ("Effect:%dx%d,%d\n", Info.width, Info.height, Info.format);
  47. int x = 0, y = 0;
  48. //From top to bottom
  49. For (y = 0; y < info.height; ++y) {
  50. // from left to right
  51. For (x = 0; x < info.width; ++x) {
  52. int a = 0, r = 0, g = 0, b = 0;
  53. void *pixel = NULL;
  54. //Get each pixel by format
  55. if (Info.format = = android_bitmap_format_rgb_565) {
  56. Pixel = ((uint16_t *) pixels) + y * info.width + x;
  57. uint16_t v = * (uint16_t *) pixel;
  58. R = Rgb565_r (v);
  59. g = Rgb565_g (v);
  60. b = Rgb565_b (v);
  61. } else {//RGBA
  62. Pixel = ((uint32_t *) pixels) + y * info.width + x;
  63. uint32_t v = * (uint32_t *) pixel;
  64. A = Rgba_a (v);
  65. R = Rgba_r (v);
  66. g = Rgba_g (v);
  67. b = Rgba_b (v);
  68. }
  69. //Grayscale
  70. int Gray = (R * + G * + + b *) >> 7;
  71. //Write the pixel back
  72. if (Info.format = = android_bitmap_format_rgb_565) {
  73. * ((uint16_t *) pixel) = make_rgb565 (Gray, Gray, gray);
  74. } else {//RGBA
  75. * ((uint32_t *) pixel) = Make_rgba (Gray, Gray, Gray, a);
  76. }
  77. }
  78. }
  79. Androidbitmap_unlockpixels (env, ZBITMAP);
  80. }

[Android] accesses and operates bitmap elements via JNI, supporting RGB565 and ARGB8888

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.