1、首先在XML中定义一个摄像头预览区域SurfaceView:
<SurfaceView android:id="@+id/mySurfaceView"
android:gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="300px" />
当然下边有按钮进行相机的初始化和拍照在此就省略了,还有一个ImageView用于拍摄好的照片预览
<ImageView android:id="@+id/myImageView"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
2、在Activity中进行应用:
public class MainActivity extends Activity implements Callback, OnClickListener {/** Called when the activity is first created. */
SurfaceView mySurfaceView;// SurfaceView的引用
SurfaceHolder mySurfaceHolder;// SurfaceHolder的引用
Button button1;// 打开按钮
Button button2;// 关闭按钮
Button button3;// 拍照按钮
Camera myCamera;// Camera的引用
boolean isView = false;// 是否在浏览中
@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.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
mySurfaceView = (SurfaceView) findViewById(R.id.mySurfaceView);
mySurfaceHolder = mySurfaceView.getHolder(); //此对象用于在Camera和SurfaceView之间传递数据
mySurfaceHolder.addCallback(this);
mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void initCamera() { //初始化相机
if (!isView) {
myCamera = Camera.open();
}
if (myCamera != null && !isView) {
try {
Camera.Parameters myParameters = myCamera.getParameters();
myParameters.setPictureFormat(PixelFormat.JPEG); //设置照片格式
myParameters.setPreviewSize(320, 480); //大小
myCamera.setParameters(myParameters);
myCamera.setPreviewDisplay(mySurfaceHolder);
myCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isView = true;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==button1){
initCamera();
}else if(v==button2){
isView=false;
myCamera.stopPreview();
myCamera.release();
myCamera=null;
}else if(v==button3){
myCamera.takePicture(mShutterCallback, myRawCallback, myjpegCallback); //进行照相
}
}
ShutterCallback mShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
PictureCallback myRawCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
}
};
PictureCallback myjpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) { //将照下来的图片用ImageView显示
// TODO Auto-generated method stub
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView myImageView=(ImageView) findViewById(R.id.myImageView);
myImageView.setImageBitmap(bm);
isView=false;
myCamera.stopPreview();
myCamera.release();
myCamera=null;
initCamera();
}
};
@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
}
}
3、记得加权限:
<uses-permission android:name="android.permission.CAMERA"></uses-permission>