android:相机预览模糊不清

gg58donl  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(428)

我正在尝试使用旧的camera api构建自定义相机,下面的代码可以工作,但预览太模糊:

以下是完整的预览课程:

public class Preview extends ViewGroup implements SurfaceHolder.Callback {
    Camera mCamera;
    SurfaceHolder mHolder;
    Paint mPaint;
    List<Camera.Size> mSupportedPreviewSizes;
    SurfaceView mSurfaceView;
    private Camera.CameraInfo mCameraInfo;
    private int mDisplayOrientation;

    public Preview(Context context) {
        super(context);
    }

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

    public Preview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public Preview(Context context, Camera.CameraInfo mCameraInfo, int mDisplayOrientation) {
        super(context);
        this.mSurfaceView = new SurfaceView(context);
        addView(this.mSurfaceView);
        this.mPaint = new Paint();
        this.mPaint.setColor(Color.WHITE);
        this.mHolder = this.mSurfaceView.getHolder();
        this.mHolder.addCallback(this);
        this.mHolder.setType(3);
        this.mCameraInfo = mCameraInfo;
        this.mDisplayOrientation = mDisplayOrientation;
    }

    public void setCamera(Camera camera) {
        this.mCamera = camera;
        if (this.mCamera != null) {
            this.mSupportedPreviewSizes = this.mCamera.getParameters().getSupportedPreviewSizes();
            requestLayout();
        }
    }

    public void switchCamera(Camera camera) throws IOException {
        setCamera(camera);
        camera.setPreviewDisplay(this.mHolder);
        Camera.Parameters parameters = camera.getParameters();
        requestLayout();
        camera.setParameters(parameters);
    }

    public void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed && getChildCount() > 0) {
            View child = getChildAt(0);
            int width = r - l;
            int height = b - t;
            if (width * height > height * width) {
                int scaledChildWidth = (width * height) / height;
                child.layout((width - scaledChildWidth) / 2, 0, (width + scaledChildWidth) / 2, height);
                return;
            }
            int scaledChildHeight = (height * width) / width;
            child.layout(0, (height - scaledChildHeight) / 2, width, (height + scaledChildHeight) / 2);
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        if (this.mCamera != null) {
            try {
                this.mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (this.mCamera != null) {
            this.mCamera.stopPreview();
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        int orientation = calculatePreviewOrientation(mCameraInfo, mDisplayOrientation);
        this.mCamera.setDisplayOrientation(orientation);
        if (this.mHolder.getSurface() != null) {
            try {
                this.mCamera.stopPreview();
                this.mCamera.setPreviewDisplay(this.mHolder);
                this.mCamera.startPreview();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) {
        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;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;
        } else {
            result = (info.orientation - degrees + 360) % 360;
        }

        return result;
    }
}

我怎么能把它弄得像主摄像头一样锐利,谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题