当前位置: 代码迷 >> Android >> android jni开发详细方法
  详细解决方案

android jni开发详细方法

热度:59   发布时间:2016-05-01 12:51:47.0
android jni开发详细步骤

Jni开发步骤:

 

环境说明:

OS: Window xp sp3

Tools:eclipse juno(下载后,解压到指定路径即可)

Toolchain:cygwin(安装略,配置环境变量$NDK)

JDK: JDK 1.6.0_34(安装略,配置环境变量)

NDK: android-ndk-r8b

SDK:Android 2.3.3

 

新建android工程

工程名:FishEye

packagecom.baony.fisheys

 

创建接口类

package com.baony.fisheys;

 

publicclass NativeFishEye {

 

    publicnativeint openFile();

    publicnativeint openImage(int image_type);

   

    static{

       System.loadLibrary("fisheye");

    }

}

 

生成.h头文件

在项目目录创建jni文件夹,从cmd进入项目目录,输入

E:\android\workspace\FishEye>javah -classpath bin/classes -d jni com.baony.fisheys.NativeFishEye

 

E:\android\workspace\FishEye>

 

生成头文件,位于项目目录下的jni文件夹

E:\android\workspace\FishEye>cd jni

 

E:\android\workspace\FishEye\jni>dir

 驱动器 E中的卷没有标签。

 卷的序列号是 0009-B506

 

 E:\android\workspace\FishEye\jni 的目录

 

2012-11-16  16:25    <DIR>          .

2012-11-16  16:25    <DIR>          ..

2012-11-16  16:15               768 Android.mk

2012-06-24  02:43               748 Android.mk.bak

2012-11-16  16:25               342 com_baony_fisheys_NativeFishEye.c

2012-11-16  16:23               309 com_baony_fisheys_NativeFishEye.c.bak

2012-11-16  16:41               713 com_baony_fisheys_NativeFishEye.h

               5 个文件         2,880 字节

               2 个目录 303,776,391,168可用字节

 

E:\android\workspace\FishEye\jni>

 

 

根据生成的.h文件,创建.c文件

#include <jni.h>

 

JNIEXPORT jint JNICALL Java_com_baony_fisheys_NativeFishEye_openFile

  (JNIEnv *env, jobject thiz)

{

    jint retval=0xFEFD;

    return retval;

}

 

JNIEXPORT jint JNICALL Java_com_baony_fisheys_NativeFishEye_openImage

  (JNIEnv *env, jobject thiz, jint image_type)

{

    jint retval=0xFEFB;

    return retval;

}

 

编写Android.mk

# Copyright (C) 2009 The Android Open Source Project

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#      http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#

LOCAL_PATH := $(call my-dir)

 

include $(CLEAR_VARS)

 

LOCAL_MODULE    := fisheye

LOCAL_SRC_FILES := com_baony_fisheys_NativeFishEye.c

 

include $(BUILD_SHARED_LIBRARY)

 

编译jni

打开cygwin命令行,切换到项目目录/jni,输入$NDK/ndk-build,回车

$ cd /cygdrive/e/android/workspace/FishEye/jni

 

[email protected] /cygdrive/e/android/workspace/FishEye/jni

$

 

[email protected] /cygdrive/e/android/workspace/FishEye/jni

$ $NDK/ndk-build

Compile thumb  : fisheye <= com_baony_fisheys_NativeFishEye.c

SharedLibrary  : libfisheye.so

Install        : libfisheye.so => libs/armeabi/libfisheye.so

 

[email protected] /cygdrive/e/android/workspace/FishEye/jni

$

至此,jni编译完成

 

Java代码调用Native接口

package com.baony.fisheys;

 

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Button;

import android.widget.Spinner;

import android.widget.Toast;

import android.app.Activity;

import android.content.pm.ActivityInfo;

 

public class FishEye extends Activity {

    private Spinner m_imageType;

    private Spinner m_carSide;

    private Button m_openFile;

    private Button m_openImage;

   

    private NativeFishEye m_nativeFishEye;

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

       

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_fish_eye);

       

        initViews();

    }

   

    private void initViews()

    {

    m_nativeFishEye = new NativeFishEye();

    m_openFile = (Button)findViewById(R.id.bv_openfile);

    m_openImage = (Button)findViewById(R.id.bv_openimage);

    m_imageType = (Spinner)findViewById(R.id.bv_imagetype);

    m_carSide = (Spinner)findViewById(R.id.bv_carside);

   

    m_openFile.setOnClickListener(new OnClickListener(){

 

            @Override

            public void onClick(View arg0) {

                Toast.makeText(getApplicationContext(),

                        "OpenFile retval="+m_nativeFishEye.openFile(), Toast.LENGTH_SHORT)

                        .show();

            }});

   

    m_openImage.setOnClickListener(new OnClickListener(){

 

            @Override

            public void onClick(View v) {

                Toast.makeText(getApplicationContext(),

                        "OpenImage retval="+m_nativeFishEye.openImage(0), Toast.LENGTH_SHORT)

                        .show();

            }});

   

    m_imageType.setOnItemSelectedListener(new OnItemSelectedListener(){

 

            @Override

            public void onItemSelected(AdapterView<?> parent, View view,

                    int position, long id) {

                String value = (String)parent.getItemAtPosition(position);

                Toast.makeText(getApplicationContext(), "Image Type: "+value, Toast.LENGTH_SHORT).show();

            }

 

            @Override

            public void onNothingSelected(AdapterView<?> arg0) {

                // TODO Auto-generated method stub

               

            }});

   

    m_carSide.setOnItemSelectedListener(new OnItemSelectedListener(){

 

            @Override

            public void onItemSelected(AdapterView<?> parent, View view,

                    int position, long id) {

                String value = (String)parent.getItemAtPosition(position);

                Toast.makeText(getApplicationContext(), "Car Side: "+value, Toast.LENGTH_SHORT).show();

            }

 

            @Override

            public void onNothingSelected(AdapterView<?> arg0) {

                // TODO Auto-generated method stub

               

            }});

    }

}

 

 

运行结果如下

 OpenFile retval=65277

 

图贴不上来啊,郁闷啊!!!

 

  相关解决方案