安卓自定义伪全屏CameraView

一直想知道微信那个全屏相机是怎么实现的,网上我也搜了很多博客,都不能准确的还原,主要是在全面屏上,会拉伸变形。那全屏实现不了,那就实现伪全屏吧。什么是伪全屏?就是寻找最合适的比例显示相机画面。

先看看效果,在普通的手机上,小米6


可以看到是没有问题的,因为小米6的相机支持的预览尺寸有16:9,而他的屏幕也是16:9 ,所以是刚好可以全屏显示。

下面再来看看小米9,全面屏手机


可以看到,相机的画面没有变形,但是底部却留了一部分黑色。为什么呢?因为小米9支持的预览尺寸是16:9,但屏幕却是18:9,背景设置为黑色,底部就会留有一块黑色,但貌似影响不是很大,因为平时原生的相机,也是有一块黑色的,也是不会全屏。
然后,在三星S9上面,相机是支持18:9的尺寸的,所以,在三星上会完美全屏显示。

实现原理

1、自定义一个view,继承SurfaceView,获取这个view的宽高比

            float widthSize = getMeasuredWidth();
            float heightSize = getMeasuredHeight();
            float screenProp = heightSize / widthSize;

2、然后获取相机支持的预览尺寸previewSize和拍摄尺寸pictureSize,先进行尺寸的排序,从小到大排列。然后循环进行比较,如果宽度大于设置的最小宽度(建议1000以上,不然图片尺寸过小会模糊),并且宽高比和view的宽高比最接近的,那我们就拿这个尺寸来显示。

    private Camera.Size getBestSize(List list, int th, float rate) {
        Collections.sort(list, sizeComparator);
        float previewDisparity = 100;
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            Camera.Size cur = list.get(i);
            float prop = (float) cur.width / (float) cur.height;
            if (Math.abs(rate - prop) < previewDisparity && cur.height > th) {
                previewDisparity = Math.abs(rate - prop);
                index = i;
            }
        }
        Log.i("---", "MakeSure Picture and preview :w = " + list.get(index).width + " h = " + list.get(index).height);
        return list.get(index);
    }

3、设置完尺寸,接下来,就是改变这个view的高度

            float prevewProp = (float) previewSize.width / (float) previewSize.height;
            final ViewGroup.LayoutParams params = getLayoutParams();
            params.height = (int) (widthSize * prevewProp);
            post(new Runnable() {
                @Override
                public void run() {
                    setLayoutParams(params);
                }
            });

4、主要就是以上步骤,其他的拍照,看看下面全部代码吧。

package com.linghit.aicamera.lib;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static android.graphics.Bitmap.createBitmap;

/**
 * DATE : 2019-06-24
 * NAME : 景天
 * 伪全屏相机
 */
public class FullCameraView extends SurfaceView {

    private Camera camera;
    //默认开启后置摄像头
    private boolean isBackCamera = true;
    private CameraSizeComparator sizeComparator = new CameraSizeComparator();

    public FullCameraView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        SurfaceHolder mSurfaceHolder = getHolder();
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
        mSurfaceHolder.setKeepScreenOn(true);

        mSurfaceHolder.addCallback(mSurfaceCallback);

    }

    public void takePhoto(final TakePhotoCallback takePhotoCallback) {
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                //进行拍照操作
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                Matrix matrix = new Matrix();
                if (isBackCamera) {
                    matrix.setRotate(90);
                } else {
                    matrix.setRotate(270);
                    //前置摄像头,镜像水平翻转
                    matrix.postScale(-1, 1);
                }

                bitmap = createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

                if (takePhotoCallback != null) {
                    takePhotoCallback.onFinish(bitmap);
                }
            }
        });
    }

    public void onDestroy() {
        if (camera != null) {
            camera.release();
        }
    }

    /**
     * 切换前后摄像头
     */
    public void changeCamera() {
        int cameraCount = 0;
        //得到摄像头的个数,大于1才能切换
        cameraCount = Camera.getNumberOfCameras();
        if (cameraCount > 1) {
            camera.setPreviewCallback(null);
            camera.stopPreview();
            camera.release();
            camera = null;

            isBackCamera = !isBackCamera;
            openCamera(getHolder());
        }
    }


    public interface TakePhotoCallback {
        void onFinish(Bitmap bitmap);
    }

    private SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            openCamera(surfaceHolder);
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            if (camera != null) {
                camera.stopPreview();
                camera.release();
            }
        }
    };

    private void openCamera(SurfaceHolder surfaceHolder) {
        try {
            if (isBackCamera) {
                camera = Camera.open(0);
            } else {
                camera = Camera.open(1);
            }
            final Camera.Parameters parameters = camera.getParameters();
            parameters.setJpegQuality(100);

            float widthSize = getMeasuredWidth();
            float heightSize = getMeasuredHeight();
            float screenProp = heightSize / widthSize;
            //设置尺寸,第二个参数为最小尺寸,为了图片清晰度,所以要给一个最小的尺寸
            Camera.Size previewSize = getBestSize(parameters.getSupportedPreviewSizes(), 1000, screenProp);
            Camera.Size pictureSize = getBestSize(parameters.getSupportedPictureSizes(), 1400, screenProp);

            parameters.setPreviewSize(previewSize.width, previewSize.height);
            parameters.setPictureSize(pictureSize.width, pictureSize.height);

            List focusModes = parameters.getSupportedFocusModes();
            if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
            }

            camera.setParameters(parameters);

            camera.setDisplayOrientation(90);
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();

            float prevewProp = (float) previewSize.width / (float) previewSize.height;

            final ViewGroup.LayoutParams params = getLayoutParams();
            params.height = (int) (widthSize * prevewProp);
            post(new Runnable() {
                @Override
                public void run() {
                    setLayoutParams(params);
                }
            });
        } catch (Exception e) {
            Log.e("surfaceCreated", e.toString());
        }
    }


    private Camera.Size getBestSize(List list, int th, float rate) {
        Collections.sort(list, sizeComparator);
        float previewDisparity = 100;
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            Camera.Size cur = list.get(i);
            float prop = (float) cur.width / (float) cur.height;
            if (Math.abs(rate - prop) < previewDisparity && cur.height > th) {
                previewDisparity = Math.abs(rate - prop);
                index = i;
            }
        }
        Log.i("---", "MakeSure Picture and preview :w = " + list.get(index).width + " h = " + list.get(index).height);
        return list.get(index);
    }


    private class CameraSizeComparator implements Comparator {
        public int compare(Camera.Size lhs, Camera.Size rhs) {
            if (lhs.width == rhs.width) {
                return 0;
            } else if (lhs.width > rhs.width) {
                return 1;
            } else {
                return -1;
            }
        }
    }
}

你可能感兴趣的:(安卓自定义伪全屏CameraView)