在sdk中找到/sdk/docs/guide/topics/media/camera.html#custom-camera,里面有详细的api参考
在清单文件中添加相应的权限:
XML/HTML代码
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-feature android:name="android.hardware.camera" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
按照官方文档,分为下面几步:
• Detect and Access Camera - Create code to check for the existence of cameras and request access.
• Create a Preview Class - Create a camera preview class that extends SurfaceView and implements theSurfaceHolder interface. This class previews the live images from the camera.
• Build a Preview Layout - Once you have the camera preview class, create a view layout that incorporates the preview and the user interface controls you want.
• Setup Listeners for Capture - Connect listeners for your interface controls to start image or video capture in response to user actions, such as pressing a button.
• Capture and Save Files - Setup the code for capturing pictures or videos and saving the output.
• Release the Camera - After using the camera, your application must properly release it for use by other applications.
接下来分别实现:
1、检查设备是否有照相机
Java代码
-
- private boolean checkCameraHardware(Context context) {
- if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
-
- return true;
- } else {
-
- return false;
- }
- }
2、得到一个照相机
Java代码
-
- public static Camera getCameraInstance(){
- Camera c = null;
- try {
- c = Camera.open();
- }
- catch (Exception e){
-
- }
- return c;
- }
3、新建一个名为CameraPreview的类
Java代码
- package com.wuyudong.mycamera;
-
- import java.io.IOException;
-
- import android.content.Context;
- import android.hardware.Camera;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
-
-
- public class CameraPreview extends SurfaceView implements
- SurfaceHolder.Callback {
- private static final String TAG = "CameraPreview";
- private SurfaceHolder mHolder;
- private Camera mCamera;
-
- public CameraPreview(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 (IOException e) {
- Log.d(TAG, "Error setting camera preview: " + e.getMessage());
- }
- }
-
- public void surfaceDestroyed(SurfaceHolder holder) {
-
- }
-
- public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
-
-
-
- if (mHolder.getSurface() == null) {
-
- return;
- }
-
-
- try {
- mCamera.stopPreview();
- } catch (Exception e) {
-
- }
-
-
-
-
-
- try {
- mCamera.setPreviewDisplay(mHolder);
- mCamera.startPreview();
-
- } catch (Exception e) {
- Log.d(TAG, "Error starting camera preview: " + e.getMessage());
- }
- }
- }
4、设置一个预览功能的layout,将原来布局文件中的内容替换成下面的代码
XML/HTML代码
- <?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、在清单文件中加入 android:screenOrientation="landscape" 调整相机为横向拍摄
6、在MainActivity中添加
Java代码
- public class MainActivity extends Activity {
-
- private Camera mCamera;
- private CameraPreview mPreview;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- mCamera = getCameraInstance();
-
-
- mPreview = new CameraPreview(this, mCamera);
- FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
- preview.addView(mPreview);
- }
- }
7、实现拍摄按钮的功能
(1)添加拍照回调方法
Java代码
- private PictureCallback mPicture = new PictureCallback() {
-
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
-
- File pictureFile = new File("/sdcard/" + System.currentTimeMillis()+".jpg");
- try {
- FileOutputStream fos = new FileOutputStream(pictureFile);
- fos.write(data);
- fos.close();
- } catch (IOException e) {
- Log.d("TAG", "Error accessing file: " + e.getMessage());
- }
- }
- };
给拍照按钮添加注册事件:
Java代码
-
- Button captureButton = (Button) findViewById(R.id.button_capture);
- captureButton.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
-
- mCamera.takePicture(null, null, mPicture);
- }
- }
- );
完整的代码如下: