本帖最后由 Smile__LV 于 2010-09-17 10:37:09 编辑

解决方案 »

  1.   

    C++与Java直接的数据格式貌似要转换一下吧!
    高手过来看一下。
      

  2.   

    把你的错误发出来看看,看是不是参数传递有问题。检查检查吧!你的Java代码应该没问题
      

  3.   

    没有错误的   问题是这样的比如我在Java层传一个用户名 admin到JNI层C里面取出来就不是admin了
      

  4.   

    JNI中的。C文件代码#include <jni.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <errno.h>
    #include <malloc.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/ioctl.h>
    #include <stdarg.h>
    #include <fcntl.h>
    #include <pthread.h>
    #include <sys/poll.h>
    #include <sys/select.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <errno.h>
    #include "dvrdef.h"
    const int SOCKET_ERROR  = -1;
    int s;
    int SendData(int sockfd,char* pch,const int nSize,const int nSecs)
    {
    int size = nSize;
    int nBytesReceived;
    while( size >0)
    {
    nBytesReceived = send(sockfd, pch, size,0);
    if(nBytesReceived == SOCKET_ERROR || nBytesReceived ==0) {
    return SOCKET_ERROR;
    }
    size -= nBytesReceived;
    pch += nBytesReceived;
    }
    return nSize - size;
    }int ReceiveData(int sockfd,char* pch, const int nSize, const int nSecs)
    {
    int size = nSize;
    int nBytesReceived;
    while( size >0)
    {
    nBytesReceived = recv(sockfd, pch, size,0);
    if(nBytesReceived == SOCKET_ERROR || nBytesReceived ==0) {
    return SOCKET_ERROR;
    }
    size -= nBytesReceived;
    pch += nBytesReceived;
    }
    return nSize - size;
    }void *SendPulsePackThread(const int *s)
    {
    DATAHEAD DataHead;
    DataHead.HeadFlag = '1111';
    DataHead.lLen = 0;
    while(1)
    {
    sleep(10);
    if(SendData(s,(char*)&DataHead,sizeof(DATAHEAD),10) == SOCKET_ERROR)
    {
    return 0;
    }
    }
    }
    int  main()
    {
    int nRetCode = 0;
    int m_DeviceSock;
    struct sockaddr_in m_DeviceAddr;
    int result;
    DATAHEAD   datahead;
    PACKCMD    CmdPack;
    NET_LOGIN_SUCCESS_REPLY Login_Ruturn_Info;//登陆返回信息
    FRAME_INFO_EX frame_info;//帧信息
    NET_COMBINED_DATA combined_data;//组合包信息
    NET_LOGIN_INFO m_NetLoginInfo; NET_LIVESTREAM_CTRL_INFO  LiveStart;
    char buf[200];
    char *data_buf = NULL;
    char *combined_data_buf = NULL; m_DeviceAddr.sin_family = AF_INET;
    m_DeviceAddr.sin_port = htons(5015);
    m_DeviceAddr.sin_addr.s_addr = inet_addr("写死的IP地址");
    m_DeviceSock = socket(AF_INET,SOCK_STREAM,0);
    if(m_DeviceSock == SOCKET_ERROR)
    {
    printf("error:%d\n errno  \n",errno);
    printf("create socket failed!");
    return 0;
    } result = connect(m_DeviceSock,(struct sockaddr*)&m_DeviceAddr,sizeof(struct sockaddr));
    if( result == SOCKET_ERROR)
    {
    m_DeviceSock = NULL;
    return 0;
    } memset(buf,0,200);
    result = ReceiveData(m_DeviceSock,buf,64,20);
    if( result == SOCKET_ERROR)
    {
    m_DeviceSock = NULL;
    return 0;
    } int dataheadsize = sizeof(DATAHEAD);
    int packcmd = sizeof(PACKCMD);
    int net_login_info = sizeof(NET_LOGIN_INFO); datahead.HeadFlag = '1111';
    datahead.lLen = packcmd +net_login_info; CmdPack.cmdType = CMD_REQUEST_LOGIN;
    CmdPack.dataLen = net_login_info; m_NetLoginInfo.connectType = CONNECTTYPE_IE;
    m_NetLoginInfo.netprotocolver = NET_PROTOCOL_VER; //3
    strcpy(m_NetLoginInfo.username,"admin");
    strcpy(m_NetLoginInfo.password ,"123456"); result =SendData(m_DeviceSock,(char*)&datahead,sizeof(datahead),10);
    result =SendData(m_DeviceSock,(char*)&CmdPack,sizeof(CmdPack),10);
    result =SendData(m_DeviceSock,(char*)&m_NetLoginInfo,sizeof(m_NetLoginInfo),10);
    if( result == SOCKET_ERROR)
    {
    printf("error:%d\n send  cmd   fail   \n",errno);
    return 0;
    } if( result == 0)
    {
    m_DeviceSock = NULL;
    return 0;
    } unsigned long dataID = -1;
    int combined_count ;
    FILE* fp = NULL;
    fp = fopen("free.264","wb");
    while(1)
    {
    memset(&datahead,0,sizeof(DATAHEAD));
    result = ReceiveData(m_DeviceSock,(char*)&datahead,sizeof(DATAHEAD),20);//接收数据头
    if( result == SOCKET_ERROR)
    break;
    if( datahead.lLen == -1)
    {//接收组合包
    memset(&combined_data,0,sizeof(NET_COMBINED_DATA));
    result = ReceiveData(m_DeviceSock,(char*)&combined_data,sizeof(NET_COMBINED_DATA),20);
    if( result == SOCKET_ERROR)
    break;
    if( dataID != combined_data.dwDataID )
    {
    free(combined_data_buf);
    combined_data_buf = NULL;
    combined_data_buf =  (char *) malloc ( sizeof(char) * combined_data.dwTotalLen );
    combined_count = 0;
    dataID = combined_data.dwDataID;
    }
    combined_count++;
    result = ReceiveData(m_DeviceSock,combined_data_buf + (combined_data.dwIndex -1)*5000,combined_data.dwLen,20);
    if( result == SOCKET_ERROR)
    break;
    if( combined_data.dwTotalPack == combined_count)
    {
    if( fp != NULL)
    {
    fwrite(combined_data_buf,1,combined_data.dwTotalLen,fp);
    }
    }
    }
    else
    {
    data_buf = (char *) malloc ( sizeof(char) * datahead.lLen); if( ReceiveData(m_DeviceSock,data_buf,datahead.lLen,20) == SOCKET_ERROR)
    break;
    switch(((PACKCMD*)data_buf)->cmdType)
    {
    case CMD_REPLY_LONGIN_FAIL://登录失败
    printf("登陆失败\n");
    m_DeviceSock = NULL;
    free(data_buf);
    return 0;
    break;
    case CMD_REPLY_LONGIN_SUCC://登录成功
    //后面还有一个NET_LOGIN_SUCCESS_REPLY
    printf("videoInputNum:%d\n",((NET_LOGIN_SUCCESS_REPLY*)(data_buf+sizeof(PACKCMD)))->videoInputNum);
    //pThis->m_IsLoginSuccess = true;
    //启动心跳包
    pthread_t thread;
    memset(&thread, 0, sizeof(thread));
    int err;
    printf("创建线程\n");
    /*创建线程*/
    err  = pthread_create(&thread, NULL,(void *)SendPulsePackThread,m_DeviceSock);
    if(err != 0)
    {
       printf("can't create thread: %s\n",strerror(err));
       return 1;
    }
    else
    {
    printf("can't create thread succ: %s \n");
    } datahead.HeadFlag = '1111';
    datahead.lLen = sizeof(CmdPack) + sizeof(LiveStart); CmdPack.cmdType = CMD_REQUEST_STREAM_START;
    CmdPack.dataLen = sizeof(LiveStart); LiveStart.fastVideoCH = 0x1<<0;
    LiveStart.audioCH = 0;
    LiveStart.slowVideoCH = 0; result =SendData(m_DeviceSock,(char*)&datahead,sizeof(datahead),10);
    result =SendData(m_DeviceSock,(char*)&CmdPack,sizeof(CmdPack),10);
    result =SendData(m_DeviceSock,(char*)&LiveStart,sizeof(LiveStart),10); if( result == SOCKET_ERROR)
    {
    printf("发送直播命令失败\n");
    return 0;
    }
    break;
    case CMD_REPLY_VIDEO_LOSS:
    case CMD_REPLY_MOTION:
    case CMD_REPLY_SENSOR:
    case CMD_REPLY_REC_STATUS:
    //用NET_STATUS解析通道状态
    break;
    case CMD_REPLY_DATA_STREAM:
    switch( ((FRAME_INFO_EX*)(data_buf+ sizeof(PACKCMD)))->frameType)
    {
    case  FRAME_TYPE_VIDEO_FORMAT:
    //视频格式
    //用定时器切换菜单和打开云台
    //SetTimer(pThis->m_hMainWnd,TIMER_DVS_DECODER,40,NULL); //pThis->InitImage(data_buf + sizeof(PACKCMD)+ sizeof(FRAME_INFO_EX),sizeof(BITMAPINFOHEADER));
    break;
    case FRAME_TYPE_VIDEO: if( fp != NULL)
    {
    fwrite(data_buf+ sizeof(PACKCMD)+ sizeof(FRAME_INFO_EX),1,((PACKCMD*)data_buf)->dataLen - sizeof(FRAME_INFO_EX),fp);
    }
    break;
    }
    break;
    default:
    break;
    }
    free(data_buf);
    data_buf = NULL;
    }
    }
    free(combined_data_buf);
    combined_data_buf = NULL;
    free(data_buf);
    data_buf = NULL;
    fclose(fp);
    }
      

  5.   

    你用的是Socket,搞了半天,这个我就不知道了,我还以为是Binder。
    汗!建议你把结果告诉大家,你就说不是admin,那是什么?
    Socket通信这块兄弟不熟,找这方面的高手吧!
      

  6.   

    应该要实现以下接口的:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class VView */#ifndef _Included_VView
    #define _Included_VView
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     VView
     * Method:    InitDecoder
     * Signature: ()I
     */
    JNIEXPORT jint JNICALL Java_VView_InitDecoder
      (JNIEnv *, jobject);/*
     * Class:     VView
     * Method:    UninitDecoder
     * Signature: ()I
     */
    JNIEXPORT jint JNICALL Java_VView_UninitDecoder
      (JNIEnv *, jobject);/*
     * Class:     VView
     * Method:    DecoderNal
     * Signature: ([BI[B)I
     */
    JNIEXPORT jint JNICALL Java_VView_DecoderNal
      (JNIEnv *, jobject, jbyteArray, jint, jbyteArray);/*
     * Class:     VView
     * Method:    Connectiondvs
     * Signature: (Ljava/lang/String;I)I
     */
    JNIEXPORT jint JNICALL Java_VView_Connectiondvs
      (JNIEnv *, jobject, jstring, jint);/*
     * Class:     VView
     * Method:    Login
     * Signature: (Ljava/lang/String;Ljava/lang/String;)I
     */
    JNIEXPORT jint JNICALL Java_VView_Login
      (JNIEnv *, jobject, jstring, jstring);/*
     * Class:     VView
     * Method:    LiveVideo
     * Signature: (I[B)I
     */
    JNIEXPORT jint JNICALL Java_VView_LiveVideo
      (JNIEnv *, jobject, jint, jbyteArray);/*
     * Class:     VView
     * Method:    StopLive
     * Signature: ()I
     */
    JNIEXPORT jint JNICALL Java_VView_StopLive
      (JNIEnv *, jobject);#ifdef __cplusplus
    }
    #endif
    #endif
      

  7.   

    其中Java_VView_Login就是这样的吧:
    JNIEXPORT jint JNICALL Java_VView_Login(JNIEnv * env, jobject self, jstring user, jstring pswd)
    {
    const jbyte *pUser;
    const jbyte *pPswd;
    pUser = (*env)->GetStringUTFChars(env, user, NULL);
    pPswd = (*env)->GetStringUTFChars(env, pswd, NULL);
    if (NULL == pUser || NULL == pPswd)
    {
    return -1;
    } // TODO: do login process. return 0;
    }
      

  8.   

    AndroidManifest.xml注意添加Permission权限
      

  9.   


    朋友 这是你的.h  我的也有这个,是.JAVA使用javac生成.class,然后再javah生成.h我们很一样啊,你也是在做h264的?我的是这样写的 不错啊 
      

  10.   

    朋友 您说的我不是 很懂 什么权限 ?怎么加?
    我吧我的AndroidManifest.xml粘贴上来
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.handlerun.view"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Sst"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SplashActivity"
    android:label="@string/splash_name">
    </activity>
            
    <activity android:name=".AlertQueryActivity"
    android:label="@string/alertquery_name">
    </activity>

    <activity android:name=".AlertViewActivity"
    android:label="@string/alertview_name">
    </activity>

    <activity android:name=".HelpActivity"
    android:label="@string/help_name">
    </activity>

    <activity android:name=".LoginParmActivity"
    android:label="@string/loginparm_name">
    </activity>

    <activity android:name=".MainMenuActivity"
    android:label="@string/mainmenu_name">
    </activity>

    <activity android:name=".PictureListActivity"
    android:label="@string/picturelist_name">
    </activity> <activity android:name=".VideoListActivity"
    android:label="@string/videolist_name">
    </activity> <activity android:name=".VideoParmActivity"
    android:label="@string/videoparm_name">
    </activity>

    <activity android:name=".RecordListActivity"
    android:label="@string/recordlist_name">
    </activity>

    <activity android:name=".WaitActivity"
    android:label="@string/wait_name">
    </activity>
        
        <activity android:name=".H264Android"
                   android:label="@string/H264_name">
        </activity>


        </application>
        <uses-permission android:name="android.permission.INTERNET" /> 
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
    </uses-permission>
    <uses-sdk android:minSdkVersion="8" />
    </manifest> 
      

  11.   

    c中实现的东西要确定在Android上是API Permission的,头文件可以利用javah生成,编码自己别抛异常问题不大。
      

  12.   

    朋友这个我不可以把那些变量定义成const因为我的这用户名和密码是根据不同用户改变的。另外不知道定义成char不使用byte可以不?
      

  13.   

    给为朋友 我又重新整理了一下这个问题 新帖子请看:
    http://topic.csdn.net/u/20100921/13/c6146bb3-3fbc-48bc-a702-9faf11bdc489.html
    坐等 高手求指点