//Main Activity
package com.demo;
import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewDemo extends Activity implements SurfaceHolder.Callback
{
Camera c;
SurfaceView sv;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sv=(SurfaceView) findViewById(R.id.surfaceView1);
sv.getHolder().addCallback(this);
sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
c = Camera.open();
}
public void onPause()
{
super.onPause();
c.stopPreview();
}
public void onDestroy()
{
super.onDestroy();
c.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Camera.Parameters params = c.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
c.setDisplayOrientation(90);
c.startPreview();
}
public void surfaceCreated(SurfaceHolder holder)
{
try
{
c.setPreviewDisplay(sv.getHolder());
}catch(Exception e)
{
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.i("PREVIEW","surfaceDestroyed");
}
}
//main.xml file Coding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SurfaceView android:layout_height="wrap_content" android:id="@+id/surfaceView1" android:layout_width="wrap_content"></SurfaceView>
</LinearLayout>
//manifest file Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SurfaceViewDemo"
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>
//also add the user permission for Camera Support
package com.demo;
import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewDemo extends Activity implements SurfaceHolder.Callback
{
Camera c;
SurfaceView sv;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sv=(SurfaceView) findViewById(R.id.surfaceView1);
sv.getHolder().addCallback(this);
sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
c = Camera.open();
}
public void onPause()
{
super.onPause();
c.stopPreview();
}
public void onDestroy()
{
super.onDestroy();
c.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Camera.Parameters params = c.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
c.setDisplayOrientation(90);
c.startPreview();
}
public void surfaceCreated(SurfaceHolder holder)
{
try
{
c.setPreviewDisplay(sv.getHolder());
}catch(Exception e)
{
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.i("PREVIEW","surfaceDestroyed");
}
}
//main.xml file Coding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SurfaceView android:layout_height="wrap_content" android:id="@+id/surfaceView1" android:layout_width="wrap_content"></SurfaceView>
</LinearLayout>
//manifest file Coding
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SurfaceViewDemo"
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>
//also add the user permission for Camera Support
No comments:
Post a Comment