相机应用问题相机预览

时间:2016-01-28 07:23:54

标签: android android-fragments camera android-camera-intent

我在Android自定义相机应用程序中工作。我正面临一个问题,即相机预览被拉伸,我该如何解决这个问题? 或者当我用前置摄像头点击图像时,然后以旋转形式显示图像。有什么图书馆吗?

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.
    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Camera.Size selected = sizes.get(0);
    parameters.setPreviewSize(selected.width,selected.height);
    mCamera.setDisplayOrientation(Constant.CAMERA_ORIENTATION);
    mCamera.setParameters(parameters);

    mCamera.startPreview();
    refreshCamera(mCamera);
}

public void setCamera(Camera camera) {
    //method to set a camera instance
    mCamera = camera;
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    // mCamera.release();

}
public int getDisplayWidth() {
    return displayWidth;
}
public int getDisplayHeight() {
    return displayHeight;
}
public void setDisplayWidth(int displayWidth) {
    this.displayWidth = displayWidth;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    final int width = resolveSize(getSuggestedMinimumWidth(),  widthMeasureSpec);
    final int height = resolveSize(getSuggestedMinimumHeight(),    heightMeasureSpec);
    if (supportedPreviewSizes != null) {

        previewSize = getOptimalPreviewSize(supportedPreviewSizes, width,    height);

        setCameraPictureSize();

        Constant.CAMERA_PICTURE_WIDTH = pictureSize.width;
        Constant.CAMERA_PICTURE_HEIGHT = pictureSize.height;
    }
    float ratio = 0f;
    if(previewSize != null) {
        if (previewSize.height >= previewSize.width) {
            ratio = (float) previewSize.height / previewSize.width;
        } else {
            ratio = (float) previewSize.width / previewSize.height;
        }
    }
    Constant.CAMERA_PREVIEW_WIDTH = previewSize.width;
    Constant.CAMERA_PREVIEW_HEIGHT = previewSize.height;
    int newWidth =  width;
    int newHeight = (int)(width * ratio);

    setMeasuredDimension(newWidth, newHeight);
    }

private void setCameraPictureSize() {
    List<Camera.Size> pictureSizes =  mCamera.getParameters().getSupportedPictureSizes();
    float previewAspectRatio = (float) previewSize.width /    previewSize.height;
    for(Camera.Size size : pictureSizes){
        float pictureAspectRatio = (float) size.width / size.height;
        if(previewAspectRatio == pictureAspectRatio){
            pictureSize = size;
            break;
        }
    }

    if(pictureSize == null) {
        //get the largest picture size
        pictureSize = pictureSizes.get(0);
    }
}

1 个答案:

答案 0 :(得分:1)

设置此,

mSurfaceView.setAspectRatioMode(SurfaceView.ASPECT_RATIO_PREVIEW);