各位大侠,小弟现在做一个Android手机视频监控软件,就是通过手机查看摄像头的视频,就像飞天眼。我们把每个用
户的信息保存在一个服务器上,使用XML格式。用户登录的时候需要用户名和密码,用户查看某个摄像头的时候我们要传数据过去(IP和端口还有摄像头用户名
和密码)。我们使用JNI解码H264视频数据。现在遇到一个问题就是在Java层Login(String name,String
password)直接传给JNI的C程序会出错。不知道各位大侠有什么好办法帮忙解决吗?QQ467887719
小弟在此先谢了![code=Java][/code]package com.handlerun.view;
import
java.io.FileInputStream; import java.io.IOException; import
java.io.InputStream; 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/butterfly.264";
String file = "/sdcard/2.264"; vv.PlayVideo(file);
//播放视频 return true; }
case EXIT_ID: { finish();
//退出 return true; } }
return super.onOptionsItemSelected(item); } }
//
实现Runable接口 class VView extends View implements Runnable{
Bitmap mBitQQ = null; Paint mPaint = null;
Bitmap mSCBitmap = null; Matrix matrix = new
Matrix(); byte [] mPixel = new byte[240*576*2];
ByteBuffer buffer = ByteBuffer.wrap( mPixel ); Bitmap VideoBit =
Bitmap.createBitmap(352, 288, Config.RGB_565); int
mTrans=0x0F0F0F0F; String PathFileName; public
native int InitDecoder(); public native int UninitDecoder();
public native int DecoderNal(byte[] in, int insize, byte[] out);
public native int Connectiondvs(String ip,int port); public
native int Login(String username,String password); public
native int LiveVideo(int videoCH,byte[] out); 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; } }
//播放视频函数 public void PlayVideo(String
file) { PathFileName = file;
new
Thread(this).start(); } @Override
protected void onDraw(Canvas canvas) { super.onDraw(canvas);
// Bitmap tmpBit =
Bitmap.createBitmap(mPixel, 320, 480,
Bitmap.Config.RGB_565);//.ARGB_8888);
VideoBit.copyPixelsFromBuffer(buffer);//makeBuffer(data565, N));
matrix.postScale(1, 1);
matrix.setRotate(90,120,130); canvas.drawBitmap(VideoBit,
matrix, null); // canvas.drawBitmap(VideoBit, 0, 0,
null); } int MergeBuffer(byte[] NalBuf, int
NalBufUsed, byte[] SockBuf, int SockBufUsed, int SockRemain) {
int i=0; byte Temp;
for(i=0;
i<SockRemain; i++) { Temp
=SockBuf[i+SockBufUsed]; NalBuf[i+NalBufUsed]=Temp;
mTrans <<= 8; mTrans |= Temp;
if(mTrans == 1) // 找到一个开始字 { i++;
break; } } return i; }
//重写run方法 public void run() {
InputStream is = null; FileInputStream fileIS=null;
int iTemp=0; int nalLen; boolean
bFirst=true; boolean bFindPPS=true; int
bytesRead=0; int NalBufUsed=0; int SockBufUsed=0;
byte [] NalBuf = new byte[40980]; // 40k
byte [] SockBuf = new byte[2048]; InitDecoder();
try { int result =
Connectiondvs("IP(直接传IP值)",端口); System.out.println("
connection stat " + result ); if(result==1) {
result = Login("admin","654321");
if(result==1) { System.out.println(" loign
succ .........." ); return ; }
else { System.out.println(" loign fail
.........." ); return ; } }
else { System.out.println("
connection fail .........." ); return ; }
// fileIS = new FileInputStream(PathFileName); }
catch(Exception e) {} while
(!Thread.currentThread().isInterrupted()) {
try { bytesRead =
fileIS.read(SockBuf, 0, 2048); if(bytesRead<=0)
break; // //
iTemp=DecoderNal(SockBuf, 2048, mPixel); // //
if(iTemp>0) // { // postInvalidate();
//使用postInvalidate可以直接在线程中更新界面 // postInvalidate(); //
} } catch (IOException e) {}
SockBufUsed
=0; while(bytesRead-SockBufUsed>0)
{ nalLen = MergeBuffer(NalBuf, NalBufUsed, SockBuf,
SockBufUsed, bytesRead-SockBufUsed); NalBufUsed
+= nalLen; SockBufUsed += nalLen;
while(mTrans == 1) { mTrans = 0xFFFFFFFF;
if(bFirst==true) // the first start flag {
bFirst = false; } else // a complete NAL
data, include 0x00000001 trail. {
if(bFindPPS==true)/// true { if(
(NalBuf[4]&0x1F) == 7 ) { bFindPPS =
false; } else {
NalBuf[0]=0; NalBuf[1]=0;
NalBuf[2]=0; NalBuf[3]=1;
NalBufUsed=4; break;
} } // decode nal
System.out.println("DecoderNal .... NalBufUsed" + (NalBufUsed-4));
iTemp=DecoderNal(NalBuf, NalBufUsed-4, mPixel);
System.out.println("DecoderNal .... iTemp" + iTemp);
if(iTemp>0) {
postInvalidate(); //使用postInvalidate可以直接在线程中更新界面 postInvalidate();
} }
NalBuf[0]=0;
NalBuf[1]=0; NalBuf[2]=0; NalBuf[3]=1;
NalBufUsed=4; } }
} try{ if(fileIS!=null)
fileIS.close(); if(is!=null) is.close();
} catch (IOException e) { e.printStackTrace();
} UninitDecoder(); } }
|