dlib-android icon indicating copy to clipboard operation
dlib-android copied to clipboard

Question: Is there any way that JNI take bitmap or byte array of the bitmap and convert to dlib::array2d without openCV?

Open tcw165 opened this issue 8 years ago • 3 comments
trafficstars

Hi, Is there any way that JNI take a bitmap or the byte array of the bitmap and convert it to dlib::array2d without openCV?

tcw165 avatar Apr 23 '17 03:04 tcw165

Hello,

Same here. I don't know how to convert an Android Bitmap to a dlib::array2d<dlib::bgr_pixel> . Does anyone has a clue ?

BrunoAndroidDev avatar Sep 12 '18 09:09 BrunoAndroidDev

Same here, did you guys had any success?

ManuelTS avatar Mar 01 '19 14:03 ManuelTS

Hi @ManuelTS . I've done the following :

#include "jni.h"
#include "android/log.h"
#include "android/bitmap.h"

void convertBitmapToArray2d(JNIEnv *env,
                            jobject bitmap,
                            dlib::array2d<dlib::bgr_pixel> &out) {
    AndroidBitmapInfo bitmapInfo;
    void *pixels;
    int state;

    if (0 > (state = AndroidBitmap_getInfo(env, bitmap, &bitmapInfo))) {
        LOGE("AndroidBitmap_getInfo() failed! error=%d", state);
        return;
    } else if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        LOGE("Bitmap format is not RGB_565!");
    }

    // Lock the bitmap for copying the pixels safely.
    if (0 > (state = AndroidBitmap_lockPixels(env, bitmap, &pixels))) {
        LOGE("AndroidBitmap_lockPixels() failed! error=%d", state);
        return;
    }

    LOGI("info.width=%d, info.height=%d", bitmapInfo.width, bitmapInfo.height);
    out.set_size((long) bitmapInfo.height, (long) bitmapInfo.width);

    char *line = (char *) pixels;
    for (int h = 0; h < bitmapInfo.height; ++h) {
        for (int w = 0; w < bitmapInfo.width; ++w) {
            uint32_t *color = (uint32_t *) (line + 4 * w);

            out[h][w].red = (unsigned char) (0xFF & (*color));
            out[h][w].green = (unsigned char) (0xFF & ((*color) >> 8));
            out[h][w].blue = (unsigned char) (0xFF & ((*color) >> 16));
        }

        line = line + bitmapInfo.stride;
    }

    // Unlock the bitmap.
    AndroidBitmap_unlockPixels(env, bitmap);
}

I hope it will help you.

BrunoAndroidDev avatar Mar 01 '19 16:03 BrunoAndroidDev