Capturing Image without user interaction
Step: 1 ==> This is service to capture photo in background Process
To create the Service Class - CapPhoto(service)
service code here.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Service;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Environment;
import android.os.IBinder;
import android.os.StrictMode;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CapPhoto extends Service
{
private SurfaceHolder sHolder;
private Camera mCamera;
private Parameters parameters;
@Override
public void onCreate()
{
super.onCreate();
Log.d("CAM", "start");
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);};
Thread myThread = null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (Camera.getNumberOfCameras() >= 2) {
mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT); }
if (Camera.getNumberOfCameras() < 2) {
mCamera = Camera.open(); }
SurfaceView sv = new SurfaceView(getApplicationContext());
try {
mCamera.setPreviewDisplay(sv.getHolder());
parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
} catch (IOException e) { e.printStackTrace(); }
sHolder = sv.getHolder();
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(final byte[] data, Camera camera)
{
FileOutputStream outStream = null;
try{
File sd = new File(Environment.getExternalStorageDirectory(), "A");
if(!sd.exists()) {
sd.mkdirs();
Log.i("FO", "folder" + Environment.getExternalStorageDirectory());
}
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String tar = (sdf.format(cal.getTime()));
outStream = new FileOutputStream(sd+tar+".jpg");
outStream.write(data); outStream.close();
Log.i("CAM", data.length + " byte written to:"+sd+tar+".jpg");
camkapa(sHolder);
} catch (FileNotFoundException e){
Log.d("CAM", e.getMessage());
} catch (IOException e){
Log.d("CAM", e.getMessage());
}}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void camkapa(SurfaceHolder sHolder) {
if (null == mCamera)
return;
mCamera.stopPreview();
mCamera.release();
mCamera = null;
Log.i("CAM", " closed");
}
}
Step:2 Activity File - name is :Second.java
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
public class Second extends Activity {
Intent service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Calendar cal = Calendar.getInstance();
service = new Intent(getBaseContext(), CapPhoto.class);
cal.add(Calendar.SECOND, 15);
//TAKE PHOTO EVERY 15 SECONDS
PendingIntent pintent = PendingIntent.getService(this, 0, service, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
60*60*1000, pintent);
startService(service);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
step 3: Android manife
st file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dbsource.dbinsert"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Second"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".CapPhoto" android:enabled="true">
<intent-filter>
</intent-filter>
</service>
</application>
</manifest>
Click here - capturing image without user interaction in android
if you have any doubt call me: chandru-9677089975
Step: 1 ==> This is service to capture photo in background Process
To create the Service Class - CapPhoto(service)
service code here.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Service;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Environment;
import android.os.IBinder;
import android.os.StrictMode;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CapPhoto extends Service
{
private SurfaceHolder sHolder;
private Camera mCamera;
private Parameters parameters;
@Override
public void onCreate()
{
super.onCreate();
Log.d("CAM", "start");
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);};
Thread myThread = null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (Camera.getNumberOfCameras() >= 2) {
mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT); }
if (Camera.getNumberOfCameras() < 2) {
mCamera = Camera.open(); }
SurfaceView sv = new SurfaceView(getApplicationContext());
try {
mCamera.setPreviewDisplay(sv.getHolder());
parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
} catch (IOException e) { e.printStackTrace(); }
sHolder = sv.getHolder();
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(final byte[] data, Camera camera)
{
FileOutputStream outStream = null;
try{
File sd = new File(Environment.getExternalStorageDirectory(), "A");
if(!sd.exists()) {
sd.mkdirs();
Log.i("FO", "folder" + Environment.getExternalStorageDirectory());
}
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String tar = (sdf.format(cal.getTime()));
outStream = new FileOutputStream(sd+tar+".jpg");
outStream.write(data); outStream.close();
Log.i("CAM", data.length + " byte written to:"+sd+tar+".jpg");
camkapa(sHolder);
} catch (FileNotFoundException e){
Log.d("CAM", e.getMessage());
} catch (IOException e){
Log.d("CAM", e.getMessage());
}}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void camkapa(SurfaceHolder sHolder) {
if (null == mCamera)
return;
mCamera.stopPreview();
mCamera.release();
mCamera = null;
Log.i("CAM", " closed");
}
}
Step:2 Activity File - name is :Second.java
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
public class Second extends Activity {
Intent service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Calendar cal = Calendar.getInstance();
service = new Intent(getBaseContext(), CapPhoto.class);
cal.add(Calendar.SECOND, 15);
//TAKE PHOTO EVERY 15 SECONDS
PendingIntent pintent = PendingIntent.getService(this, 0, service, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
60*60*1000, pintent);
startService(service);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
step 3: Android manife
st file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dbsource.dbinsert"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Second"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".CapPhoto" android:enabled="true">
<intent-filter>
</intent-filter>
</service>
</application>
</manifest>
Click here - capturing image without user interaction in android
if you have any doubt call me: chandru-9677089975
can u put the code for main activity as well
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHELLO SIR,
ReplyDeletecan you just share the complete code i want to implement this in my application..
Thanks
Can you share complete code to me as well.
ReplyDeleteput the complete code u idiot
ReplyDeleteHey don't use unnecessary words
Delete