1

我在横向模式下在 FrameLayout 中打开 android 相机,然后屏幕看起来像 -

在此处输入图像描述

但是当我在 FrameLayout 中的 ImageView 中打开相同的图像时,它看起来像 - 在此处输入图像描述

请忽略图片的内容和第二张图片中绘制的有趣方块。不同之处在于,第一个 CameraView 在整个屏幕中打开或高度触及屏幕头部,但第二个图像缩小图像以适应。

我希望第一个图像也像第二个一样工作,它应该适合定义的矩形。唯一的目标是 Camera View 和 ImageView 显示相同的 Look。

如果需要,我也可以更改整个布局。FacePreviewImageView 是我添加的图像。

第一个相机视图仅适用于 Android 相机,我正在添加到 android framelayout-

Camera mCamera = Camera.open(1);
FrameLayout layout = (FrameLayout) findViewById(R.id.ll2);
layout.addView(new CameraView());

我添加的第二帧像 -

layout.removeAllViews();
                layout.addView(faceImageView);
                faceView.setVisibility(View.GONE);
                faceImageView.setVisibility(View.VISIBLE);

布局 XML 是 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:baselineAligned="false">

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" 
        android:orientation="horizontal">


    </LinearLayout>

     <FrameLayout
        android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="horizontal" >

         <com.example.defaultfacetracker.FacePreviewImageView
             android:id="@+id/facePreview"             
             android:layout_width="match_parent"
             android:layout_height="match_parent" 
             android:src="@android:drawable/toast_frame"
             /> 

    </FrameLayout>

    <LinearLayout
        android:id="@+id/ll3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:text="Process" />

    </LinearLayout>

</LinearLayout>

我的 SurfaceHolder 看起来像 -

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;
    Camera.PreviewCallback previewCallback;

    Preview(Context context, Camera.PreviewCallback previewCallback, Camera mCamera2) {
        super(context);
        this.previewCallback = previewCallback;
        this.mCamera = mCamera2;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
      //  mCamera = Camera.open(1);
        //mCamera.setDisplayOrientation(270);
        try {
           mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
            // TODO: add more exception handling logic here
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }


    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();

        List<Size> sizes = parameters.getSupportedPreviewSizes();
        Size optimalSize = getOptimalPreviewSize(sizes, w, h);
        parameters.setPreviewSize(optimalSize.width, optimalSize.height);

        mCamera.setParameters(parameters);
        if (previewCallback != null) {
            mCamera.setPreviewCallbackWithBuffer(previewCallback);
            Camera.Size size = parameters.getPreviewSize();
            byte[] data = new byte[size.width*size.height*
                    ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())/8];
            mCamera.addCallbackBuffer(data);
        }
//        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//          mCamera.setDisplayOrientation(90);
//          
//      } else {
//          mCamera.setDisplayOrientation(0);
//      }
        mCamera.startPreview();
    }

}
4

1 回答 1

4

您设置了错误的相机预览尺寸的高度和宽度,还需要使用setDisplayOrientation,请显示您设置高度和宽度的代码,并设置显示方向度。

你也可以在这里看到正确的初始化相机

于 2013-10-18T11:16:34.620 回答