现在的位置: 首页 > 综合 > 正文

利用Android手机里的摄像头进行拍照

2017年12月13日 ⁄ 综合 ⁄ 共 2717字 ⁄ 字号 评论关闭
-------
源自梦想
永远是你IT事业的好友、只是勇敢地说出我学到! ----------


1.在API Guides中找到Camera,里面讲解了如何使用系统自带的摄像头进行工作,之后我会试着翻译这部分的内容。

2.找到Camera类:有android.hardware.Camera和android.graphics.Camera两个类,我们这里使用android.hardware.Camera。

使用Camera类来拍照的步骤如下(API 原文):

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object
    and call setParameters(Camera.Parameters).
  4. If desired, call  setDisplayOrientation(int).
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder).
    Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to
    start updating the preview surface. Preview must be started before you can take a picture.
  7. When you want, call takePicture(Camera.ShutterCallback,
    Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)
     to capture a photo. Wait for the callbacks to provide the actual image data.
  8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again
    first.
  9. Call stopPreview() to
    stop updating the preview surface.
  10. Important: Call release() to
    release the camera for use by other applications. Applications should release the camera immediately in onPause() (and
    re-open() it in onResume()).

1.首先,我们调用Camera类的open(int)方法来获取一个实例,如果是要使用后置摄像头,直接调用open()方法即可,如果想打开指定的摄像头,可为这个方法传递一个int值的摄像头ID参数(cameraID),这个ID的值只能在0到getNumberOfCameras()-1之间,一般0代表后置,1代表前置。我们在调用这个方法时,如果摄像头已经被其他的应用打开,那么就会抛出RuntimeException()。需要注意的是,这个方法可能会使用很长时间才能够完成,所以我们最好开启一个新线程来防止UI线程卡死。Camera
camera = Camera.open(); //开启默认的后置摄像头

2.第二步,我们调用Camera类的getParameters()方法来获取拍照的参数,这个方法返回了一个Camera.Parameters对象。即Camera.Parameters parameters=camera.getParameters();

3.如果有必要的话,我们调用setParameters(Camera.Parameters params)来修改上面所返回的Camera.Parameters对象的拍照参数。设置的方法见Camera.Parameters类,常用的有:设置预览照片的大小setPreviewSize(int width,int height),设置预览照片时每秒显示多少帧的最小值和最大值setPreviewFpsRanges(int min,int max),fps指的是:每秒帧数(frames
per second);

4.如果有必要的话,我们可以调用 setDisplayOrientation(int
degrees)
方法来设置预览照片的方向(顺时针的)。这个方法在肖像模式的应用中很有用,因为前置摄像头拍出来的照片就像是照镜子一样,左右是相反的,这时候就要改变预览的方向了。(这个之后自己做个实例对比)。

下面两步非常重要

5.我们传一个已经初始化完的SurfaceHoldersetPreviewDisplay(SurfaceHolder),来设置使用哪一个SurfaceView来显示取景照片,如果没有这个SurfaceView,就无法预览。

6.然后我们调用startPreView()方法开始预览取景。然后就可以拍照了。

7.调用Camera的 takePicture(Camera.ShutterCallback
shutter, Camera.PictureCallback raw, Camera.PictureCallback postview, Camera.PictureCallback jpeg)
参数如下:

shutter the callback for image capture moment, or null
raw the callback for raw (uncompressed) image data, or null
postview callback with postview image data, may be null
jpeg the callback for JPEG image data, or null

8.当拍完一张照片之后,预览显示就会关掉。如果我们还想拍些照片,就需要重新调用startPreview();

9.结束程序时,要调用Camera的stopPreview()来结束取景预览。

10.重要:为了能让其他应用能够使用摄像头,我们要调用release()方法来释放摄像头。当Activity在onPause()的时候,释放掉;当onResume()的时候,重新打开。



-------
源自梦想
永远是你IT事业的好友、只是勇敢地说出我学到! ----------

抱歉!评论已关闭.