Android相机预览在谷歌眼镜上无法正常显示?

时间:2014-04-03 09:52:34

标签: android android-layout android-camera surfaceview surfaceholder

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

enter image description here

拍摄的照片很清晰,但预览却搞砸了。

enter image description here

以下是完整的相机预览代码:

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 + "");

    }

}

任何人都可以指导我为什么会这样?

1 个答案:

答案 0 :(得分:3)

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

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

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