Android 中的camera的使用是个很普遍的用法,今天小结下其使用步骤,翻译自http://mobile.tutsplus.com/tutorials/android/android-essentials-create-a-mirror/ 
1) 设计界面,如下
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <FrameLayout        android:id="@+id/camPreview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true" >    </FrameLayout>    <Button        android:id="@+id/capture"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="@string/capture" /></RelativeLayout>


2 开始调用摄象头,在其oncreate事件中,如下代码:
private Camera mCam;private MirrorView mCamPreview;private int mCameraId = 0;private FrameLayout mPreviewLayout;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mCameraId = findFirstFrontFacingCamera();mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);mPreviewLayout.removeAllViews();startCameraInLayout(mPreviewLayout, mCameraId);}


这里mCameraId = findFirstFrontFacingCamera();
首先找出前置摄象头,有个findFirstFrontFacingCamera()的方法,代码如下:
 private int findFirstFrontFacingCamera() {        int foundId = -1;        // find the first front facing camera        int numCams = Camera.getNumberOfCameras();        for (int camId = 0; camId < numCams; camId++) {            CameraInfo info = new CameraInfo();            Camera.getCameraInfo(camId, info);            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {                Log.d(DEBUG_TAG, "Found front facing camera");                foundId = camId;                break;            }        }        return foundId;    }

其实就是一个循环找出其前置摄象头



3 接下来startCameraInLayout(mPreviewLayout, mCameraId);
就是打开这个前置摄象头,将捕捉到的图象放到屏幕的界面上来,代码如下:
private void startCameraInLayout(FrameLayout layout, int cameraId) {     mCam = Camera.open(cameraId);     if (mCam != null) {         mCamPreview = new MirrorView(this, mCam);         layout.addView(mCamPreview);     } }


4 下面重点来看MirrorView类
这个类其实是继承了SurfaceView类,如下,这个其实就是让camera直接写surface了

        public class MirrorView extends SurfaceView implements            SurfaceHolder.Callback {        private SurfaceHolder mHolder;        private Camera mCamera;        public MirrorView(Context context, Camera camera) {            super(context);            mCamera = camera;            mHolder = getHolder();            mHolder.addCallback(this);            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        }        public void surfaceCreated(SurfaceHolder holder) {            try {                mCamera.setPreviewDisplay(holder);                mCamera.startPreview();            } catch (Exception error) {                Log.d(DEBUG_TAG,                        "Error starting mPreviewLayout: " + error.getMessage());            }        }        public void surfaceDestroyed(SurfaceHolder holder) {        }        public void surfaceChanged(SurfaceHolder holder, int format, int w,                int h) {            if (mHolder.getSurface() == null) {                return;            }            // can't make changes while mPreviewLayout is active            try {                mCamera.stopPreview();            } catch (Exception e) {            }            try {                // set rotation to match device orientation                setCameraDisplayOrientationAndSize();                // start up the mPreviewLayout                mCamera.setPreviewDisplay(mHolder);                mCamera.startPreview();            } catch (Exception error) {                Log.d(DEBUG_TAG,                        "Error starting mPreviewLayout: " + error.getMessage());            }        }



5 最后还要适当调整下图片的比例:

        public void setCameraDisplayOrientationAndSize() {            CameraInfo info = new CameraInfo();            Camera.getCameraInfo(mCameraId, info);            int rotation = getWindowManager().getDefaultDisplay().getRotation();            int degrees = rotation * 90;            /*             * // the above is just a shorter way of doing this, but could break             * if the values change switch (rotation) { case Surface.ROTATION_0:             * degrees = 0; break; case Surface.ROTATION_90: degrees = 90;             * break; case Surface.ROTATION_180: degrees = 180; break; case             * Surface.ROTATION_270: degrees = 270; break; }             */            int result;            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {                result = (info.orientation + degrees) % 360;                result = (360 - result) % 360;            } else {                result = (info.orientation - degrees + 360) % 360;            }            mCamera.setDisplayOrientation(result);            Camera.Size previewSize = mCam.getParameters().getPreviewSize();            if (result == 90 || result == 270) {                // swap - the physical camera itself doesn't rotate in relation                // to the screen ;)                mHolder.setFixedSize(previewSize.height, previewSize.width);            } else {                mHolder.setFixedSize(previewSize.width, previewSize.height);            }        }


6 最后要做些清理工作,分别为onresume和onpause事件

@Override    protected void onResume() {        super.onResume();        if (mCam == null && mPreviewLayout != null) {            mPreviewLayout.removeAllViews();            startCameraInLayout(mPreviewLayout, mCameraId);        }    }    @Override    protected void onPause() {        if (mCam != null) {            mCam.release();            mCam = null;        }        super.onPause();    }

完整的代码还包括判断是否有摄象头,以及拍下照片保存到文件中去,详细代码见附件,

更多相关文章

  1. Android中的visibility属性的区别
  2. android 字体&颜色
  3. Android(安卓)Notification的使用
  4. android 用代码画虚线边框背景
  5. Android(安卓)Camera使用小结
  6. android 网络之 httppost
  7. 使用反射调用android API中的hide方法
  8. android webview 底层实现的逻辑
  9. 在eclipse中查看Android(安卓)SDK源代码

随机推荐

  1. SQL SERVER提交事务回滚机制
  2. SQL Server中使用判断语句(IF ELSE/CASE W
  3. 程序员最实用的 SQL 语句收藏,看完这篇就
  4. 在SQL中该如何处理NULL值
  5. SQL Server 2014 数据库中文版安装图文教
  6. SQL 窗口函数实现高效分页查询的案例分析
  7. mybatis调用sqlserver存储过程返回结果集
  8. 仅用一句SQL更新整张表的涨跌幅、涨跌率
  9. jdbc使用PreparedStatement批量插入数据
  10. SQL Server2019数据库之简单子查询的具有