簡體   English   中英

Android相機預覽肖像比例

[英]Android camera preview portrait scale

請幫助正確配置Android攝像頭(在HTC Desire 2.3.3上進行測試),以便在旋轉(縱向)模式下從攝像頭進行預覽。 預覽必須占用不到一半的屏幕,寬度必須等於設備寬度,並且必須根據相機縱橫比動態設置比例。 我目前的代碼:

public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback {
…
public void surfaceCreated(SurfaceHolder holder) {
try {
    if (camera != null) {
                try {
                    camera.stopPreview();
                } catch (Exception ignore) {
                }
                try {
                    camera.release();
                } catch (Exception ignore) {
                }
                camera = null;
            }

            camera = Camera.open();             
            camera.setPreviewDisplay(holder);
        } catch (Exception ex) {
            try {
                if (camera != null) {
                    try {
                        camera.stopPreview();
                    } catch (Exception ignore) {
                    }
                    try {
                        camera.release();
                    } catch (Exception ignore) {
                    }
                    camera = null;
                }
            } catch (Exception ignore) {    
            }
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        try {
            if (camera != null) {
                try {
                    camera.stopPreview();
                } catch (Exception ignore) {
                }
                try {
                    camera.release();
                } catch (Exception ignore) {
                }
                camera = null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        try {
            Camera.Parameters parameters = camera.getParameters();
            float bff = 0;
            try {
                List<Camera.Size> supportedSizes = null;

                //maximize supported resizes, TODO remove as hardcode
                w*=1.5;
                h*=1.5;

                // On older devices (<1.6) the following will fail
                // the camera will work nevertheless
                supportedSizes = Compatibility.getSupportedPreviewSizes(parameters);

                // preview form factor
                float ff = (float) w / h;
                Log.d("TAG", "Screen res: w:" + w + " h:" + h
                        + " aspect ratio:" + ff);

                // holder for the best form factor and size                 
                int bestw = 0;
                int besth = 0;
                Iterator<Camera.Size> itr = supportedSizes.iterator();

                // we look for the best preview size, it has to be the closest
                // to the
                // screen form factor

                while (itr.hasNext()) {
                    Camera.Size element = itr.next();
                    // current form factor
                    float cff = (float) element.width / element.height;
                    // check if the current element is a candidate to replace
                    // the best match so far
                    // current form factor should be closer to the bff
                    // preview width should be less than screen width
                    // preview width should be more than current bestw
                    // this combination will ensure that the highest resolution
                    // will win
                    Log.d("TAG", "Candidate camera element: w:"
                            + element.width + " h:" + element.height
                            + " aspect ratio:" + cff);
                    if ((ff - cff <= ff - bff) && (element.width <= w)
                            && (element.width >= bestw)) {
                        bff = cff;
                        bestw = element.width;
                        besth = element.height;
                    }
                }
                Log.d("TAG", "Chosen camera element: w:" + bestw + " h:"
                        + besth + " aspect ratio:" + bff);
                // Some Samsung phones will end up with bestw and besth = 0
                // because their minimum preview size is bigger then the screen
                // size.
                // In this case, we use the default values: 480x320
                if ((bestw == 0) || (besth == 0)) {
                    Log.d("Mixare", "Using default camera parameters!");
                    bestw = 480;
                    besth = 320;
                }               
                parameters.setPreviewSize(bestw,besth);
            } catch (Exception ex) {
                parameters.setPreviewSize(480,320);

                bff=1.5f;
            }
            makeResizeForCameraAspect(bff);                     
            camera.setDisplayOrientation(90);//only Android 2.2 and later
            camera.setParameters(parameters);               
            camera.startPreview();
        } catch (Exception ex) {
            Log.e(TAG,"",ex);
        }
    }

    private void makeResizeForCameraAspect(float cameraAspectRatio){
        LayoutParams layoutParams=this.getLayoutParams();
        int matchParentWidth=this.getWidth();           
        int newHeight=(int)(matchParentWidth/cameraAspectRatio);
        if(newHeight!=layoutParams.height){
            layoutParams.height=newHeight;
            layoutParams.width=matchParentWidth;    
            this.setLayoutParams(layoutParams);
            this.invalidate();
        }
    }
}

活動布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >  
    <org.mixare.CameraSurface
        android:id="@+id/camera_surface"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"

         />
    <!--CameraSurface layout_height will be changed during it's loading process -->
</RelativeLayout>

AndroidManifest配置為肖像:

  <activity android:label="@string/app_name" android:name=".MixView" 
    android:screenOrientation="portrait"  
    android:launchMode="singleTop" >

實際圖片:

在此輸入圖像描述 - 設備旋轉肖像

在此輸入圖像描述 - 設備旋轉景觀

在相同的主題中找到: Android - 相機預覽是側面的Android相機旋轉 ,但我的代碼有相同的結果

在這里你可以看到錯誤的圖像比例。 如何以適當的比例顯示旋轉和縮放的相機預覽?

部分代碼取自Mixare開源項目, http://www.mixare.org/

我知道我來不及回答這個問題,但是這個問題可以通過將攝像機的顯示方向設置為90度來修復,即通過在CameraSurface類的SurfaceCreated方法中的CameraSurface camera.open()之后添加以下行來CameraSurface

camera.setDisplayOrientation(90);

希望這有助於其他人遇到同樣的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM