1

我正在开发一个相机应用程序,问题是我正在制作的相机显示扭曲的预览。

在此处输入图像描述

它拍摄的照片很清晰,但预览却一团糟。

在此处输入图像描述

完整的相机预览代码如下以防万一:

import java.io.IOException;
import java.util.List;

import com.glass.cuxtomcam.constants.CuxtomIntent.CAMERA_MODE;

import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.OnZoomChangeListener;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CameraPreview extends SurfaceView implements
        SurfaceHolder.Callback, OnZoomChangeListener {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private Context mContext;
    private static String TAG = "CAMERA PREVIEW";
    private int zoomOffset;
    private CameraListener mCallback;
    private int cameraMode;

    public CameraPreview(Context context, Camera camera, int cameraMode) {
        super(context);
        mContext = context;
        mCamera = camera;
        mHolder = getHolder();
        this.cameraMode = cameraMode;
        mHolder.addCallback(this);
        mHolder.setKeepScreenOn(true);

    }

    public void setCameraListener(CameraListener listener) {
        mCallback = listener;
    }

    @Override
    public synchronized void surfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mCamera != null) {
            try {
                mCamera.stopPreview();
                if (mCamera.getParameters().isZoomSupported()) {
                    mCamera.setZoomChangeListener(this);
                    zoomOffset = mCamera.getParameters().getMaxZoom() / 5;
                }
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
                mCamera.lock();
            } catch (IOException e) {
                Log.e("Error starting preview", e.getMessage());
            }

        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mCamera != null) {
            mCamera.stopPreview();
            // release the camera
            mCamera.release();
            // unbind the camera from this object
            mCamera = null;
        }

    }

    public synchronized void zoomIn() {
        if (mCamera != null && mCamera.getParameters().isZoomSupported()
                && mCamera.getParameters().isSmoothZoomSupported()) {
            int zoomvalue = mCamera.getParameters().getZoom() + zoomOffset;
            if (zoomvalue <= mCamera.getParameters().getMaxZoom()) {
                mCamera.startSmoothZoom(zoomvalue);
            }
        } else {
            Toast.makeText(mContext, "Zoom In is not supported",
                    Toast.LENGTH_LONG).show();
        }
    }

    public synchronized void zoomOut() {
        if (mCamera != null && mCamera.getParameters().isZoomSupported()
                && mCamera.getParameters().isSmoothZoomSupported()) {
            int zoomvalue = mCamera.getParameters().getZoom() - zoomOffset;
            if (zoomvalue >= 0) {
                mCamera.startSmoothZoom(zoomvalue);
            }
        } else {
            Toast.makeText(mContext, "Zoom Out is not supported",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onZoomChange(int zoomValue, boolean stopped, Camera camera) {
        // Log.i("Camera Zoom Value", zoomValue + "");

    }

}

谁能指导我为什么会发生这种情况?

4

1 回答 1

3

不确定它是否仍然相关(2 个月后没有答案)。但这是当我的相机像你展示的那样时我使用的解决方案。

Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewFpsRange(30000, 30000);
parameters.setPreviewSize(640,360);
camera.setParameters(parameters);

它将每秒帧数设置为 30fps,您遇到的问题是帧率太高(很可能)

于 2014-06-17T12:34:21.430 回答