向AndroidManifest文件添加访问摄像头的权限:
<uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SurfaceView android:id="@+id/surfaceView" android:layout_height="0dip" android:layout_width="0dip"></SurfaceView> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView"></ImageView> </LinearLayout>
活动
import java.io.IOException; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.ImageView; public class TakePicture extends Activity implementsSurfaceHolder.Callback { //一个变量,用于在main.xml文件中存储对“图像视图”的引用 private ImageView iv_image; //用于在main.xml文件中存储对Surface视图的引用的变量 private SurfaceView sv; //位图以显示捕获的图像 private Bitmap bmp; //相机变量 //表面支架 private SurfaceHolder sHolder; //控制相机的变量 private Camera mCamera; //相机参数 private Parameters parameters; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //在main.xml文件中获取图像视图 iv_image = (ImageView) findViewById(R.id.imageView); //在main.xml文件中获取Surface视图 sv = (SurfaceView) findViewById(R.id.surfaceView); //取得表面 sHolder = sv.getHolder(); //将下面定义的回调接口方法添加为Surface View回调 sHolder.addCallback(this); //告诉Android,该表面会不断替换其数据 sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { //获取相机参数 parameters = mCamera.getParameters(); //设置相机参数 mCamera.setParameters(parameters); mCamera.startPreview(); //设置拍摄照片后应执行的代码 Camera.PictureCallbackmCall = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { //将摄像机获取的数据解码为位图 bmp = BitmapFactory.decodeByteArray(data, 0, data.length); String filename=Environment.getExternalStorageDirectory() +File.separator+ "testimage.jpg"; FileOutputStream out = null; try { out = new FileOutputStream(filename); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp是您的Bitmap实例 // PNG是一种无损格式,压缩因子(100)被忽略 } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } //设置iv_image iv_image.setImageBitmap(bmp); } }; mCamera.takePicture(null, null, mCall); } @Override public void surfaceCreated(SurfaceHolder holder) { // Surface已创建,获取相机并告诉它在哪里 // 绘制预览。 mCamera = Camera.open(); try { mCamera.setPreviewDisplay(holder); } catch (IOException exception) { mCamera.release(); mCamera = null; } } @Override public void surfaceDestroyed(SurfaceHolder holder) { //停止预览 mCamera.stopPreview(); //释放相机 mCamera.release(); //将相机与该对象解除绑定 mCamera = null; } }