3

我想在 android 的实时提要上叠加图像。谁能建议从哪里开始?我已将所需的网址转换为图像。下面是代码:

ImageView imgView = (ImageView) findViewById(R.id.ImageView01); Drawable drawable = LoadImageFromWebOperations("https://www.google.com.pk/logos/classicplus.png"); imgView.setImageDrawable(drawable);

private Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }

现在,我想要的是如何将此图像叠加到视频中的所需区域,即手部检测区域。问题是这个感兴趣的区域可以移动,因为它是一个实时提要......我们将不胜感激。

4

1 回答 1

0

我通过叠加两个视图(下面称为预览和叠加)来做到这一点。一个负责相机预览,另一个控制画布以绘制我想要的任何东西。

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <FrameLayout
      android:id="@+id/toplevelframe"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
    <your.project.Preview
    android:id="@+id/preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
    <your.project.Overlay
    android:id="@+id/overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
  </FrameLayout>
</LinearLayout>

在您的活动课程中,onCreate()

...
setContentView(R.layout.main);
mPreview = (Preview) findViewById(R.id.preview);
mOverlay = (Overlay) findViewById(R.id.overlay);
...

onResume()

...
mCamera = Camera.open();
mPreview.setCamera(mCamera);
...

onPause()

if (mCamera != null) {
    mPreview.setCamera(null);
    mCamera.release();
    mCamera = null;
}

Overlay扩展 View 并在画布上绘图。没有什么特别的,只是覆盖onDraw()

对于相机预览类(Preview如下),请查看官方指南以获取示例。它看起来像这样:

class Preview extends ViewGroup implements SurfaceHolder.Callback {
...
  private SurfaceView mSurfaceView;
  private SurfaceHolder mHolder;
  private Camera mCamera;
...
  public Preview(Context context, AttributeSet aset) {
      ...
      mSurfaceView = new SurfaceView(context);
      addView(mSurfaceView);
      // Install a SurfaceHolder.Callback so we get notified when the
      // underlying surface is created and destroyed.
      mHolder = mSurfaceView.getHolder();
      mHolder.addCallback(this);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }
...
  public void setCamera(Camera camera) {
      mCamera = camera;
      if (mCamera != null) {
          mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
          requestLayout();
      }
   }
...
}

然后也实现SurfaceHolder.Callback的所有方法。

于 2012-07-26T15:19:13.200 回答