的FrameLayout正在萎缩的ImageView,但相机内的FrameLayout正常开放 [英] FrameLayout is shrinking ImageView , but camera is opening properly inside FrameLayout

查看:154
本文介绍了的FrameLayout正在萎缩的ImageView,但相机内的FrameLayout正常开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在横向模式下打开的FrameLayout里面的Andr​​oid相机,屏幕看起来像 -

但是,当我在里面的FrameLayout的ImageView打开相同的图像,那么它看起来喜欢 -

请忽略图片的内容和所述第二图像中绘制的滑稽正方形。
不同的是,
首先CameraView在整个屏幕打开或高度摸了摸头的thescreen但第二图像shirinking图像以适应。

我希望第一个形象,以及像第二个工作,它应符合规定的rectange下。唯一的目标是相机视图和ImageView的显示相同的外观。

如果需要,我可以改变整个布局为好。
面对previewImageView是我加入的形象。

首先摄像头视图仅仅是Android相机和我加入到了Android的FrameLayout -

 相机mCamera = Camera.open(1);
的FrameLayout布局=(的FrameLayout)findViewById(R.id.ll2);
layout.addView(新CameraView());

第二帧我加入类似 -

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

布局XML是 -

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向
    机器人:baselineAligned =假>    <的LinearLayout
        机器人:ID =@ + ID / LL1
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_weight =1
        机器人:方向=横向>
    < / LinearLayout中>     <的FrameLayout
        机器人:ID =@ + ID / [112
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_weight =0.5
        机器人:方向=横向>         < com.example.defaultfacetracker.Face previewImageView
             机器人:ID =@ + ID /面部preVIEW
             机器人:layout_width =match_parent
             机器人:layout_height =match_parent
             机器人:SRC =@机器人:可绘制/ toast_frame
             />    < /&的FrameLayout GT;    <的LinearLayout
        机器人:ID =@ + ID / 113
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_weight =1
        机器人:方向=横向>        <按钮
            机器人:ID =@ + ID /按钮1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向
            机器人:文字=过程/>    < / LinearLayout中>< / LinearLayout中>

我SurfaceHolder样子 -

 类preVIEW延伸SurfaceView实现SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    相机mCamera;
    相机previewCallback previewCallback。    preVIEW(上下文的背景下,相机。previewCallback previewCallback,相机mCamera2){
        超级(上下文);
        这previewCallback = previewCallback。
        this.mCamera = mCamera2;        //安装SurfaceHolder.Callback所以我们得到通知时,
        //创建下垫面和销毁。
        mHolder = getHolder();
        mHolder.addCallback(本);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }    公共无效surfaceCreated(SurfaceHolder持有人){
        //表面有被创建,获取摄像机,并告诉它在哪里
        //绘制。
      // mCamera = Camera.open(1);
        //mCamera.setDisplayOrientation(270);
        尝试{
           mCamera.set previewDisplay(支架);
        }赶上(IOException异常除外){
            mCamera.release();
            mCamera = NULL;
            // TODO:在此处增加更多的异常处理逻辑
        }
    }    公共无效surfaceDestroyed(SurfaceHolder持有人){
        //当我们回到地面就会被破坏,因此停止preVIEW。
        //因为CameraDevice对象不是共享资源,这是非常
        //重要的是释放它当活动被暂停。
        mCamera.stop preVIEW();
        mCamera.release();
        mCamera = NULL;
    }
    私人尺寸getOptimal previewSize(列表<尺寸和GT;的大小,INT W,INT高){
        最终双ASPECT_TOLERANCE = 0.05;
        双targetRatio =(双)W / H;
        如果(大小== NULL)返回NULL;        大小optimalSize = NULL;
        双minDiff = Double.MAX_VALUE;        INT targetHeight = H;        //尝试找到一个大小匹配纵横比和尺寸
        对于(尺寸大小:大小){
            双率=(双)size.width / size.height;
            如果(Math.abs(比 - targetRatio)GT; ASPECT_TOLERANCE)继续;
            如果(Math.abs(size.height - targetHeight)LT; minDiff){
                optimalSize =大小;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }        //找不到一个长宽比匹配,忽略此要求
        如果(optimalSize == NULL){
            minDiff = Double.MAX_VALUE;
            对于(尺寸大小:大小){
                如果(Math.abs(size.height - targetHeight)LT; minDiff){
                    optimalSize =大小;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        返回optimalSize;
    }    公共无效surfaceChanged(SurfaceHolder架,INT格式,诠释W,INT高){
        //现在,尺寸是已知的,设置相机参数,并开始
        //将preVIEW。
        Camera.Parameters参数= mCamera.getParameters();        清单<尺寸和GT;大小= parameters.getSupported previewSizes();
        大小optimalSize = getOptimal previewSize(尺寸,W,H);
        parameters.set previewSize(optimalSize.width,optimalSize.height);        mCamera.setParameters(参数);
        如果(previewCallback!= NULL){
            mCamera.set previewCallbackWithBuffer(previewCallback);
            Camera.Size大小= parameters.get previewSize();
            字节[]数据=新的字节[size.width * size.height *
                    ImageFormat.getBitsPerPixel(parameters.get previewFormat())/ 8];
            mCamera.addCallbackBuffer(数据);
        }
//如果(getResources()。getConfiguration()。方向== Configuration.ORIENTATION_PORTRAIT){
// mCamera.setDisplayOrientation(90);
//
//}其他{
// mCamera.setDisplayOrientation(0);
//}
        mCamera.start preVIEW();
    }}


解决方案

您设置一个摄像头preVIEW尺寸错误的高度和宽度,也需要使用 setDisplayOrientation ,请出示code在这里你设置的高度和宽度,并设置显示方向度。

你也可以看到正确初始化相机<一个href=\"https://github.com/kvirair/Simple-Scanner-Android/blob/master/src/garin/artemiy/simplescanner/library/views/SimpleCameraView.java\"相对=nofollow>这里

I am opening the android Camera inside a FrameLayout in landscape mode, then the screen looks like-

But the same Image when I open inside ImageView in FrameLayout, then it looks like-

Please ignore the content of the Picture and funny squares drawn in the second image. The difference is, First CameraView opened in the entire screen or height touched the head of thescreen but second image shirinking the image to fit.

I want the First image as well work like second, it should fit under a defined rectange. The only objective is Camera View and ImageView shows the same Look.

I can change the entire layout as well if required. FacePreviewImageView is the image I am adding.

First Camera View is Android Camera only and I am adding into the android framelayout-

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

Second Frame I am adding like-

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

The Layout XML is -

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

My SurfaceHolder looks like-

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

}

解决方案

You set wrong height and width of a camera preview size, also need use setDisplayOrientation, please show code where you set height and width, and set display orientation degrees.

Also you may see properly initialization camera here

这篇关于的FrameLayout正在萎缩的ImageView,但相机内的FrameLayout正常开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆