0

我正在使用 Surface View Camera 在 Android 中开发一个项目。我想在相机的 Surface View 上显示一些按钮和一个框架,并使其成为相机捕捉功能。

现在我完成了 SurfaceView 以查看相机上方的帧图像,但是当我添加按钮时,它显示在表面视图之外。下面是我的主要代码:

//这是我的PreviewDemo.java

    package com.commonsware.android.camera;

     import android.app.Activity;
     import android.content.pm.ActivityInfo;
     import android.hardware.Camera;
     import android.os.Bundle;
     import android.util.Log;
     import android.view.LayoutInflater;
     import android.view.SurfaceHolder;
     import android.view.SurfaceView;
     import android.view.View;
     import android.view.ViewGroup.LayoutParams;
     import android.widget.Toast;

     public class PreviewDemo extends Activity {
     private SurfaceView preview=null;
     private SurfaceHolder previewHolder=null;
     private Camera camera=null;
     private boolean inPreview=false;
     private boolean cameraConfigured=false;
     LayoutInflater inflater = null;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    preview=(SurfaceView)findViewById(R.id.preview);
    previewHolder=preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    inflater = LayoutInflater.from(getBaseContext());
    View view = inflater.inflate(R.layout.overlay, null);
    LayoutParams layoutParamsControl= new LayoutParams(LayoutParams.MATCH_PARENT,   LayoutParams.MATCH_PARENT);
    this.addContentView(view, layoutParamsControl);
     }

     @Override
    public void onResume() {
     super.onResume();

    camera=Camera.open();
    startPreview();
    }

    @Override
    public void onPause() {
    if (inPreview) {
      camera.stopPreview();
    }

    camera.release();
    camera=null;
    inPreview=false;

    super.onPause();
    }

    private Camera.Size getBestPreviewSize(int width, int height,
                                         Camera.Parameters parameters) {
    Camera.Size result=null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
      if (size.width<=width && size.height<=height) {
        if (result==null) {
          result=size;
        }
        else {
          int resultArea=result.width*result.height;
          int newArea=size.width*size.height;

          if (newArea>resultArea) {
            result=size;
          }
        }
       }
     }

    return(result);
     }

    private void initPreview(int width, int height) {
    if (camera!=null && previewHolder.getSurface()!=null) {
      try {
        camera.setPreviewDisplay(previewHolder);
      }
      catch (Throwable t) {
        Log.e("PreviewDemo-surfaceCallback",
              "Exception in setPreviewDisplay()", t);
        Toast
          .makeText(PreviewDemo.this, t.getMessage(), Toast.LENGTH_LONG)
          .show();
      }

      if (!cameraConfigured) {
        Camera.Parameters parameters=camera.getParameters();
        Camera.Size size=getBestPreviewSize(width, height,
                                            parameters);

        if (size!=null) {
          parameters.setPreviewSize(size.width, size.height);
          camera.setParameters(parameters);
          cameraConfigured=true;
        }
      }
      }
      }

      private void startPreview() {
      if (cameraConfigured && camera!=null) {
      camera.startPreview();
      inPreview=true;
      }
     }

     SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
     public void surfaceCreated(SurfaceHolder holder) {
      // no-op -- wait until surfaceChanged()
     }

     public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
      initPreview(width, height);
       startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
      // no-op
     }
    };
  }

//这是我的Overlay.Xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="fill_vertical"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/surface_camera"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:background="@drawable/frame"
        android:clickable="true"
        android:longClickable="false"
        android:padding="@android:dimen/thumbnail_height" />

</LinearLayout>
4

2 回答 2

0

我希望我已经理解了你的问题。但是,如果您希望在框架的表面视图上方显示“捕获”按钮,请尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/frame"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Capture" />

<SurfaceView
    android:id="@+id/surface_camera"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/frame"
    android:clickable="true"
    android:layout_below="@id/button1"
    android:longClickable="false" />

</RelativeLayout>
于 2013-10-11T07:09:09.403 回答
0

我希望我理解正确。您正在尝试添加将在视频预览顶部显示按钮的叠加层。为此,您需要使用相对布局,该布局(默认情况下)将视图置于彼此之上。放置 SurfaceView 后,您可以放置​​您喜欢的任何视图,并使用其属性以您想要的方式在屏幕上对齐它。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill_vertical">

<SurfaceView
    android:id="@+id/surface_camera"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:layout_weight="1"
    android:background="@drawable/frame"
    android:clickable="true"
    android:longClickable="false"
    android:padding="@android:dimen/thumbnail_height" />

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    // PUT YOUR BUTTONS HERE
</FrameLayout

</RelativeLayout>
于 2013-10-11T07:10:40.927 回答