package gongzibai.co.cc;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Ex2Activity extends Activity implements SurfaceHolder.Callback {
Camera mCamera;
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
Button mButton1,mButton2;
boolean isPreview=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView=(SurfaceView)findViewById(R.id.surfaceView1);
mButton1=(Button)findViewById(R.id.button1);
mButton2=(Button)findViewById(R.id.button2);
mSurfaceHolder=mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initCamera();
}
private void initCamera() {
// TODO Auto-generated method stub
if (!isPreview) {
mCamera=Camera.open();
}
if (mCamera!=null&&!isPreview) {
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
isPreview=true;
}
}
});
mButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCamera!=null&&isPreview) {
mCamera.stopPreview();
mCamera.release();
mCamera=null;
isPreview=false;
}
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
本文介绍了一个简单的Android应用程序示例,该应用使用摄像头进行实时视频预览,并提供了开始和停止预览的功能。通过实现SurfaceHolder.Callback接口,该程序能够在指定的SurfaceView上显示摄像头画面。
1万+

被折叠的 条评论
为什么被折叠?



