相机预览被拉伸和倾斜

时间:2016-04-20 09:44:17

标签: android android-camera surfaceview

如何解决相机预览中图像的拉伸问题?

相机预览类

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 
private SurfaceHolder mHolder; 
private Camera mCamera;

public class CameraPreview extends SurfaceView implements
        SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // create the surface and start camera preview
            if (mCamera == null) {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
        } catch (IOException e) {
            Log.d(VIEW_LOG_TAG,
                    "Error setting camera preview: " + e.getMessage());
        }
    }

    public void refreshCamera(Camera camera) {
        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }
        // set preview size and make any resize, rotate or
        // reformatting changes here
        // start preview with new settings
        setCamera(camera);
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(VIEW_LOG_TAG,
                    "Error starting camera preview: " + e.getMessage());
        }
    }

    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.
        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();

    }
}

1 个答案:

答案 0 :(得分:0)

也许您的CameraView的宽高比与相机预览尺寸的宽高比不匹配 您应该计算CameraView的宽高比并使用此宽高比来查找you need to get the height of string and then set cell size aacording to that string!!! - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { height=_collectionView.contentSize.height; width = _collectionView.frame.size.width; _collectionView.frame = CGRectMake(0,20,width, height); } #pragma MARK COLECTIONVIEW DELEGATE FLOW LAYOUT - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize Size =[[self.Array_LIKEs objectAtIndex:indexPath.row] sizeWithAttributes:NULL]; CGFloat calculate_width =Size.width+Size.width/2+20; CGFloat calculate_height=Size.height+10; return CGSizeMake(calculate_width, calculate_height); } 方法的最佳大小。 试试这个

Camera.Parameters.setPreviewSize()