2012-03-06 103 views
6

我想把相机放在android手机上的肖像方向的surfaceView。我使用http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html将相机放在surfaceView中。然而,相机旋转90度,所以我试着做setDisplayOrientation(90)来解决它,但这压扁了图像(它可能不是调整大小的SurfaceView正确??)。我该如何制作它,这样相机不会被挤压?相机在肖像模式下拉伸

回答

6

您需要根据旋转调整图像大小。一个例子见here

+0

应该高度和宽度是多少在该例子吗?我希望预览大约是屏幕的一半,但我不想硬编码值,因此它可以在不同的屏幕尺寸上工作 – user1154920 2012-03-06 19:45:21

+0

这是SurfaceHolder回调方法,因此方法的高度和宽度参数将由硬件。如果你希望你的预览是屏幕的一半,那么只需将它们设置为'if'语句,如:parameters.setPreviewSize(width/2,height/2); – 2012-03-06 23:30:00

2

如果您使用Android API演示,则需要修改OnLayout()函数。

基本上是Android API演示,根据宽高比设置预览大小,以便预览图像被压扁并在纵向模式下以小尺寸显示在屏幕中央。

即使我们将屏幕方向设置为纵向模式,预览宽度和高度也不会改变。 这就是为什么预览图像被压扁的原因。

-2

使用此功能调整图像大小这将是对你有用

public Bitmap resize(Bitmap img,int Width,int Height){ 

    int width = img.getWidth(); 

    int height = img.getHeight(); 

    int newWidth = (int) Width; 

    int newHeight = (int) Height; 

    // calculate the scale - in this case = 0.4f 
    float scaleWidth = ((float) newWidth)/width; 

    float scaleHeight = ((float) newHeight)/height; 

    // createa matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 

    // rotate the Bitmap 
    //matrix.postRotate(45); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true); 

    return resizedBitmap; 

} 
0

只为我工作的事情是

在代码mCamera.setDisplayOrientation(90);

布局具有fill_parent两个heightwidth

和清单的文件有android:screenOrientation="portrait"