Android Open Source - camera Camera Interface






From Project

Back to project page camera.

License

The source code is released under:

Apache License

If you think the Android project camera listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.yanzi.camera;
//ww  w. j a  v a 2  s .c o m
import java.io.IOException;
import java.util.List;

import org.yanzi.util.CamParaUtil;
import org.yanzi.util.FileUtil;
import org.yanzi.util.ImageUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;

public class CameraInterface {
  private static final String TAG = "yanzi";
  private Camera mCamera;
  private Camera.Parameters mParams;
  private boolean isPreviewing = false;
  private float mPreviwRate = -1f;
  private static CameraInterface mCameraInterface;

  public interface CamOpenOverCallback{
    public void cameraHasOpened();
  }

  private CameraInterface(){

  }
  public static synchronized CameraInterface getInstance(){
    if(mCameraInterface == null){
      mCameraInterface = new CameraInterface();
    }
    return mCameraInterface;
  }
  /**??Camera
   * @param callback
   */
  public void doOpenCamera(CamOpenOverCallback callback){
    Log.i(TAG, "Camera open....");
    mCamera = Camera.open();
    Log.i(TAG, "Camera open over....");
    callback.cameraHasOpened();
  }
  /**???Surfaceview???????
   * @param holder
   * @param previewRate
   */
  public void doStartPreview(SurfaceHolder holder, float previewRate){
    Log.i(TAG, "doStartPreview...");
    if(isPreviewing){
      mCamera.stopPreview();
      return;
    }
    if(mCamera != null){
      try {
        mCamera.setPreviewDisplay(holder);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      initCamera(previewRate);
    }


  }
  /**???TextureView???Camera
   * @param surface
   * @param previewRate
   */
  public void doStartPreview(SurfaceTexture surface, float previewRate){
    Log.i(TAG, "doStartPreview...");
    if(isPreviewing){
      mCamera.stopPreview();
      return;
    }
    if(mCamera != null){
      try {
        mCamera.setPreviewTexture(surface);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      initCamera(previewRate);
    }
    
  }

  /**
   * ??????????Camera
   */
  public void doStopCamera(){
    if(null != mCamera)
    {
      mCamera.setPreviewCallback(null);
      mCamera.stopPreview(); 
      isPreviewing = false; 
      mPreviwRate = -1f;
      mCamera.release();
      mCamera = null;     
    }
  }
  /**
   * ????
   */
  public void doTakePicture(){
    if(isPreviewing && (mCamera != null)){
      mCamera.takePicture(mShutterCallback, null, mJpegPictureCallback);
    }
  }



  private void initCamera(float previewRate){
    if(mCamera != null){

      mParams = mCamera.getParameters();
      mParams.setPictureFormat(PixelFormat.JPEG);//?????????????????
//      CamParaUtil.getInstance().printSupportPictureSize(mParams);
//      CamParaUtil.getInstance().printSupportPreviewSize(mParams);
      //????PreviewSize??PictureSize
      Size pictureSize = CamParaUtil.getInstance().getPropPictureSize(
          mParams.getSupportedPictureSizes(),previewRate, 800);
      mParams.setPictureSize(pictureSize.width, pictureSize.height);
      Size previewSize = CamParaUtil.getInstance().getPropPreviewSize(
          mParams.getSupportedPreviewSizes(), previewRate, 800);
      mParams.setPreviewSize(previewSize.width, previewSize.height);

      mCamera.setDisplayOrientation(90);

//      CamParaUtil.getInstance().printSupportFocusMode(mParams);
      List<String> focusModes = mParams.getSupportedFocusModes();
      if(focusModes.contains("continuous-video")){
        mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
      }
      mCamera.setParameters(mParams);  
      mCamera.startPreview();//???????



      isPreviewing = true;
      mPreviwRate = previewRate;

      mParams = mCamera.getParameters(); //????get???
      Log.i(TAG, "????????:PreviewSize--With = " + mParams.getPreviewSize().width
          + "Height = " + mParams.getPreviewSize().height);
      Log.i(TAG, "????????:PictureSize--With = " + mParams.getPictureSize().width
          + "Height = " + mParams.getPictureSize().height);
    }
  }



  /*????????????????????????????????????????????????*/
  ShutterCallback mShutterCallback = new ShutterCallback() 
  //?????????????????????????????????????????????????????????????
  {
    public void onShutter() {
      // TODO Auto-generated method stub
      Log.i(TAG, "myShutterCallback:onShutter...");
    }
  };
  PictureCallback mRawCallback = new PictureCallback() 
  // ?????????????????,?????null
  {

    public void onPictureTaken(byte[] data, Camera camera) {
      // TODO Auto-generated method stub
      Log.i(TAG, "myRawCallback:onPictureTaken...");

    }
  };
  PictureCallback mJpegPictureCallback = new PictureCallback() 
  //??jpeg??????????,?????????????
  {
    public void onPictureTaken(byte[] data, Camera camera) {
      // TODO Auto-generated method stub
      Log.i(TAG, "myJpegCallback:onPictureTaken...");
      Bitmap b = null;
      if(null != data){
        b = BitmapFactory.decodeByteArray(data, 0, data.length);//data?????????????????????
        mCamera.stopPreview();
        isPreviewing = false;
      }
      //????????sdcard
      if(null != b)
      {
        //????FOCUS_MODE_CONTINUOUS_VIDEO)???myParam.set("rotation", 90)????
        //???????????????????????????
        Bitmap rotaBitmap = ImageUtil.getRotateBitmap(b, 90.0f);
        FileUtil.saveBitmap(rotaBitmap);
      }
      //?????????
      mCamera.startPreview();
      isPreviewing = true;
    }
  };


}




Java Source Code List

cn.bgxt.mediarecorderdemo.MainActivity.java
cn.bgxt.mediarecorderdemo.RecordActivity.java
com.example.android.photobyintent.AlbumStorageDirFactory.java
com.example.android.photobyintent.BaseAlbumDirFactory.java
com.example.android.photobyintent.FroyoAlbumDirFactory.java
com.example.android.photobyintent.PhotoIntentActivity.java
com.marcodinacci.book.acb.VideoActivity.java
org.yanzi.activity.CameraActivity.java
org.yanzi.camera.CameraInterface.java
org.yanzi.camera.preview.CameraTextureView.java
org.yanzi.util.CamParaUtil.java
org.yanzi.util.DisplayUtil.java
org.yanzi.util.FileUtil.java
org.yanzi.util.ImageUtil.java