SysFont.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <jni.h>
  2. #include <android/log.h>
  3. #include <android/bitmap.h>
  4. #define LOG_TAG "lib_morefun"
  5. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  6. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
  7. typedef struct
  8. {
  9. uint8_t alpha;
  10. uint8_t red;
  11. uint8_t green;
  12. uint8_t blue;
  13. } argb;
  14. JNIEXPORT void JNICALL Java_com_morefun_SysFont_convertToRGBA(JNIEnv* env, jclass clazz, jobject buffer, jint length)
  15. {
  16. void* pixels = env->GetDirectBufferAddress(buffer);
  17. unsigned char * p_argb = (unsigned char *)pixels;
  18. unsigned char a;
  19. for (int i = 0; i < length; i+=4)
  20. {
  21. a = p_argb[i];
  22. p_argb[i ] = p_argb[i+1];
  23. p_argb[i+1] = p_argb[i+2];
  24. p_argb[i+2] = p_argb[i+3];
  25. p_argb[i+3] = a;
  26. }
  27. }
  28. JNIEXPORT void JNICALL Java_com_morefun_SysFont_copyPixelsToBufferRGBA(JNIEnv* env, jclass clazz, jobject bitmap, jobject buffer)
  29. {
  30. AndroidBitmapInfo info_bitmap;
  31. int ret;
  32. void* pixelscolor;
  33. if ((ret = AndroidBitmap_getInfo(env, bitmap, &info_bitmap)) < 0) {
  34. LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
  35. return;
  36. }
  37. if (info_bitmap.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
  38. LOGE("Bitmap format is not RGBA_8888 !");
  39. return;
  40. }
  41. if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixelscolor)) < 0) {
  42. LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
  43. }
  44. {
  45. for (int y=0; y<info_bitmap.height; y++) {
  46. argb * line = (argb *) pixelscolor;
  47. for (int x=0; x<info_bitmap.width; x++) {
  48. //grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;
  49. }
  50. pixelscolor = (char *)pixelscolor + info_bitmap.stride;
  51. }
  52. }
  53. AndroidBitmap_unlockPixels(env, bitmap);
  54. }