如何使用SurfaceView Android应用全相机功能

时间:2013-10-11 06:48:12

标签: android xml camera surfaceview

我正在使用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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>