Android摄像头自定义相机拍照流程

最近完善了一下相机的app,除了拍照功能,现在增加了开启闪光灯,切换前后置摄像头,相册功能,图片删除,图片分享功能,设置照片大小,以及照片的质量,视频方面,有录制视频,设置视频录制大小,本应用拍摄的视频预览,以及视频播放界面。

下面是应用的一些截图,由于项目是自己开发,没有美工优化界面,所以界面比较难看,另外附上github的地址:代码方面并没有做过多的优化,只是供新人学习相机开发流程,大神勿喷:

github : https://github.com/jingxiongdi/CameraJXD

应用部分界面截图:

Android摄像头自定义相机拍照流程_第1张图片Android摄像头自定义相机拍照流程_第2张图片Android摄像头自定义相机拍照流程_第3张图片

Android摄像头自定义相机拍照流程_第4张图片

记录一下Android摄像头拍照的问题:

前言:权限问题,一定别忘记

    
    
    

    
    

1.摄像头开启:0为后置,1为前置

判断是否有前后置摄像头

由于getNumberOfCameras以及getCameraInfo均为API 9 引入,所以方法只适用于2.3及其以上

private boolean checkCamera(final int facing) {
     if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
         return false;
     }
     final int cameraCount = Camera.getNumberOfCameras();
     CameraInfo info = new CameraInfo();
     for (int i = 0; i < cameraCount; i++) {
         Camera.getCameraInfo(i, info);
         if (facing == info.facing) {
             return true;
         }
     }
     return false;
 }
 
public static boolean hasBackFacingCamera() {
     final int CAMERA_FACING_BACK = 0;
     return checkCameraFacing(CAMERA_FACING_BACK);
 }
 
public static boolean hasFrontFacingCamera() {
     final int CAMERA_FACING_FRONT = 1;
     return checkCameraFacing(CAMERA_FACING_FRONT);
 }
 
public static int getSdkVersion() {
     return android.os.Build.VERSION.SDK_INT;
 }
 
2.开启摄像头 0为后置,1为前置
mCamera = Camera.open(0);
3.设置预览旋转
public void setCameraDisplayOrientation ( Activity activity , int cameraId , android.hardware.Camera camera )
    { android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo ( cameraId , info );
        int rotation = activity.getWindowManager ().getDefaultDisplay ().getRotation ();
        int degrees = 0 ;
        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 = 0;
        if ( info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT )
        {
            result = ( info.orientation + degrees ) % 360 ;
            result = ( 360 - result ) % 360 ; // compensate the mirror
        }
        else
        {
         // back-facing
             result = ( info.orientation - degrees + 360 ) % 360 ;
        }
        camera.setDisplayOrientation ( result );
    }

4.获取和设置相机参数:

   try {
                Camera.Parameters parameters = mCamera.getParameters();
//                List supportedPreviewSizes = parameters.getSupportedPreviewSizes();
//                for(int i = 0; i supportedVideoSize =  parameters.getSupportedVideoSizes();
                for(int i = 0; i supportedJpegThumbnailSizes = parameters.getSupportedJpegThumbnailSizes();
//                for(int i = 0; i supportedPictureSizes =  parameters.getSupportedPictureSizes();
                    for(int i = 0; i supportedPreviewFpsRange = parameters.getSupportedPreviewFpsRange();
//                for(int i = 0; i

5.开启预览

                //通过SurfaceView显示预览
                mCamera.setPreviewDisplay(mHolder);
                //开始预览
                mCamera.startPreview();

6.点击拍照以及设置图片旋转

 private void takePhoto() {
        if (mCamera == null ) {
            return;
        }
       // mCamera.stopPreview();
        Log.d("jxd","takePhoto ");
        mCamera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                new SavePictureTask().execute(data);
               // camera.startPreview();
            }
        });
    }

    // save pic
    class SavePictureTask extends AsyncTask {
        @Override
        protected String doInBackground(byte[]... params) {
            String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg";

            Log.i("jxd", "fname="+fname+";dir="+ Environment.getExternalStorageDirectory());
            //picture = new File(Environment.getExternalStorageDirectory(),fname);// create file

            File picture = new File(Environment.getExternalStorageDirectory()+"/CameraJXD/"+fname);

            try {
                FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream
                fos.write(params[0]); // Written to the file
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            /**
             * 解决照片旋转90度的问题
             */
            Bitmap bitmap = BitmapFactory.decodeFile(picture.getPath());
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                    matrix, true);
            FileOutputStream fOut = null;
            try {
                fOut = new FileOutputStream(Environment.getExternalStorageDirectory()+"/CameraJXD/"+"rotate"+fname);
                dstbmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {

            }
            picture.delete();
            return null;
        }
    }
7.自定义相机就完成了,可以随意设置照片的大小和质量,这也是我一直以来想要的功能,之前一直没有去弄这个,网上又没有找到现成的可以随意定义这么多参数的Android相机,所以自己弄一个玩玩。

你可能感兴趣的:(移动开发,Android)