亲宝软件园·资讯

展开

Android拍照功能 Android开发实现拍照功能的方法实例解析

CharlinGod 人气:0
想了解Android开发实现拍照功能的方法实例解析的相关内容吗,CharlinGod在本文为您仔细讲解Android拍照功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,拍照功能,下面大家一起来学习吧。

本文实例讲述了Android开发实现拍照功能的方法。分享给大家供大家参考,具体如下:

解析:

1)判断是否有摄像头checkCameraHardware(this)
2)获得相机camera = Camera.open(0);
3)把相机添加到mPreView = new SurfacePreView(this, mCamera);
4)实现拍照 mCamera.autoFocus
5)在拍照后使用mCamera.takePicture(null, null, mPicture);方法把图片保存到sdcard

代码实现:

/**
 * 使用照相机拍照
 * @author chen.lin
 *
 */
public class CameraActivity extends Activity {
  private Camera mCamera;//相机
  private SurfacePreView mPreView;//视图
  private FrameLayout mFrameLayout;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_takephoto);
    mFrameLayout = (FrameLayout) findViewById(R.id.framelayout);
    if (checkCameraHardware(this)) {
      mCamera = getCamera();
      mPreView = new SurfacePreView(this, mCamera);
      mFrameLayout.addView(mPreView);
    }
  }
  /**
   * 拍照对焦
   * @param view
   */
  public void takephoto(View view){
    mCamera.autoFocus(new AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
        mCamera.takePicture(null, null, mPicture);
      }
    });
  }
  /**
   * 拍照后的图片保存
   */
  private PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
      try {
        File file = new File("/sdcard/file/aa.jpg");
        if (!file.exists()) {
          file.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data);
        fos.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  };
  /**
   * 判断照相机是否存在
   * @param context
   * @return
   */
  private boolean checkCameraHardware(Context context){
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      return true;
    }
    return false; 
  }
  /**
   * 初始化照相机
   * @return
   */
  public static Camera getCamera(){
    Camera camera = null;
    try {
      camera = Camera.open(0);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return camera;
  }
  /**
   * 实现拍照视图
   */
  @SuppressWarnings("deprecation")
  private class SurfacePreView extends SurfaceView implements SurfaceHolder.Callback{
    private Camera mCamera;
    private SurfaceHolder mHolder;
    public SurfacePreView(Context context, Camera camera) {
      super(context);
      mCamera = camera;
      mHolder = getHolder();
      mHolder.addCallback(this);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
      try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      if (holder.getSurface() == null) {
        return;
      }
      try {
        mCamera.stopPreview();
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
      //
      mCamera = null;
    }
  }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <FrameLayout
    android:id="@+id/framelayout"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" >
  </FrameLayout>
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="takephoto"
    android:text="拍照" />
</LinearLayout>

希望本文所述对大家Android程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论