Android Camera系列开发 (三): 通过CameraAPI拍照
作者:雨水 2013-8-21 CSDN博客:http://blog.csdn.net/gobitan/
概述
使用Camera有两种方式:通过Intent使用已有的app和通过Camera构建自己的app。在开发系列(一) 中已经介绍了通过Intent方式,本文介绍通过CameraAPI的方式拍照。
关键类解析
通过CameraAPI方式拍照需要引入几个关键的类:
Camera类:最主要的类,用于管理Camera设备,本文中主要用到以下方法:
Camera.PictureCallback接口:该回调接口包含了一个onPictureTaken(byte[]data, Camera camera)方法。在这个方法中可以保存图像数据。
SurfaceView类:用于控制预览界面
SurfaceHolder.Callback接口:用于处理预览的事件,需实现如下三个方法:
surfaceCreated(SurfaceHolderholder):预览界面创建时调用,每次界面改变后都会重新创建,需要获取相机资源并设置SurfaceHolder。
surfaceChanged(SurfaceHolderholder, int format, int width, int height):预览界面发生变化时调用,每次界面发生变化之后需要重新启动预览。
surfaceDestroyed(SurfaceHolderholder):预览销毁时调用,停止预览,释放相应资源。
通过Camera方式来实现拍照
通过Camera方式 会比通过Intent方式获得更为丰富的功能。通常创建一个定制化的Camera需要如下步骤:
(1) 通过Camera.open()来获取Camera实例。
(2) 创建Preview类,需要继承SurfaceView类并实现SurfaceHolder.Callback接口。
(3) 为相机设置Preview
(4) 构建一个Preview的Layout来 预览相机;
(5) 为拍照建立Listener以获取拍照后的回调;
(6) 拍照并保存文件;
(7) 释放Camera。
具体步骤
第一步:在Eclipse中创建一个名为AndroidCamera的Android工程,可参见Helloworld的例子;
第二步:在AndroidManifest.xml中添加使用Camera相关的声明如下:
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
第三步:编写AndroidCameraActivity类,类的源代码如下:
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.FrameLayout; public class AndroidCameraActivity extends Activity implements OnClickListener, PictureCallback { private CameraSurfacePreview mCameraSurPreview = null; private Button mCaptureButton = null; private String TAG = "Dennis"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create our Preview view and set it as the content of our activity. FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); mCameraSurPreview = new CameraSurfacePreview(this); preview.addView(mCameraSurPreview); // Add a listener to the Capture button mCaptureButton = (Button) findViewById(R.id.button_capture); mCaptureButton.setOnClickListener(this); } @Override public void onPictureTaken(byte[] data, Camera camera) { //save the picture to sdcard File pictureFile = getOutputMediaFile(); if (pictureFile == null){ Log.d(TAG, "Error creating media file, check storage permissions: "); return; } try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(data); fos.close(); } catch (FileNotFoundException e) { Log.d(TAG, "File not found: " + e.getMessage()); } catch (IOException e) { Log.d(TAG, "Error accessing file: " + e.getMessage()); } // Restart the preview and re-enable the shutter button so that we can take another picture camera.startPreview(); //See if need to enable or not mCaptureButton.setEnabled(true); } @Override public void onClick(View v) { mCaptureButton.setEnabled(false); // get an image from the camera mCameraSurPreview.takePicture(this); } private File getOutputMediaFile(){ //get the mobile Pictures directory File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //get the current time String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); return new File(picDir.getPath() + File.separator + "IMAGE_"+ timeStamp + ".jpg"); } }
第四步:新增CameraSurfacePreview类,源代码如下:
import java.io.IOException; import android.content.Context; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraSurfacePreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraSurfacePreview(Context context) { super(context); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { Log.d("Dennis", "surfaceCreated() is called"); try { // Open the Camera in preview mode mCamera = Camera.open(); mCamera.setPreviewDisplay(holder); } catch (IOException e) { Log.d("Dennis", "Error setting camera preview: " + e.getMessage()); } } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d("Dennis", "surfaceChanged() is called"); try { mCamera.startPreview(); } catch (Exception e){ Log.d("Dennis", "Error starting camera preview: " + e.getMessage()); } } public void surfaceDestroyed(SurfaceHolder holder) { if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } Log.d("Dennis", "surfaceDestroyed() is called"); } public void takePicture(PictureCallback imageCallback) { mCamera.takePicture(null, null, imageCallback); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:id="@+id/camera_preview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <Button android:id="@+id/button_capture" android:text="Capture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
第六步:为AndroidManifest.xml中的acitvity增加如下属性:
android:screenOrientation="landscape"
拍完照之后,可以在SD卡中的Pictures目录下找到保存的照片。
源代码工程下载地址:https://github.com/dennishucd/DennisTech/blob/master/archives/AndroidCamera_20130821.zip
参考资料
1. http://developer.android.com/training/camera/videobasics.html
2. http://developer.android.com/guide/topics/media/camera.html
3. http://developer.android.com/reference/android/hardware/Camera.html