編輯:關於Android編程
大致分是這幾步:
(1)新建Android工程:
HelloJni.java代碼
package com.panpass.main; public class HelloJni { static{ System.loadLibrary(demo-jni); } public void sayHello(){} public native String jniSay(); }
package com.panpass.main; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.example.demo.R; public class MainActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); mTextView = (TextView) findViewById(R.id.text_view); HelloJni demojin = new HelloJni(); mTextView.setText(demojin.jniSay()); } }(2)生成頭文件。
在cmd中進入相應的目錄下:
例如我的工程在這裡:
最終是進入到這裡:
也就是打開cmd後輸入命令:
cd C:androidworkoneDemoinclasses
接著輸入:javah -jni com.panpass.main.HelloJni
就會生成.h文件。
(3)新建jni文件。
demo-jni.c就是c層函數的具體實現。如下:
/* * 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. * */ #include#include /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */ jstring Java_com_panpass_main_HelloJni_jniSay( JNIEnv* env, jobject thiz) { return (*env)->NewStringUTF(env, hello jni); }
Java_com_panpass_main_HelloJni_jniSay( JNIEnv* env, jobject thiz)就是從生成的.h文件中來的。看生成的.h文件:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include/* Header for class com_panpass_main_HelloJni */ #ifndef _Included_com_panpass_main_HelloJni #define _Included_com_panpass_main_HelloJni #ifdef __cplusplus extern C { #endif /* * Class: com_panpass_main_HelloJni * Method: haveReturnFromJni * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_panpass_main_HelloJni_haveReturnFromJni (JNIEnv *, jobject, jstring); /* * Class: com_panpass_main_HelloJni * Method: jniSay * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_panpass_main_HelloJni_jniSay (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
# 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 := demo-jni LOCAL_SRC_FILES := demo-jni.c include $(BUILD_SHARED_LIBRARY)具體意思可以baidu。
(4)下載ndk使用ndk生成.so庫。
ndk下載去百度。
配置ndk環境變量:
其中NDK:
在cmd中進入工程根目錄:
接著執行:
ndk-build
刷新工程你就會發現:
多了libs和obj
最後運行,OK。
本文將向大家展示如何拍照截圖。先看看效果圖:拍照截圖有點兒特殊,要知道,現在的Android智能手機的攝像頭都是幾百萬的像素,拍出來的圖片都是非常大的。因此,我們不能像對
先看下最終的效果一、開始實現新建一個DoughnutView繼承View public class DoughnutView extends View { }先重寫o
Android Studio配置及使用OpenCV 前言:最近在做項目移植,項目較大,在Eclipse中配置的Jni及OpenCV環境沒任何問題,但是遷移到St
本文實例總結了Android編程中圖片特效處理方法。分享給大家供大家參考,具體如下:這裡介紹的Android圖片處理方法包括:轉換 - drawable To