hi ....
write Following code in your project
first create camaratest android project.......
this example for take image and view in below image view check it........ and enjoy
1) CamaratestActivity.java
package com.in.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/*
* O@K tech...
*/
public class CamaratestActivity extends Activity implements SurfaceHolder.Callback ,OnClickListener {
Camera camera;
SurfaceView surfaceview;
SurfaceHolder mHolder ;
Button btnSnap;
ImageView mPic;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceview = (SurfaceView)findViewById(R.id.surfaceView1);// view camera view in main xml.... check it
mHolder = surfaceview.getHolder();
mHolder.addCallback(this);
btnSnap = (Button)findViewById(R.id.button1);
btnSnap.setOnClickListener(this);
mPic = (ImageView)findViewById(R.id.imageView1);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("surfacechanged is called");
try{
camera.setPreviewDisplay(mHolder);
Camera.Parameters parameter = camera.getParameters();
camera.setParameters(parameter);
camera.startPreview();
}
catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("surfacreate is called");
try{
camera = Camera.open();
}
catch (Exception e) {
System.out.println("Error:"+e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("surfacedestroy is called");
camera.stopPreview();
camera.release();
camera = null;
}
@Override
public void onClick(View v) {
camera.takePicture(shutter, raw, postview, jpeg);// this class for take pic .
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
camera.startPreview();
}
}, 1000);
//camera.startPreview();
}
ShutterCallback shutter = new ShutterCallback() {
@Override
public void onShutter() {
System.out.println("Shuttercallback picature call back.....");
}
} ;
PictureCallback raw = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("raw .. picature call back.....");
}
};
PictureCallback jpeg = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("jpeg picature call back.....");
Bitmap btmCamerapic = BitmapFactory.decodeByteArray(data,0,data.length);
mPic.setImageBitmap(btmCamerapic);
}
};
PictureCallback postview = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("postview... picature call back.....");
}
};
}
2)main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Snap Card..." />
<FrameLayout android:layout_width="fill_parent"
android:layout_height="250dp">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="250dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="Snap" />
</RelativeLayout>
</FrameLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.in.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:icon="@drawable/camera"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".CamaratestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
All iz well
Output
write Following code in your project
first create camaratest android project.......
this example for take image and view in below image view check it........ and enjoy
1) CamaratestActivity.java
package com.in.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/*
* O@K tech...
*/
public class CamaratestActivity extends Activity implements SurfaceHolder.Callback ,OnClickListener {
Camera camera;
SurfaceView surfaceview;
SurfaceHolder mHolder ;
Button btnSnap;
ImageView mPic;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceview = (SurfaceView)findViewById(R.id.surfaceView1);// view camera view in main xml.... check it
mHolder = surfaceview.getHolder();
mHolder.addCallback(this);
btnSnap = (Button)findViewById(R.id.button1);
btnSnap.setOnClickListener(this);
mPic = (ImageView)findViewById(R.id.imageView1);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("surfacechanged is called");
try{
camera.setPreviewDisplay(mHolder);
Camera.Parameters parameter = camera.getParameters();
camera.setParameters(parameter);
camera.startPreview();
}
catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("surfacreate is called");
try{
camera = Camera.open();
}
catch (Exception e) {
System.out.println("Error:"+e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("surfacedestroy is called");
camera.stopPreview();
camera.release();
camera = null;
}
@Override
public void onClick(View v) {
camera.takePicture(shutter, raw, postview, jpeg);// this class for take pic .
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
camera.startPreview();
}
}, 1000);
//camera.startPreview();
}
ShutterCallback shutter = new ShutterCallback() {
@Override
public void onShutter() {
System.out.println("Shuttercallback picature call back.....");
}
} ;
PictureCallback raw = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("raw .. picature call back.....");
}
};
PictureCallback jpeg = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("jpeg picature call back.....");
Bitmap btmCamerapic = BitmapFactory.decodeByteArray(data,0,data.length);
mPic.setImageBitmap(btmCamerapic);
}
};
PictureCallback postview = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("postview... picature call back.....");
}
};
}
2)main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Snap Card..." />
<FrameLayout android:layout_width="fill_parent"
android:layout_height="250dp">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="250dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="Snap" />
</RelativeLayout>
</FrameLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
3) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.in.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:icon="@drawable/camera"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".CamaratestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
All iz well
Output
Android User Session Management using Shared Preferences
ReplyDeletehttp://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
bhai aa link ma Error establishing a database connection ni error ave 6
DeleteHello,Raviraj it's good app but i have one problem why the application Camera API is exit after taking snep ?
ReplyDeleteI tried apk in Bluestack software which is having Android OS in PC for Hardware use like camera.....
Reply........plz
m waiting....
hi bro....... this application run in sony-xpiria and samsung note . it's ok....... in 4.x and
ReplyDeletemay be add following line in your code may be problem will be solve....
Camera.Parameters parameters = Camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
parameters.setPictureSize(640, 480);
Camera.setParameters(parameters);
Hello Shukhadiya Raviraj,
ReplyDeletei want to know that how surface Holder work when button is clicked as well as camera goes to open and you didnt mention that how to capture the image and record the video.......Thanks
...............Reply must
Brother great tutorial.....You saved my time....:)
ReplyDelete