Skip to content

Commit

Permalink
bug修复
Browse files Browse the repository at this point in the history
  • Loading branch information
HUPENG committed Sep 11, 2016
1 parent bc2d476 commit fcb5373
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="me.hupeng.android.monitor"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="19"/>
<uses-sdk android:minSdkVersion="18"/>
<!--以下为申请的各种权限-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Expand Down
79 changes: 67 additions & 12 deletions src/me/hupeng/android/monitor/Mina/MinaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Bitmap;
import android.util.Log;
import me.hupeng.android.monitor.Listener.SimpleListener;
import me.hupeng.android.monitor.UI.MainActivity;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
Expand Down Expand Up @@ -98,20 +99,40 @@ public static MinaUtil getInstance(SimpleListener simpleListener,Boolean isServe
private MinaUtil(SimpleListener simpleListener, Boolean isServer,String serverAddr){
this.isServer = isServer;
this.simpleListener = simpleListener;
this.serverAddr = serverAddr;
if (!isServer) {
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
NioSocketConnector connector = new NioSocketConnector();
connector.setHandler(new MyClientHandler());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MyImageFactory()));
ConnectFuture future;
if (serverAddr != null){
future = connector.connect(new InetSocketAddress(serverAddr, 9191));
}else {
future = connector.connect(new InetSocketAddress("192.168.43.1", 9191));
}

future.awaitUninterruptibly();
MinaUtil.this.session = future.getSession();
break;

}catch (Exception e){
e.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

NioSocketConnector connector = new NioSocketConnector();
connector.setHandler(new MyClientHandler());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MyImageFactory()));
ConnectFuture future;
if (serverAddr != null){
future = connector.connect(new InetSocketAddress(serverAddr, 9191));
}else {
future = connector.connect(new InetSocketAddress("192.168.43.1", 9191));
}
}
}).start();

future.awaitUninterruptibly();
this.session = future.getSession();
}else {
if (acceptor == null){
acceptor = new NioSocketAcceptor(Runtime.getRuntime().availableProcessors() + 1);
Expand Down Expand Up @@ -163,7 +184,13 @@ public Boolean send(Object obj){
return flag;
}else {
try {
session.write(obj);
if (session == null){
MyData myData = (MyData)obj;
myData.bitmap.recycle();
}else{
session.write(obj);
}

}catch (Exception e){
return false;
}
Expand Down Expand Up @@ -231,6 +258,34 @@ public void messageSent(IoSession session, Object message) throws Exception {
public void sessionClosed(IoSession session) throws Exception {
System.out.println(session.getId());
System.out.println("sessionClosed");
MinaUtil.this.session = null;
new Thread(new Runnable() {
@Override
public void run() {

while (true) {
try {
Thread.sleep(1000);
NioSocketConnector connector = new NioSocketConnector();
connector.setHandler(new MyClientHandler());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MyImageFactory()));
ConnectFuture future;
if (serverAddr != null) {
future = connector.connect(new InetSocketAddress(serverAddr, 9191));
} else {
future = connector.connect(new InetSocketAddress("192.168.43.1", 9191));
}
future.awaitUninterruptibly();
MinaUtil.this.session = future.getSession();
System.out.println("连接");
break;
} catch (Exception ex) {
Log.i("连接", ex.getMessage());
System.out.println("连接:" + ex.getMessage());
}
}
}
}).start();
}

public void sessionCreated(IoSession session) throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion src/me/hupeng/android/monitor/UI/ConfigActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onClick(View view) {
* */
private String getServerIp(){
String tempServerIp = SharedPreferencesUtil.readString(ConfigActivity.this, "server");
return (tempServerIp==null || tempServerIp.equals("")) ? "0.0.0.0" : tempServerIp;
return (tempServerIp==null || tempServerIp.equals("")) ? "183.175.12.160" : tempServerIp;
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/me/hupeng/android/monitor/UI/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback,Cam
* */
private MinaUtil minaUtil = null;

/**
* 录像标记
* */
private boolean videoFlag = false;


/**
* 摄像头相关代码
* */
Expand Down Expand Up @@ -180,7 +186,7 @@ public void surfaceChanged(SurfaceHolder holder, int format, int width, int heig
camera.setDisplayOrientation(90);

camera.setParameters(params);
// camera.startPreview() ;
camera.startPreview() ;

camera.setPreviewCallback(this);
}catch(Exception e){
Expand All @@ -196,7 +202,7 @@ public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
if (previewTime ++ % loweredRate == 0){
if (videoFlag && previewTime ++ % loweredRate == 0){
Camera.Size size = camera.getParameters().getPreviewSize();
try {
YuvImage image = new YuvImage(bytes, ImageFormat.NV21, size.width,
Expand Down Expand Up @@ -238,10 +244,12 @@ class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListene
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
//open
camera.startPreview();
videoFlag = true;
//camera.startPreview();
}else {
//close
camera.stopPreview();
videoFlag = false;
//camera.stopPreview();
}
}
}
Expand Down

0 comments on commit fcb5373

Please sign in to comment.