3

我正在尝试使用 TextureView 捕获图像,但 main.xml 文件被 TextureView 覆盖,因此我看不到 main.xml 文件的内容。我正在尝试将 TextureView 添加到 main.xml 中。请就如何改进我的代码提出建议。

爪哇:

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextureView = (TextureView) findViewById(R.id.textureView1);
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open(0);
        try {
            mCamera.setPreviewTexture(surface);
        } 
        catch(IOException e) {
            e.printStackTrace();
        }
        mCamera.startPreview();
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
    }
}

布局资源 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextureView
    android:id="@+id/textureView1"
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:layout_below="@+id/textView1" />
4

2 回答 2

0

我不明白你的问题。

你说你想在TextureView你的 activity_main.xml 布局中添加一个。但它已经在那里了。

显然如果你使用setContentView(mTextureView);之后setContentView(R.layout.activity_main);整个布局就会消失。您正在将活动内容设置为单个视图而不是整个布局。

您还正在创建一个新的TextureView,而不是在 activity_main 布局上使用那个。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextureView = (TextureView)findViewById(R.id.textureView1);
    mTextureView.setSurfaceTextureListener(this);
}

我认为你应该做这样的事情。我从来没有测试过这个。

您似乎正在尝试使用 TextureView 进行相机预览,这里有一个很好的示例这里有一个更高级的版本。

于 2014-01-06T06:10:01.717 回答
-1

检查此代码,这对我有用。

MainActivity.java。

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends Activity implements SurfaceTextureListener {

   private TextureView myTexture;
   private Camera mCamera;

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myTexture = new TextureView(this);
      myTexture.setSurfaceTextureListener(this);
      setContentView(myTexture);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
   @SuppressLint("NewApi")
   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
   int arg2) {
      mCamera = Camera.open();
      Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
      myTexture.setLayoutParams(new FrameLayout.LayoutParams(
      previewSize.width, previewSize.height, Gravity.CENTER));
      try {
         mCamera.setPreviewTexture(arg0);
        } catch (IOException t) {
        }
      mCamera.startPreview();
      myTexture.setAlpha(1.0f);
      myTexture.setRotation(90.0f);
   }

   @Override
   public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
      mCamera.stopPreview();
      mCamera.release();
      return true;
   }

   @Override
   public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
   int arg2) {
   // TODO Auto-generated method stub
   }
   @Override
   public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
   // TODO Auto-generated method stub
   }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextureView
      android:id="@+id/textureView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.textureview"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />
   <uses-permission android:name="android.permission.CAMERA"/>
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity 
         android:name="com.example.textureview.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
       </activity>
   </application>
</manifest>
于 2014-01-06T07:25:07.597 回答