安卓相机预览人像比例

标签 android camera surfaceview

请帮助正确配置 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();
        }
    }
}

Activity 布局:

<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" >

实际图片:

enter image description here - 设备旋转纵向

enter image description here - 设备旋转横向

在相同主题中找到:Android - Camera preview is sideways , Android camera rotate , 但我用他们的代码得到了相同的结果

在这里您可能会看到图像的错误比例。 如何以适当的比例显示旋转和缩放的相机预览?

部分代码取自 Mixare 开源项目,http://www.mixare.org/

最佳答案

我知道我来不及回答这个问题,但是这个问题可以通过将相机的显示方向设置为 90 度来解决,即在 中的 camera.open() 之后添加以下行CameraSurface 类的 SurfaceCreated 方法

camera.setDisplayOrientation(90);

希望这能帮助其他遇到同样问题的人。

关于安卓相机预览人像比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14562255/

相关文章:

android - 如何将 OpenGL 上下文中的纹理复制到另一个上下文

android - 是否可以使用 32 位 (ARGB_8888) 表面格式的动态壁纸?

java - 从 SurfaceView 调用 finish() 不会销毁 Activity

Android:在将标记保持在中心的同时拖动 map

android - 是否可以从应用程序外部识别单击的 View 及其索引号?

iPhone iOS 4.0 相机 FigCreateCGImageFromJPEG 返回 -1

Android Camera.takePicture() - nullPointerException

Android MediaRecorder 宽高比不正确

android - 将 native 页脚 View 添加到 webview

android - 如何在android中隐藏用户界面