美文网首页我爱编程
javah生成头文件

javah生成头文件

作者: 任振铭 | 来源:发表于2018-04-16 19:05 被阅读37次

编写native方法,如:

package com.example.renzhenming.appmarket.ui.selectimage;


import android.graphics.Bitmap;

public class ImageUtil {
    static {
        System.loadLibrary("jpeg");
        System.loadLibrary("compressimg");
    }

    /**
     * 图片压缩
     * @param quality 压缩的质量
     * @param fileName 压缩后的路径
     */
    public static void compressImage(Bitmap bitmap,int quality,
                                     String fileName){
        compressBitmap(bitmap,quality,fileName);
    }


    /**
     * NDK方法加载图片
     * @param quality 压缩的质量
     * @param fileName 压缩后的路径
     * @return
     */
    public native static int compressBitmap(Bitmap bitmap,int quality,
                                           String fileName);

    /*public static Bitmap decodeFile(String path) {
        int finalWidth = 800;

        // 先获取宽度
        BitmapFactory.Options options = new BitmapFactory.Options();
        // 不加载图片到内存只拿宽高
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path,options);

        int bitmapWidth = options.outWidth;

        int inSampleSize = 1;

        if(bitmapWidth>finalWidth){
            inSampleSize = bitmapWidth/finalWidth;
        }

        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path,options);
    }*/
}

cmd进入build/intermediates/classes/debug文件目录下,输入如下命令

C:\Users\renzhenming\Desktop\appdemo\AppMarketDemo\app\build\intermediates\classes\debug>javah -classpath D:\application\java\sdk\platforms\android-26\android.jar;. -jni com.example.renzhenming.appmarket.ui.selectimage.ImageUtil

其中的-classpath作用是引入安卓类库中的类,因为很明显在上边我们的例子中使用了Android中的bitmap类,如果我们不加-classpath的话,就会报错
错误: 找不到类android.graphics.Bitmap。(如果没有使用的话不需要加)
注意后边的分号和点必须加,否则无法找到


图片.png

编写完成后make project 一下,会在文件夹下生成.h文件(不make也许也可以)


图片.png
这样我们就获取到头文件了,可以进行c语言编程了
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_renzhenming_appmarket_ui_selectimage_ImageUtil */

#ifndef _Included_com_example_renzhenming_appmarket_ui_selectimage_ImageUtil
#define _Included_com_example_renzhenming_appmarket_ui_selectimage_ImageUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_renzhenming_appmarket_ui_selectimage_ImageUtil
 * Method:    compressBitmap
 * Signature: (Landroid/graphics/Bitmap;ILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_example_renzhenming_appmarket_ui_selectimage_ImageUtil_compressBitmap
  (JNIEnv *, jclass, jobject, jint, jstring);

#ifdef __cplusplus
}
#endif
#endif

相关文章

网友评论

    本文标题:javah生成头文件

    本文链接:https://www.haomeiwen.com/subject/seoekftx.html