找不到本机方法:rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap

时间:2016-01-29 06:21:12

标签: android android-studio java-native-interface

image.c

JNIEXPORT void Java_rabta_pk_chatthahehehe_AndroidUtilities_loadBitmap(JNIEnv *env, jclass class, jstring path, jobject bitmap, int scale, int width, int height, int stride) {

    AndroidBitmapInfo info;
    int i;

    if ((i = AndroidBitmap_getInfo(env, bitmap, &info)) >= 0) {
        char *fileName = (*env)->GetStringUTFChars(env, path, NULL);
        FILE *infile;

        if ((infile = fopen(fileName, "rb"))) {
            struct my_error_mgr jerr;
            struct jpeg_decompress_struct cinfo;

            cinfo.err = jpeg_std_error(&jerr.pub);
            jerr.pub.error_exit = my_error_exit;

            if (!setjmp(jerr.setjmp_buffer)) {
                jpeg_create_decompress(&cinfo);
                jpeg_stdio_src(&cinfo, infile);

                jpeg_read_header(&cinfo, TRUE);

                cinfo.scale_denom = scale;
                cinfo.scale_num = 1;

                jpeg_start_decompress(&cinfo);
                int row_stride = cinfo.output_width * cinfo.output_components;
                JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) & cinfo, JPOOL_IMAGE,
                                                               row_stride, 1);

                unsigned char *pixels;
                if ((i = AndroidBitmap_lockPixels(env, bitmap, &pixels)) >= 0) {
                    int rowCount = min(cinfo.output_height, height);
                    int colCount = min(cinfo.output_width, width);

                    while (cinfo.output_scanline < rowCount) {
                        jpeg_read_scanlines(&cinfo, buffer, 1);

                        //if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
                        if (cinfo.out_color_space == JCS_GRAYSCALE) {
                            for (i = 0; i < colCount; i++) {
                                float alpha = buffer[0][i] / 255.0f;
                                pixels[i * 4] *= alpha;
                                pixels[i * 4 + 1] *= alpha;
                                pixels[i * 4 + 2] *= alpha;
                                pixels[i * 4 + 3] = buffer[0][i];
                            }
                        } else {
                            int c = 0;
                            for (i = 0; i < colCount; i++) {
                                pixels[i * 4] = buffer[0][i * 3];
                                pixels[i * 4 + 1] = buffer[0][i * 3 + 1];
                                pixels[i * 4 + 2] = buffer[0][i * 3 + 2];
                                pixels[i * 4 + 3] = 255;
                                c += 4;
                            }
                        }
                        //} else if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {

                        //}

                        pixels += stride;
                    }

                    AndroidBitmap_unlockPixels(env, bitmap);
                } else {
                    throwException(env, "AndroidBitmap_lockPixels() failed ! error=%d", i);
                }

                jpeg_finish_decompress(&cinfo);
            } else {
                throwException(env, "the JPEG code has signaled an error");
            }

            jpeg_destroy_decompress(&cinfo);
            fclose(infile);
        } else {
            throwException(env, "can't open %s", fileName);
        }

        (*env)->ReleaseStringUTFChars(env, path, fileName);
    } else {
        throwException(env, "AndroidBitmap_getInfo() failed ! error=%d", i);
    }

nativeloader.java

private final static int LIB_VERSION = 4;
    private final static String LIB_NAME = "chat." + LIB_VERSION;
    .
    .
    .
    .
    .

        try {
            System.loadLibrary(LIB_NAME);
            nativeLoaded = true;
        } catch (Error e) {
            Log.e(Constants.TAG, e.getMessage());
        }

logcat的

01-29 11:15:34.747 3628-3628/rabta.pk.chatthahehehe E/cutils: to chown(/mnt/shell/emulated/0, 0, 0)
01-29 11:15:34.747 3628-3628/rabta.pk.chatthahehehe E/cutils: to chown(/mnt/shell/emulated/obb, 0, 0)
01-29 11:15:34.748 3628-3628/rabta.pk.chatthahehehe E/cutils: to chown(/storage/emulated/0/Android, 0, 0)
01-29 11:15:34.748 3628-3628/rabta.pk.chatthahehehe E/cutils: to chown(/storage/emulated/0/Android/obb, 0, 0)
01-29 11:15:35.057 3628-3628/rabta.pk.chatthahehehe E/linker: load_library(linker.cpp:759): library "libmaliinstr.so" not found
01-29 11:15:36.348 3628-3779/rabta.pk.chatthahehehe E/TAG: Native method not found: rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap:(Ljava/lang/String;Landroid/graphics/Bitmap;IIII)V
01-29 11:15:36.384 3628-3780/rabta.pk.chatthahehehe E/TAG: Native method not found: rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap:(Ljava/lang/String;Landroid/graphics/Bitmap;IIII)V
01-29 11:15:36.411 3628-3784/rabta.pk.chatthahehehe E/TAG: Native method not found: rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap:(Ljava/lang/String;Landroid/graphics/Bitmap;IIII)V
01-29 11:15:36.427 3628-3786/rabta.pk.chatthahehehe E/TAG: Native method not found: rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap:(Ljava/lang/String;Landroid/graphics/Bitmap;IIII)V
01-29 11:15:37.527 3628-3851/rabta.pk.chatthahehehe E/TAG: Native method not found: rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap:(Ljava/lang/String;Landroid/graphics/Bitmap;IIII)V
01-29 11:15:37.528 3628-3850/rabta.pk.chatthahehehe E/TAG: Native method not found: rabta.pk.chatthahehehe.AndroidUtilities.loadBitmap:(Ljava/lang/String;Landroid/graphics/Bitmap;IIII)V

1 个答案:

答案 0 :(得分:0)

您必须在Java端声明您的方法,如下所示:

class AndroidUtilities
{
   private static native void loadBitmap(String path, android.graphics.Bitmap bitmap, int scale, int width, int height, int stride);

}