一直想在自己的Android手機上實現一個手機監控攝像頭功能。今天逛論壇,看到一個例子,於是做了出來,留著以後完善。
功能點:
1。Android和PC通過socket通信。
2。Android下Camera的使用。
看代碼:
package com.wenix.androidcameramonitor;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TableLayout;
public class GetIP extends Activity {
String ipname = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
final Builder builder = new AlertDialog.Builder(this); // 定義一個AlertDialog.Builder對象
builder.setTitle("登錄服務器對話框"); // 設置對話框的標題
// 裝載/res/layout/login.xml界面布局
TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(
R.layout.login, null);
final EditText iptext = (EditText) loginForm
.findViewById(R.id.ipedittext);
builder.setView(loginForm); // 設置對話框顯示的View對象
// 為對話框設置一個“登錄”按鈕
builder.setPositiveButton("登錄"
// 為按鈕設置監聽器
, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 此處可執行登錄處理
ipname = iptext.getText().toString().trim();
Bundle data = new Bundle();
data.putString("ipname", ipname);
Intent intent = new Intent(GetIP.this, MainActivity.class);
intent.putExtras(data);
startActivity(intent);
}
});
// 為對話框設置一個“取消”按鈕
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 取消登錄,不做任何事情。
System.exit(1);
}
});
// 創建、並顯示對話框
builder.create().show();
}
}
獲取ip後就跳轉到MainActivity。
package com.wenix.androidcameramonitor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import android.app.Activity;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
SurfaceView sView;
SurfaceHolder surfaceHolder;
int screenWidth, screenHeight;
Camera camera; // 定義系統所用的照相機
boolean isPreview = false; // 是否在浏覽中
private String ipname;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// 獲取IP地址
Intent intent = getIntent();
Bundle data = intent.getExtras();
ipname = data.getString("ipname");
screenWidth = 640;
screenHeight = 480;
sView = (SurfaceView) findViewById(R.id.sView); // 獲取界面中SurfaceView組件
surfaceHolder = sView.getHolder(); // 獲得SurfaceView的SurfaceHolder
// 為surfaceHolder添加一個回調監聽器
surfaceHolder.addCallback(new Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
initCamera(); // 打開攝像頭
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 如果camera不為null ,釋放攝像頭
if (camera != null) {
if (isPreview)
camera.stopPreview();
camera.release();
camera = null;
}
System.exit(0);
}
});
// 設置該SurfaceView自己不維護緩沖
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void initCamera() {
if (!isPreview) {
camera = Camera.open();
}
if (camera != null && !isPreview) {
try {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(screenWidth, screenHeight); // 設置預覽照片的大小
parameters.setPreviewFpsRange(20, 30); // 每秒顯示20~30幀
parameters.setPictureFormat(ImageFormat.NV21); // 設置圖片格式
parameters.setPictureSize(screenWidth, screenHeight); // 設置照片的大小
// camera.setParameters(parameters); // android2.3.3以後不需要此行代碼
camera.setPreviewDisplay(surfaceHolder); // 通過SurfaceView顯示取景畫面
camera.setPreviewCallback(new StreamIt(ipname)); // 設置回調的類
camera.startPreview(); // 開始預覽
camera.autoFocus(null); // 自動對焦
} catch (Exception e) {
e.printStackTrace();
}
isPreview = true;
}
}
}
class StreamIt implements Camera.PreviewCallback {
private String ipname;
public StreamIt(String ipname) {
this.ipname = ipname;
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Size size = camera.getParameters().getPreviewSize();
try {
// 調用image.compressToJpeg()將YUV格式圖像數據data轉為jpg格式
YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
if (image != null) {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, outstream);
outstream.flush();
// 啟用線程將圖像數據發送出去
Thread th = new MyThread(outstream, ipname);
th.start();
}
} catch (Exception ex) {
Log.e("Sys", "Error:" + ex.getMessage());
}
}
}
class MyThread extends Thread {
private byte byteBuffer[] = new byte[1024];
private OutputStream outsocket;
private ByteArrayOutputStream myoutputstream;
private String ipname;
public MyThread(ByteArrayOutputStream myoutputstream, String ipname) {
this.myoutputstream = myoutputstream;
this.ipname = ipname;
try {
myoutputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
// 將圖像數據通過Socket發送出去
Socket tempSocket = new Socket(ipname, 6000);
outsocket = tempSocket.getOutputStream();
ByteArrayInputStream inputstream = new ByteArrayInputStream(myoutputstream.toByteArray());
int amount;
while ((amount = inputstream.read(byteBuffer)) != -1) {
outsocket.write(byteBuffer, 0, amount);
}
myoutputstream.flush();
myoutputstream.close();
tempSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這樣就打開了socket,然後把camera獲取的數據發送到PC端。