Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FrameLayout正在缩小ImageView,但摄影机在FrameLayout内正确打开_Android_Android Imageview_Android Framelayout - Fatal编程技术网

Android FrameLayout正在缩小ImageView,但摄影机在FrameLayout内正确打开

Android FrameLayout正在缩小ImageView,但摄影机在FrameLayout内正确打开,android,android-imageview,android-framelayout,Android,Android Imageview,Android Framelayout,我在横向模式下打开框架布局内的android摄像头,然后屏幕看起来像- 但是当我在FrameLayout中打开inside ImageView时,它看起来像- 请忽略第二张图片中的图片内容和有趣的方块。 区别是,, 第一个摄像机在整个屏幕上打开或高度触摸屏幕头部,但第二个图像将图像隐藏以适应 我希望第一个图像也像第二个一样工作,它应该适合定义的矩形。唯一的目标是Camera View和ImageView显示相同的外观 如果需要,我也可以更改整个布局。 FacePreviewImageView

我在横向模式下打开框架布局内的android摄像头,然后屏幕看起来像-

但是当我在FrameLayout中打开inside ImageView时,它看起来像-

请忽略第二张图片中的图片内容和有趣的方块。 区别是,, 第一个摄像机在整个屏幕上打开或高度触摸屏幕头部,但第二个图像将图像隐藏以适应

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

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

第一个摄像头视图仅适用于Android摄像头,我正在将其添加到Android框架布局中-

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>

我的表面师看起来像-

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();
    }

}
类预览扩展了SurfaceView实现了SurfaceHolder.Callback{
表面粗糙度;
麦克默拉照相机;
Camera.PreviewCallback PreviewCallback;
预览(上下文上下文,Camera.PreviewCallback PreviewCallback,Camera mCamera2){
超级(上下文);
this.previewCallback=previewCallback;
this.mCamera=mCamera2;
//安装SurfaceHolder.Callback,以便在
//创建和破坏下垫面。
mHolder=getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE\u TYPE\u PUSH\u缓冲区);
}
已创建的公共空白表面(表面持有人){
//曲面已创建,获取摄影机并告知其位置
//画。
//mCamera=摄像机打开(1);
//mCamera.setDisplayOrientation(270);
试一试{
mCamera.setPreviewDisplay(支架);
}捕获(IOException异常){
mCamera.release();
mCamera=null;
//TODO:在此处添加更多异常处理逻辑
}
}
公共空间表面覆盖(表面覆盖物持有人){
//当我们返回时,曲面将被破坏,因此请停止预览。
//因为CameraDevice对象不是共享资源,所以它非常有用
//当活动暂停时释放它很重要。
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
私有大小GetOptimizePreviewSize(列表大小,整数w,整数h){
最终双纵横比公差=0.05;
双目标率=(双)w/h;
如果(size==null)返回null;
Size=null;
double minDiff=double.MAX_值;
int targetHeight=h;
//尝试找到与纵横比和大小匹配的大小
用于(尺寸:尺寸){
双倍比率=(双倍)size.width/size.height;
如果(数学abs(比率-目标比率)>纵横比公差)继续;
if(数学绝对值(尺寸高度-目标光)
您设置了错误的相机预览大小的高度和宽度,还需要使用设置显示方向,请在设置高度和宽度的位置显示代码,并设置显示方向度


此外,您可能会看到正确的初始化相机

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


另外,您可能会看到初始化摄像头

已经共享了我设置摄像头的代码,您可以检查没有宽度和高度设置您的摄像头初始化错误,您需要设置摄像头。设置预览显示(surfaceHolder)。如果surfaceHolder是SurfaceView的持有者,请查看上面的链接。或者你可以看看这个例子。希望有帮助。我已经用我的SUrface Holder代码更新了这个问题,你在哪里发现了问题?是的,GetOptimization PreviewSize方法错误,最好使用aspect,再次查看上面的代码,例如aspect。谢谢,现在的一个问题是我正在检测人脸,但简单的绘制矩形非常小,我需要做一些计算,b会做什么