3

I'm trying to capture an image using TextureView, but the main.xml file is overridden by TextureView, and hence I cannot see the contents of the main.xml file. I'm trying to add TextureView into main.xml. Please give suggestions about how to improve my code.

Java:

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) {
    }
}

Layout resource 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" />
ankuranurag2
  • 2,300
  • 15
  • 30
Aswathy
  • 337
  • 2
  • 8
  • 18

2 Answers2

0

I dont understand you question.

You say you want to add a TextureView to your activity_main.xml layout. but it is already there.

Obviously if you use setContentView(mTextureView); after setContentView(R.layout.activity_main); the entire layout will be gone. You are setting the activity content to a single view instead of the entire layout.

You are also creating a new TextureView and not using the one on the activity_main layout.

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

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

I think you should be doing something like this. I never tested this though.

It seems that you are trying to make a camera preview with the TextureView, there is a good example here, and a more advanced version here.

string.Empty
  • 10,393
  • 4
  • 39
  • 67
  • View.addView(mTextureView); this line is showing me error continously. its asking me to add cast to Text_View..I ve updated my code pls check it – Aswathy Jan 06 '14 at 06:13
  • well `View.addView(mTextureView);` means that you are taking a `TextureView` from the layout, and adding a newly created `TextureView` and adding it to it. You dont need to have 2 `TextureViews` to start off with – string.Empty Jan 06 '14 at 06:15
  • @Nicholas Tyler...is it wrong to do so...it has to be done in dis way na? What can i do to eliminate this error? – Aswathy Jan 06 '14 at 06:18
  • i tried this before...it will just display the layout with hello world...there will be no texture view. Thats why i went for new Textureview – Aswathy Jan 06 '14 at 06:24
  • well, you do not need more than one `TextureView` i see that you removed the `setContentView(mTextureView);` which is better. Your problem is probably that you are putting a big margin on your xml, try removing this: `android:layout_marginLeft="372dp"` and this: `android:layout_marginTop="180dp"`. if you are testing on a small screen, its very easy to put the textureview outside the bounds of the screen. – string.Empty Jan 06 '14 at 06:28
  • @Nicolas...do u have any solution – Aswathy Jan 06 '14 at 08:55
-1

Check this code this is working for me.

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>
Sathish
  • 1,455
  • 1
  • 16
  • 22
  • butif we use setContentView(myTexture); after setContentView(R.layout.activity_main); the entire layout will be gone. i want to see the texture view as well as the layout. when i run this program it will show only the texture view. – Aswathy Jan 06 '14 at 07:33
  • Y are you using setcontentview after loading the layout? Your textureview with in the layout right. Then its sufficient to load only layout – Sathish Jan 06 '14 at 07:35
  • look the answer that u gave me super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTexture = new TextureView(this); myTexture.setSurfaceTextureListener(this); setContentView(myTexture); – Aswathy Jan 06 '14 at 07:36
  • yeah now i got it. Camera is not opened. Sorry I haven't used textureview so got confused. – Sathish Jan 06 '14 at 07:38
  • But i used it in my code...still invisible. in my xml there is a texture view as well as a textview "Hello world". I want to display both of this. But for now only Hello world is displayed – Aswathy Jan 06 '14 at 07:42
  • remove that layout from setcontentview – Sathish Jan 06 '14 at 07:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44590/discussion-between-sathish-and-aswathy) – Sathish Jan 06 '14 at 07:54