Android Cameraはリボンを使用しています。

7657 ワード

 Android   camera           ,          ,   http://mobile.tutsplus.com/tutorials/android/android-essentials-create-a-mirror/ 

1) ,
  


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <FrameLayout
        android:id="@+id/camPreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </FrameLayout>
    <Button
        android:id="@+id/capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/capture" />
</RelativeLayout>

2 ヘッドの び しを し、そのoncreateイベントでは、 のコードがあります。
  

private Camera mCam;
private MirrorView mCamPreview;
private int mCameraId = 0;
private FrameLayout mPreviewLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

mCameraId = findFirstFrontFacingCamera();

	mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
	mPreviewLayout.removeAllViews();

	startCameraInLayout(mPreviewLayout, mCameraId);

}

   ここmCamera Id=findFirst Front FacingCamera()
まず の ヘッドを し して、findFirst Front FacingCamera()の があります。コードは の りです。
  


 private int findFirstFrontFacingCamera() {
        int foundId = -1;
        // find the first front facing camera
        int numCams = Camera.getNumberOfCameras();
        for (int camId = 0; camId < numCams; camId++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(camId, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d(DEBUG_TAG, "Found front facing camera");
                foundId = camId;
                break;
            }
        }
        return foundId;
    }

  は してその ヘッドを し します。
3 にstartCameraa InLayout(mPreview Layout、mCamera Id);
この きカメラを けて、キャプチャーされた を の に いてください。コードは の りです。
 


private void startCameraInLayout(FrameLayout layout, int cameraId) {   
  mCam = Camera.open(cameraId);    
 if (mCam != null) {
         mCamPreview = new MirrorView(this, mCam);         layout.addView(mCamPreview);
     }

 }
4 のポイントはMirrorView です。
     このクラスは はSurfaceView を しています。これはつまりcameraに Surfaceを かせます。

       
 public class MirrorView extends SurfaceView implements
            SurfaceHolder.Callback {
        private SurfaceHolder mHolder;
        private Camera mCamera;

        public MirrorView(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 (Exception error) {
                Log.d(DEBUG_TAG,
                        "Error starting mPreviewLayout: " + error.getMessage());
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w,
                int h) {
            if (mHolder.getSurface() == null) {
                return;
            }

            // can't make changes while mPreviewLayout is active
            try {
                mCamera.stopPreview();
            } catch (Exception e) {

            }

            try {
                // set rotation to match device orientation
                setCameraDisplayOrientationAndSize();

                // start up the mPreviewLayout
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();

            } catch (Exception error) {
                Log.d(DEBUG_TAG,
                        "Error starting mPreviewLayout: " + error.getMessage());
            }
        }

5 に の を に します。
   


        public void setCameraDisplayOrientationAndSize() {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(mCameraId, info);
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            int degrees = rotation * 90;

            /*
             * // the above is just a shorter way of doing this, but could break
             * if the values change switch (rotation) { case Surface.ROTATION_0:
             * degrees = 0; break; case Surface.ROTATION_90: degrees = 90;
             * break; case Surface.ROTATION_180: degrees = 180; break; case
             * Surface.ROTATION_270: degrees = 270; break; }
             */

            int result;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360;
            } else {
                result = (info.orientation - degrees + 360) % 360;
            }
            mCamera.setDisplayOrientation(result);

            Camera.Size previewSize = mCam.getParameters().getPreviewSize();
            if (result == 90 || result == 270) {
                // swap - the physical camera itself doesn't rotate in relation
                // to the screen ;)
                mHolder.setFixedSize(previewSize.height, previewSize.width);
            } else {
                mHolder.setFixedSize(previewSize.width, previewSize.height);

            }
        }
6 にいくつかの をします。それぞれonreumeとonpauseのイベントです。
 

@Override
    protected void onResume() {
        super.onResume();
        if (mCam == null && mPreviewLayout != null) {
            mPreviewLayout.removeAllViews();
            startCameraInLayout(mPreviewLayout, mCameraId);
        }
    }

    @Override
    protected void onPause() {
        if (mCam != null) {
            mCam.release();
            mCam = null;
        }
        super.onPause();
    }
  なコードはまた、カメラヘッドがあるかどうかを することと、 を ってファイルに することと、 コードは ファイルを してください。