Textureview in Android example and Tutorial


Introduction 
          Textureview in android is used to display video, OpenGL scenes, live feed from a camera device etc., Say for example, what if I need to show a video in android app, in that case we can use textureview which makes it very simple.
Android Textureview will behave as a normal view by displaying videos in the same view. Textureview has key properties like, we can set the texture view from opaque to transparent and we can also rotate the textureview, which is quite interesting.
          Now, lets get into the example and see how this Android textureview can be implemented. Lets try to display live feed from camera device for this example.

Step 1 
         Using Android Textureview is very simple, all we need to do is, get its SurfaceView and this SurfaceView will render the content for us as shown below.

mTexture = new TextureView(this);
mTexture.setSurfaceTextureListener(this);
setContentView(mTexture);

Then we need to override  the below methods to complete our application :

public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) 
  - Invoked when a TextureView's SurfaceTexture is ready for use.
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0)
  - Invoked when the specified SurfaceTexture is about to be destroyed
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,int arg2)
  - Invoked when the SurfaceTexture's buffers size changed
public void onSurfaceTextureUpdated(SurfaceTexture arg0)
  - Invoked when the specified SurfaceTexture is updated through updateTexImage().

Step 2 
        When the Surface texture is available open the camera device and display its live feed as shown below :

mCamera = Camera.open();
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
mTexture.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
try {
mCamera.setPreviewTexture(arg0);
} catch (IOException t) {
}
mCamera.startPreview();

As mentioned earlier we can control views transparency and rotation with setAlpha() and setRotation() methods respectively.
mTexture.setAlpha(1.0f);
mTexture.setRotation(90.0f);
 
Setalpha(float) - Its value varies from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque.
SetRotation(float) - We can set values to rotation which is clockwise.

Now run the application in an android mobile with camera device, you should be able to see the camera's live feed in the screen. Below is a sample. Hope this example helps you better understand Textureview in Android

Note: Ensure to add camera permission to AndroidManifest.xml


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/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

MainActivity.java

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.widget.FrameLayout;
import java.io.IOException;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements SurfaceTextureListener {
private TextureView mTexture;
private Camera mCamera;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTexture = new TextureView(this);
mTexture.setSurfaceTextureListener(this);
setContentView(mTexture);
}

@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();
mTexture.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
try {
mCamera.setPreviewTexture(arg0);
} catch (IOException t) {
}
mCamera.startPreview();
mTexture.setAlpha(1.0f);
mTexture.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
}
}

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <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.androidtextureview.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>

SourceCode of this application
AndroidTextureView.zip





Reactions:

0 comments :

Post a Comment