//main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
//MainActivity如下:
package com.bravesoft;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
/**
* 注意权限
* <uses-permission android:name="android.permission.CAMERA"/>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
* @author Administrator
*
*/
public class TestCameraActivity extends Activity {
private SurfaceView surfaceView;
private Camera camera;
private boolean isPriview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去掉标题栏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 全屏幕显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏幕显示
setContentView(R.layout.main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144);// 设置分辨率
surfaceView.getHolder().addCallback(new SurfaceHolderCallbackImpl());// SurfaceView的回调
}
class SurfaceHolderCallbackImpl implements SurfaceHolder.Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
WindowManager windowManager = (WindowManager) TestCameraActivity.this.getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
camera = Camera.open();// 打开摄像头
Parameters params = camera.getParameters();
params.setPreviewSize(display.getWidth(), display.getHeight());// 设置预览照片的大小
params.setPreviewFrameRate(5);// 摄像头每秒捕获的照片数量.此为每秒钟5个画面
params.setPictureFormat(PixelFormat.JPEG);// 设置照片的输出格式
params.set("jpeg-quality", 85);// 设置照片质量
params.setPictureSize(display.getWidth(), display.getHeight());// 设置照片的大小
try {
camera.setParameters(params);// 摄像头设置参数
camera.setPreviewDisplay(holder);// 设置摄像头使用的SurfaceView
camera.startPreview();// 摄像头开启预览
isPriview = true;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
if (isPriview) {// 如果在预览
camera.stopPreview();// 停止预览
isPriview = false;
}
camera.release();// 释放掉摄像头
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {// 重写Activity的方法
if (camera != null && event.getRepeatCount() == 0) {// event.getRepeatCount()按键的重复次数.重复次数=0,表示只按了一下
switch (keyCode) {
// case KeyEvent.KEYCODE_SEARCH://搜索按键
// camera.autoFocus(null);//自动对焦.此为自动对焦的回调方法.可以为空
// return true;
case KeyEvent.KEYCODE_CAMERA:// 拍照键
case KeyEvent.KEYCODE_SEARCH:// SEARCH键.按下这两个键的时候执行拍照
camera.takePicture(null, null, null, new PictureCallbackImpl());// 拍照.且拍照后调用TakePictureCallback回调函数来处理照片
return true;
}
}
return super.onKeyDown(keyCode, event);
}
class PictureCallbackImpl implements PictureCallback {// 拍照后的回调处理
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, outputStream);// 100是无损压缩
outputStream.close();
camera.startPreview();// 拍照后,摄像头再次开始预览.否则出现屏幕不动的情况
} catch (Exception e) {
e.printStackTrace();
}
}
}
}