利用Android手机里的摄像头进行拍照
2013-09-09 17:27
477 查看
-------
源自梦想、永远是你IT事业的好友、只是勇敢地说出我学到! ----------
1.在API Guides中找到Camera,里面讲解了如何使用系统自带的摄像头进行工作,之后我会试着翻译这部分的内容。
2.找到Camera类:有android.hardware.Camera和android.graphics.Camera两个类,我们这里使用android.hardware.Camera。
使用Camera类来拍照的步骤如下(API 原文):
Obtain an instance of Camera from
Get existing (default) settings with
If necessary, modify the returned
and call
If desired, call
Important: Pass a fully initialized
Without a surface, the camera will be unable to start the preview.
Important: Call
start updating the preview surface. Preview must be started before you can take a picture.
When you want, call
After taking a picture, preview display will have stopped. To take more photos, call
first.
Call
stop updating the preview surface.
Important: Call
release the camera for use by other applications. Applications should release the camera immediately in
re-
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.如果有必要的话,我们可以调用
6.然后我们调用startPreView()方法开始预览取景。然后就可以拍照了。
7.调用Camera的
8.当拍完一张照片之后,预览显示就会关掉。如果我们还想拍些照片,就需要重新调用startPreview();
9.结束程序时,要调用Camera的stopPreview()来结束取景预览。
10.重要:为了能让其他应用能够使用摄像头,我们要调用release()方法来释放摄像头。当Activity在onPause()的时候,释放掉;当onResume()的时候,重新打开。
-------
源自梦想、永远是你IT事业的好友、只是勇敢地说出我学到! ----------
源自梦想、永远是你IT事业的好友、只是勇敢地说出我学到! ----------
1.在API Guides中找到Camera,里面讲解了如何使用系统自带的摄像头进行工作,之后我会试着翻译这部分的内容。
2.找到Camera类:有android.hardware.Camera和android.graphics.Camera两个类,我们这里使用android.hardware.Camera。
使用Camera类来拍照的步骤如下(API 原文):
Obtain an instance of Camera from
open(int
).
Get existing (default) settings with
getParameters().
If necessary, modify the returned
Camera.Parametersobject
and call
setParameters(Camera.Parameters).
If desired, call
setDisplayOrientation(int).
Important: Pass a fully initialized
SurfaceHolderto
setPreviewDisplay(SurfaceHolder).
Without a surface, the camera will be unable to start the preview.
Important: Call
startPreview()to
start updating the preview surface. Preview must be started before you can take a picture.
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.
After taking a picture, preview display will have stopped. To take more photos, call
startPreview()again
first.
Call
stopPreview()to
stop updating the preview surface.
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.我们传一个已经初始化完的SurfaceHolder给setPreviewDisplay(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 |
9.结束程序时,要调用Camera的stopPreview()来结束取景预览。
10.重要:为了能让其他应用能够使用摄像头,我们要调用release()方法来释放摄像头。当Activity在onPause()的时候,释放掉;当onResume()的时候,重新打开。
-------
源自梦想、永远是你IT事业的好友、只是勇敢地说出我学到! ----------
相关文章推荐
- 利用Android手机里的摄像头进行拍照
- 利用Android手机里的摄像头进行拍照
- 微信浏览器内兼容android iOS调取手机摄像头进行拍照、打水印、压缩、预览
- Android应用中调用手机摄像头拍照并保存
- android 调用前摄像头进行拍照的方法及完整例子
- android中利用Socket实现手机客户端与PC端进行通信
- android 调用摄像头进行拍照,并对照片进行裁剪,然后显示。
- Android实现调用摄像头进行拍照功能
- 在android的同一个wifi局域网下,利用socket与多个手机进行数据的收发
- 使用手机的摄像头进行拍照
- Mac系统下利用ADB命令连接android手机并进行文件操作
- 从零开始打造Android手机摄像头拍照程序
- android调用手机摄像头拍照
- Android调用手机摄像头拍照和录音功能
- 调用Android系统摄像头进行拍照以及从图库选择图片
- Mac系统下利用ADB命令连接android手机并进行文件操作
- Mac系统下利用ADB命令连接android手机并进行文件操作
- android 使用相机拍照,并存储到手机sd卡上,并利用系统录像录像并播放