Camera类:最主要的类,用于管理Camera设备,本文中主要用到以下方法:
- open():通过open方法获取Camera实例。
- setPreviewDisplay(SurfaceHolder):设置预览拍照
- startPreview():开始预览
- stopPreview():停止预览
- release():释放Camera实例
- takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg):这个是拍照要执行的方法,包含了三个回调参数。Shutter是快门按下时的回调,raw是获取拍照原始数据的回调,jpeg是获取经过压缩成jpg格式的图像数据。在本文中需要实现最后一个回调,参见下面。
Camera.PictureCallback接口:该回调接口包含了一个onPictureTaken(byte[]data, Camera camera)方法。在这个方法中可以保存图像数据。
SurfaceView类:用于控制预览界面
SurfaceHolder.Callback接口:用于处理预览的事件,需实现如下三个方法:
surfaceCreated(SurfaceHolder holder):预览界面创建时调用,每次界面改变后都会重新创建,需要获取相机资源并设置SurfaceHolder。
surfaceChanged(SurfaceHolder holder, int format, int width, int height):预览界面发生变化时调用,每次界面发生变化之后需要重新启动预览。
surfaceDestroyed(SurfaceHolder holder):预览销毁时调用,停止预览,释放相应资源。
通过Camera方式来实现拍照
通过Camera方式 会比通过Intent方式获得更为丰富的功能。通常创建一个定制化的Camera需要如下步骤:
(1) 通过Camera.open()来获取Camera实例;
(2) 创建Preview类,需要继承SurfaceView类并实现SurfaceHolder.Callback接口;
(3) 为相机设置Preview;
(4) 构建一个Preview的Layout来 预览相机;
(5) 为拍照建立Listener以获取拍照后的回调;
(6) 拍照并保存文件;
(7) 释放Camera。
1、在AndroidManifest.xml中添加:
<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" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.dennishucd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<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" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidCameraActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2、AndroidCameraActivity类:
package cn.dennishucd;
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;
import android.widget.Toast;
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();
Toast.makeText(this, "Image has been saved to "+pictureFile.getAbsolutePath(),
Toast.LENGTH_LONG).show();
} 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);
File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
//get the current time
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(picDir.getPath() + File.separator + "IMAGE_"+ timeStamp + ".jpg");
}
}
3、CameraSurfacePreview类:
package cn.dennishucd;
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);
}
}
4、Layout文件:
<?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>
5、拍完照之后,可以在存储卡中的DCIM目录下可以找到保存的照片。