2012-03-06 101 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"