SurfaceView只有当activity显示到了前台,该控件才会被创建,因此需要监听SurfaceView的创建。
1 // SurfaceView只有当activity显示到了前台,该控件才会被创建 因此需要监听surfaceview的创建 2 holder.addCallback(new MySurfaceCallback());
在拍照的时候相机是被占用的,拍照之后需要重新开启预览。
具体拍照的demo:
1 package com.android.hzy.takepicture; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 import android.app.Activity; 8 import android.hardware.Camera; 9 import android.hardware.Camera.Parameters; 10 import android.hardware.Camera.PictureCallback; 11 import android.os.Bundle; 12 import android.os.Environment; 13 import android.util.Log; 14 import android.view.SurfaceHolder; 15 import android.view.SurfaceHolder.Callback; 16 import android.view.SurfaceView; 17 import android.view.View; 18 import android.view.Window; 19 import android.view.WindowManager; 20 21 public class MainActivity extends Activity { 22 23 private SurfaceView surfaceview; 24 private Camera camera; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 requestWindowFeature(Window.FEATURE_NO_TITLE); // 没有标题 必须在设置布局之前找到调用 30 setContentView(R.layout.activity_main); 31 32 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, // 设置全屏显示 33 WindowManager.LayoutParams.FLAG_FULLSCREEN); 34 35 surfaceview = (SurfaceView) findViewById(R.id.surfaceview); 36 SurfaceHolder holder = surfaceview.getHolder(); 37 holder.setFixedSize(176, 155);// 设置分辨率 38 holder.setKeepScreenOn(true); 39 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 40 41 // SurfaceView只有当activity显示到了前台,该控件才会被创建 因此需要监听surfaceview的创建 42 holder.addCallback(new MySurfaceCallback()); 43 44 } 45 46 /** 47 * 监听surfaceview的创建 48 * @author Administrator 49 * Surfaceview只有当activity显示到前台,该空间才会被创建 50 */ 51 private final class MySurfaceCallback implements Callback{ 52 53 @Override 54 public void surfaceChanged(SurfaceHolder holder, int format, int width, 55 int height) { 56 // TODO Auto-generated method stub 57 58 } 59 60 @Override 61 public void surfaceCreated(SurfaceHolder holder) { 62 // TODO Auto-generated method stub 63 64 try { 65 // 当surfaceview创建就去打开相机 66 camera = Camera.open(); 67 Parameters params = camera.getParameters(); 68 Log.i("i", params.flatten()); 69 params.setJpegQuality(80); // 设置照片的质量 70 params.setPictureSize(1024, 768); 71 params.setPreviewFrameRate(5); // 预览帧率 72 camera.setParameters(params); // 将参数设置给相机 73 // 设置预览显示 74 camera.setPreviewDisplay(surfaceview.getHolder()); 75 // 开启预览 76 camera.startPreview(); 77 } catch (IOException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 } 81 82 } 83 84 @Override 85 public void surfaceDestroyed(SurfaceHolder holder) { 86 // TODO Auto-generated method stub 87 if(camera != null){ 88 camera.release(); 89 camera = null; 90 } 91 } 92 93 } 94 95 public void autofoucs(View v){ 96 // 自动对焦由硬件来完成 97 } 98 99 public void takepicture(View v){ 100 /* 101 * shutter:快门被按下 102 * raw:相机所捕获的原始数据 103 * jpeg:相机处理的数据 104 */ 105 camera.takePicture(null, null, new MyPictureCallback()); 106 } 107 108 private final class MyPictureCallback implements PictureCallback{ 109 110 @Override 111 public void onPictureTaken(byte[] data, Camera camera) { 112 // TODO Auto-generated method stub 113 try { 114 File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); 115 FileOutputStream fos = new FileOutputStream(file); 116 fos.write(data); 117 // 在拍照的时候相机是被占用的,拍照之后需要重新预览 118 camera.startPreview(); 119 } catch (Exception e) { 120 // TODO Auto-generated catch block 121 e.printStackTrace(); 122 } 123 124 } 125 126 } 127 128 }