zoukankan      html  css  js  c++  java
  • and cmake 链接库及播放例子 及读文件

    建一个Native-cpp工程

    1、引入android库的方法

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.4.1)
    
    add_library( # Sets the name of the library.
                 native-lib
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 native-lib.cpp )
    
    find_library( # Sets the name of the path variable.
            log-lib
    
            # Specifies the name of the NDK library that
            # you want CMake to locate.
            log )
    find_library( # Sets the name of the path variable.
            android-lib
    
            # Specifies the name of the NDK library that
            # you want CMake to locate.
            android )
    
    target_link_libraries( # Specifies the target library.
                           native-lib
    
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib}   ${android-lib})

    2、java

    public class MainActivity extends AppCompatActivity {
    
        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");
        }
        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();
    
        public native void playRgb(Surface sf);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MyView(this));
            /*
            setContentView(R.layout.activity_main);
    
            // Example of a call to a native method
            TextView tv = findViewById(R.id.sample_text);
            tv.setText(stringFromJNI());
            */
        }
    
        //------------------------------>
        //视图内部类
        class MyView extends SurfaceView implements SurfaceHolder.Callback
        {
            private SurfaceHolder holder;
            private MyThread myThread;
            public MyView(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
                holder = this.getHolder();
                holder.addCallback(this);
                myThread = new MyThread(holder);//创建一个绘图线程
            }
    
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                       int height) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                myThread.isRun = true;
                myThread.start();
            }
    
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                myThread.isRun = false;
            }
    
        }
        //线程内部类
        class MyThread extends Thread
        {
            private SurfaceHolder holder;
            public boolean isRun ;
            public  MyThread(SurfaceHolder holder)
            {
                this.holder =holder;
                isRun = true;
            }
            @Override
            public void run()
            {
                int count = 0;
                while(isRun)
                {
                    Canvas c = null;
                    try
                    {
                        synchronized (holder)
                        {
                            //new Hello().playRgb();
                            playRgb(holder.getSurface());
    
                            Thread.sleep(1000);//睡眠时间为1秒
                        }
                    }
                    catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                    finally
                    {
                        if(c!= null)
                        {
                            holder.unlockCanvasAndPost(c);//结束锁定画图,并提交改变。
    
                        }
                    }
                }
            }
        }
    
    }

    3、cpp  看来该有的头文件和库都有了

    #include <jni.h>
    #include <string>
    #include <unistd.h>
    #include <android/native_window_jni.h>
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_testnativecpp_MainActivity_stringFromJNI(
            JNIEnv* env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    char rgb[640*480*4];
    extern "C" JNIEXPORT void JNICALL
    Java_com_example_testnativecpp_MainActivity_playRgb(
            JNIEnv* env,
            jobject /* this */,
            jobject surface) {
    
        int outWidth = 640;
        int outHeight = 480;
        //char *rgb =  (char* )malloc(outWidth * outHeight * 4);
        ANativeWindow *nwin = ANativeWindow_fromSurface(env, surface);
        ANativeWindow_setBuffersGeometry(nwin, outWidth, outHeight, WINDOW_FORMAT_RGBA_8888);
    
        for(int i=0;i<255;i++){
            memset(rgb,i,640*480);
            memset(rgb+640*480,255-i,640*480);
    
            ANativeWindow_Buffer wbuf;
            ANativeWindow_lock(nwin, &wbuf, 0);
            uint8_t *dst = (uint8_t *) wbuf.bits;
            memcpy(dst, rgb, outWidth * outHeight * 4);
            ANativeWindow_unlockAndPost(nwin);
            usleep(100);
        }
    }

    试试读文件竟然直接可以

    char StrLine[1024]={0};
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_testnativecpp_MainActivity_stringFromJNI(
            JNIEnv* env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        FILE *fp;
        char ch;
        char *strFile = "data/data/com.example.testnativecpp/1.txt";
        if((fp=fopen(strFile,"r"))==NULL){
           // printf("
    Cannot open file strike any key exit!");
    
            return env->NewStringUTF("wrong!");
        }
        fgets(StrLine,1023,fp);
        fclose(fp);
        return env->NewStringUTF(StrLine);
    }
  • 相关阅读:
    DLX
    逆康托展开
    康托展开
    light oj 1427(ac自动机)
    hdu 2586 (lca-RMQ)
    约瑟夫问题
    C-运算符
    C-数据
    shell-流程控制
    python编程规范-最佳实践
  • 原文地址:https://www.cnblogs.com/cnchengv/p/15174571.html
Copyright © 2011-2022 走看看