相机预览未全屏显示(显示在一个框中)

瓦姆西安波卢

我想使用自定义相机拍摄比预览更小的尺寸,我听说保持纵横比非常重要。我PreviewFrameLayoutAOSP ICS中使用过。我发现SurfaceView仅显示在中央活动在一个框内,并没有覆盖整个屏幕。

public class PreviewFrameLayout extends RelativeLayout{

static final String TAG="PreviewFrameLayout";
public interface OnSizeChangedListener
{
    public void onSizeChanged(int w,int h);
}

OnSizeChangedListener mListener;

public void setOnSizeChangedListener(OnSizeChangedListener listener)
{
    mListener=listener;
}

private double mAspectRatio=4.0/3.0;

public PreviewFrameLayout(Context c,AttributeSet attrs)
{
    super(c,attrs);
}

public void setAspectRatio(double ratio)
{
    if(ratio<=0.0)
        throw new IllegalArgumentException();
    if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
        ratio=1/ratio;
    if(mAspectRatio!=ratio)
    {
        mAspectRatio=ratio;
        requestLayout();
    }
}

@Override
protected void onMeasure(int widthSpec,int heightSpec)
{
    int previewWidth=MeasureSpec.getSize(widthSpec);
    int previewHeight=MeasureSpec.getSize(heightSpec);

    int hPadding=getPaddingLeft()+getPaddingRight();
    int vPadding=getPaddingTop()+getPaddingBottom();

    previewWidth-=hPadding;
    previewHeight-=vPadding;

    if(previewWidth>previewHeight*mAspectRatio)
        previewWidth=(int) (previewHeight*mAspectRatio+.5);
    else
        previewHeight=(int)(previewWidth/mAspectRatio+.5);
    previewWidth+=hPadding;
    previewHeight+=vPadding;
    Log.d(TAG,"Aspect ratio "+mAspectRatio);
    Log.d(TAG, "Width: "+previewWidth+" Height: "+previewHeight);
    super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
}

protected void onSizeChanged(int w,int h,int oldw,int oldh)
{
    if(mListener!=null)
        mListener.onSizeChanged(w, h);
}
}

并设置预览和图片大小,如下所示:

  private Size getOptimalPreviewSize(List<Size> supportedSizeList,double targetRatio)
{
    final double ASPECT_TOLERANCE=0.05;
    double minDiff=Double.MAX_VALUE;
    Size optimalSize=null;
    Display display=getWindowManager().getDefaultDisplay();
    @SuppressWarnings("deprecation")
    int targetHeight=Math.min(display.getWidth(), display.getHeight());
    if(targetHeight<=0)
    {   
        WindowManager windowManager=(WindowManager)getSystemService(Context.WINDOW_SERVICE);
        targetHeight=windowManager.getDefaultDisplay().getHeight();
    }
    for(Size size:supportedSizeList)
    {
        double ratio=(double)size.width/size.height;
        if(Math.abs(targetRatio-ratio)>ASPECT_TOLERANCE)
            continue;
        if(Math.abs(size.height-targetHeight)<minDiff)
        {
            optimalSize=size;
            minDiff=Math.abs(size.height-targetHeight);
        }
    }

    for(Size size:supportedSizeList)
    {
        if((Math.abs(size.height-targetHeight))<minDiff)
        {   
            optimalSize=size;
            minDiff=Math.abs(size.height-targetHeight);
        }
    }
    return optimalSize;
}

private Size getDesiredPictureSize(List<Size> supportedSizeList)
{
    //Resolution is widthxheight

    Size result=null;
    final int minArea=500*500;  
    final int maxArea=1000*1000;
    for(Size size:supportedSizeList)
    {
        if(size.width*size.height>minArea && size.width*size.height<maxArea)
        {
            if(result==null)
                result=size;
            else
            {
                int resultArea=result.width*result.height;
                int sizeArea=size.width*size.height;
                if(resultArea<sizeArea)
                {   
                    result=size;
                }   
            }
        }
    }
    return result;
 }

我在这里使用代码将长宽比设置为图片大小:

    mParameters=mCamera.getParameters();
    List<Size> supportedPictureSizes=mParameters.getSupportedPictureSizes();
    List<Size> supportedPreviewSizes=mParameters.getSupportedPreviewSizes();
    mPictureSize=getDesiredPictureSize(supportedPictureSizes);
    double targetRatio=(double)mPictureSize.width/mPictureSize.height;
    mPreviewPanel=findViewById(R.id.frame_layout);
    mPreviewFrameLayout=(PreviewFrameLayout)findViewById(R.id.frame);
    mPreviewFrameLayout.setAspectRatio(targetRatio);

此布局的父级是RelativeLayout

<com.example.newcameraproject.PreviewFrameLayout android:id="@+id/frame"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <SurfaceView android:id="@+id/camera_preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
      </com.example.newcameraproject.PreviewFrameLayout>

包括在主相机布局中,如下所示:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>

 <include layout="@layout/preview_frame"/>

 </LinearLayout>

编辑:

纵向:

CameraActivity(6502): Resolution Width: 720 Resolution Height: 1184
CameraActivity(6502): The Picture Width: 1152 The Picture Height: 864
CameraActivity(6502): The Preview Width:960 Preview Height: 720
CameraActivity(6502): Picture Ratio: 1.3333333333333333
CameraActivity(6502): Preview Ratio: 1.3333333333333333
PreviewFrameLayout(6502): Left: 0 Right: 0 Top: 0 Bottom: 0
PreviewFrameLayout(6502): Aspect ratio 0.75
PreviewFrameLayout(6502): Width: 720 Height: 960

由于纵横比处于纵向,因此纵横比被反转。

if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
        ratio=1/ratio;

摄像机显示为一个仅比活动小的框。

景观方向:

  CameraActivity(6502): Resolution Width: 1196 Resolution Height: 720
  CameraActivity(6502): The Picture Width: 1152 The Picture Height: 864
  CameraActivity(6502): The Preview Width:960 Preview Height: 720
  CameraActivity(6502): Picture Ratio: 1.3333333333333333
  CameraActivity(6502): Preview Ratio: 1.3333333333333333
  PreviewFrameLayout(6502): Left: 0 Right: 0 Top: 0 Bottom: 0
  PreviewFrameLayout(6502): Aspect ratio 1.3333333333333333
  PreviewFrameLayout(6502): Width: 787 Height: 590

 mPreviewSize=getOptimalPreviewSize(this,supportedPreviewSizes, targetRatio);
    Log.d(TAG, "The Preview Width:"+mPreviewSize.width+" Preview Height: "+mPreviewSize.height);
    double ratio=(double)mPreviewSize.width/mPreviewSize.height;
    Log.d(TAG,"Picture Ratio: "+targetRatio);
    Log.d(TAG, "Preview Ratio: "+ratio);
    int new_width=0, new_height=0;    
 if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
    {
        if((double)previewFrame.getWidth()/previewFrame.getHeight()<ratio)
        {
            new_width=(int)(Math.round(previewFrame.getHeight()*ratio));
            new_height=getWindowManager().getDefaultDisplay().getHeight();
        }
        else
        {
            new_width=getWindowManager().getDefaultDisplay().getHeight();
            new_height=(int)Math.round((double)new_width/ratio);
        }
    }
    if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
    {
        if((double)previewFrame.getWidth()/previewFrame.getHeight()<ratio)
        {
            new_width=(int)(Math.round(previewFrame.getHeight()*ratio));
            new_height=getWindowManager().getDefaultDisplay().getHeight();
        }
        else
        {
            new_width=getWindowManager().getDefaultDisplay().getWidth();
            new_height=(int)Math.round((double)new_width/ratio);
        }
    }

编辑:

1.Ensured that getOptimalPreviewSize and other methods works,changed code outside of this post to do so.
2.Even though these methods now work,the preview was not filling the screen,so changed to FrameLayout.The final version(see above) now works for both landscape and portrait.
3.I have noticed that the image in the preview looks stretched when the Camera Activity is rotated to landscape.

添加了一个问题以解决在使用FrameLayout时拉伸的Camera Preview中的问题

编辑2:

当我将以下代码添加到构造函数中时,Camera PreviewFrameLayout几乎可以完美地工作(几乎是全屏显示)……就在ShutterButton的上方:

 public PreviewFrameLayout(Context c,AttributeSet attrs)
{
    super(c,attrs);
    setAspectRatio(4.0/3.0);
}

仍然存在一些问题,例如在屏幕的显示方向为90的风景中拍照并预览...

我发现尽管使用ExifInterfaceMatrix来固定此旋转代码,但在横向和纵向模式下,获得的图像仍会沿横向和纵向模式顺时针旋转90度ImagePreviewActivity

Exif方向始终为0,我上次修复了此问题(Parameters.setRotation设置方向时没有使用代码的PreviewFrameLayout ...但此处似乎不起作用

亚历克斯·科恩

我看不到那里getOptimalPreviewSize()叫什么。另外,没有真正的理由设置预览大小以匹配屏幕;通常,制造商会选择默认的相机预览以使其在屏幕上看起来不错(这涉及缩放)。

有了这些,您发布的代码就可以大大简化。如果我理解正确,则您的活动中具有固定的screenOrientation,并希望用相机预览填充所有屏幕,并保留宽高比以匹配所需的图片尺寸。

这个简单的示例显示了如何设置预览曲面视图的宽度和高度。如果您还不够,请随时询问更多详细信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

相机预览未全屏显示(显示在一个框中)

来自分类Dev

元素ui图像预览未全屏显示

来自分类Dev

如何在GIMP中显示全屏预览?

来自分类Dev

捕获相机并在div中显示预览

来自分类Dev

遮罩未在OpenCV相机预览中显示

来自分类Dev

如何在“活动”中显示相机预览?

来自分类Dev

捕获相机并在div中显示预览

来自分类Dev

当前表中未显示数据,但另一个表中未显示数据

来自分类Dev

Android:Framelayout中的相对布局未显示(自定义相机预览屏幕)

来自分类Dev

在onClick中显示一个对话框

来自分类Dev

如何在GNOME Shell预览中突出显示最后一个活动窗口

来自分类Dev

未显示最后一个ID

来自分类Dev

如何在不显示父级活动的情况下将一个全屏对话框片段替换为另一个?

来自分类Dev

全屏未显示壁纸

来自分类Dev

AVPlayer未显示全屏

来自分类Dev

CardView 圆角未显示在 Android 预览中

来自分类Dev

预览标题未显示在 Facebook 帖子中

来自分类Dev

UIImagePickerController在ios7中未显示(需要花费更多时间来加载)相机预览

来自分类Dev

在选择另一个组合框之后,仅在组合框中显示更新的值

来自分类Dev

Android:对话框未全屏显示或AlertDialog在Android 2.3中没有奇怪的背景

来自分类Dev

当我在另一个网站(如 Facebook、Whatsapp 等)中粘贴时如何显示图像预览和标题

来自分类Dev

两个日期的差异显示在另一个文本框中

来自分类Dev

一个视图控制器中的两个表视图,一个表视图未显示

来自分类Dev

我想在显示相机上画一条线(相机预览)

来自分类Dev

在一个窗口中并排显示两个全屏视频背景

来自分类Dev

修改Windows 7预览窗格以显示文件夹的子目录,而不是一个大的空白框

来自分类Dev

修改Windows 7预览窗格以显示文件夹的子目录,而不是一个大的空白框

来自分类Dev

在GroupBox中显示一个PictureBox

来自分类Dev

Android:在相机预览中显示前景(和视图),以使旧相机回望

Related 相关文章

  1. 1

    相机预览未全屏显示(显示在一个框中)

  2. 2

    元素ui图像预览未全屏显示

  3. 3

    如何在GIMP中显示全屏预览?

  4. 4

    捕获相机并在div中显示预览

  5. 5

    遮罩未在OpenCV相机预览中显示

  6. 6

    如何在“活动”中显示相机预览?

  7. 7

    捕获相机并在div中显示预览

  8. 8

    当前表中未显示数据,但另一个表中未显示数据

  9. 9

    Android:Framelayout中的相对布局未显示(自定义相机预览屏幕)

  10. 10

    在onClick中显示一个对话框

  11. 11

    如何在GNOME Shell预览中突出显示最后一个活动窗口

  12. 12

    未显示最后一个ID

  13. 13

    如何在不显示父级活动的情况下将一个全屏对话框片段替换为另一个?

  14. 14

    全屏未显示壁纸

  15. 15

    AVPlayer未显示全屏

  16. 16

    CardView 圆角未显示在 Android 预览中

  17. 17

    预览标题未显示在 Facebook 帖子中

  18. 18

    UIImagePickerController在ios7中未显示(需要花费更多时间来加载)相机预览

  19. 19

    在选择另一个组合框之后,仅在组合框中显示更新的值

  20. 20

    Android:对话框未全屏显示或AlertDialog在Android 2.3中没有奇怪的背景

  21. 21

    当我在另一个网站(如 Facebook、Whatsapp 等)中粘贴时如何显示图像预览和标题

  22. 22

    两个日期的差异显示在另一个文本框中

  23. 23

    一个视图控制器中的两个表视图,一个表视图未显示

  24. 24

    我想在显示相机上画一条线(相机预览)

  25. 25

    在一个窗口中并排显示两个全屏视频背景

  26. 26

    修改Windows 7预览窗格以显示文件夹的子目录,而不是一个大的空白框

  27. 27

    修改Windows 7预览窗格以显示文件夹的子目录,而不是一个大的空白框

  28. 28

    在GroupBox中显示一个PictureBox

  29. 29

    Android:在相机预览中显示前景(和视图),以使旧相机回望

热门标签

归档