本帖最后由 Smile__LV 于 2010-09-21 15:51:05 编辑

解决方案 »

  1.   

    现在说我在java层H264Android.java调用的函数
    package com.handlerun.view;import java.nio.ByteBuffer;
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Bitmap.Config;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;public class H264Android extends Activity { VView vv;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            vv = new VView(this);
            setContentView(vv);
        }
        
        // Menu item Ids
        public static final int PLAY_ID = Menu.FIRST;    
        public static final int EXIT_ID = Menu.FIRST + 1;     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            
            menu.add(0, PLAY_ID, 0, R.string.play);   
            menu.add(0, EXIT_ID, 1, R.string.exit);        return true;
        }    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {        
            case PLAY_ID:
            {
             String file = "/sdcard/free.264";
             vv.PlayVideo(file);
                return true;
            }
            case EXIT_ID:
            {
             finish();
                return true;
            }
            }
            return super.onOptionsItemSelected(item);
        }}class VView extends View  implements Runnable{
       
        Bitmap  mBitQQ  = null;   
        
        Paint   mPaint = null;   
           
        Bitmap  mSCBitmap = null;   
        
        Matrix matrix = new Matrix(); 
        
        byte [] mPixel = new byte[288*352*2];
        
        ByteBuffer buffer = ByteBuffer.wrap( mPixel );
    Bitmap VideoBit = Bitmap.createBitmap(352, 288, Config.RGB_565);           
        
    VView v  = this;

    int mTrans=0x0F0F0F0F;
        public native int InitDecoder();
        public native int UninitDecoder(); 
        public native int DecoderNal(byte[] in, int insize, byte[] out);
        public native void LiveVideo(byte[] out);
        public native int getSequence(byte[] out);
        public native int Connectiondvs(String ip,int port);
        //public native int Login(String username,String password);
        public native int LoginCam(String username,String password);
        public native int StopLive();
        static 
        {
            System.loadLibrary("H264Android");
        }
    /**
     * 构造函数
     */
        public VView(Context context) {
            super(context);
            setFocusable(true);
            
            int i = mPixel.length;
        
            for(i=0; i<mPixel.length; i++)
            {
             mPixel[i]=(byte)0x00;
            }
        }
       /**
    * 播放视频
    * @param file
    */
        public void PlayVideo(String file)
        {
         String uName="admin";
         String pWd="123456";
         int connect=Connectiondvs("192.168.1.170",5014);
         System.out.println("connect+++"+connect);
         int lo=LoginCam(uName, pWd);
         System.out.println("login+++"+lo);         
         if(lo==1&&connect==1){
         new Thread(this).start();
             new Thread(new Video()).start();
         }    
        }
        
        @Override protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);   
          VideoBit.copyPixelsFromBuffer(buffer);//makeBuffer(data565, N));     
          canvas.drawBitmap(VideoBit, 0, 0, null); 
        }    public void updateUI()
        {
         postInvalidate();
        }    public void run()   
        {    
         byte[] index ={0};
         int getIndex  = 0;
         int beforeIndex = 0;
         
            while (!Thread.currentThread().isInterrupted())   
            {   
             getIndex  = getSequence(index);
             //System.out.println("getIndex++++"+getIndex);
             if(getIndex!=0&&getIndex!=beforeIndex)
             {
                 System.out.println(" #################################### ");
                
               //运行测试内容
                 long t1= System.currentTimeMillis();
                 updateUI();
                 long t2= System.currentTimeMillis();
    //             System.out.println("******** tiem " +  (t2-t1)); 
             beforeIndex  = getIndex  ;
             }
         } 
        }
           public void sayHello()
    {
    System.out.println("****************#####################################################");
    }
            class Video implements Runnable{
    public void run() {
    // TODO Auto-generated method stub
              LiveVideo(mPixel);
    }
        }
    }
    单步调试到int lo=LoginCam(uName, pWd);就会报错空指针而和LoginCam这个函数很相似的函数Connectiondvs却不报错。而且可以输出connect+++1的结果。
      

  2.   

    这里给出Connectiondvs函数在JNI层被调用的代码:
    JNIEXPORT jint JNICALL Java_com_handlerun_view_VView_Connectiondvs
      (JNIEnv *env, jobject obj, jstring ip, jint port)
    {
      ipAdress=(char*)(*env)->GetStringUTFChars(env,ip, NULL);  portNum=++port;//HTTP端口加1是tcp端口  return 1;
    }
      

  3.   

    CSDN人气这么旺盛 自己不顶马上就沉底了、、、、、、、、
    大侠来帮忙啊 
      

  4.   

    原因找到了 原来是我使用char接收一个字符串 出错了 错误代码:
    int portNum=NULL;//端口
    char ipAdress=NULL;//IP地址
    char u_name=NULL;//摄像头用户名
    char pwd=NULL;//密码以及:
    JNIEXPORT jint JNICALL Java_com_handlerun_view_VView_LoginCam
      (JNIEnv *env,jobject obj,jstring username,jstring password)
    {pwd =(char*)(*env)->GetStringUTFChars(env,password, NULL);
      u_name=(char*)(*env)->GetStringUTFChars(env,username, NULL);  char um="admin";
    char pd="123456";
    int a=strcmp(um,u_name);
    int b=strcmp(pd,password);
    if(a==0&&b==0)
    {
    return 0;
    }else{
    return 1;
    }
    应该修改成:int portNum=NULL;//端口
    char *ipAdress=NULL;//IP地址
    char *u_name=NULL;//摄像头用户名
    char *pwd=NULL;//密码
    以及:
    u_name=(char*)(*env)->GetStringUTFChars(env,username, NULL);
    pwd =(char*)(*env)->GetStringUTFChars(env,password, NULL);
    char *pd="123456";
    char *um="admin"; int a=strcmp(um,u_name);
    int b=strcmp(pd,password);
    if(a==0&&b==0)
    {
    return 0;
    }else{
       return 1;
    }
      

  5.   

    首先恭喜lz把程序改成了没有错误的,但你这里有内存泄露的bug。pwd =(char*)(*env)->GetStringUTFChars(env,password, NULL);你对java层的password增加了一次引用,但从没释放(C层删除pwd 时,你没有通知java层),所以在java层一直认为这一变量引用计数不为0,也就永远不会删除这个变量,后果很严重,你懂的。在你不需要再试用pwd时,写入(*env)->ReleaseStringUTFChars(env, jstring, pwd ); 通知java层,此变量减少1引用计数。
      

  6.   

    最近也在做jni,所以搜到的,正好发现你有个bug,所以提出来
      

  7.   

    楼主啊~我最近也在做网络摄像头,登录和获取h.264  搞得我头大!!!能否赐教一下啊,最好有个demo    [email protected] 拜谢~