2 Replies - 4006 Views - Last Post: 22 March 2012 - 11:06 PM

#1 heryirawan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 11

How to detect motion on android device

Posted 22 March 2012 - 04:35 PM

Hi, I want to create motion detection app, but it doesnt work , program doesnt detect motion. could you help me.

my code :

package com.iseng;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import android.hardware.*;
import android.hardware.Camera.PictureCallback;

public class IsengActivity extends Activity implements SurfaceHolder.Callback, Camera.PreviewCallback, PictureCallback  {
    
   
	Camera kamera;
    SurfaceHolder surface;
    SurfaceView kameraview;
    
    public byte dataOld[];
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        kameraview =(SurfaceView) findViewById(R.id. kameraku);
        surface = kameraview.getHolder();
        surface.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surface.addCallback(this);
        
        
        
    }


	@Override
	public void onPreviewFrame(byte[] data, Camera kamera) {
		
		if(data.length ==0) return;
		
		if(dataOld.length==0 )
			data=dataOld;
		
		int Isdiferent=0;
		
		int treshold =50;
		for (int x=0; x<data.length; x=+200)
		{
			if(Math.abs(data[x] - dataOld[x]) <= treshold )
			{
				Isdiferent +=1;
			}
			
			float precentage = Isdiferent / data.length;
			if (precentage > 20 )
			{
			
			kamera.takePicture(null,null,this);
			}
				
		}
	}


	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int h, int w) {
		kamera.startPreview();
	}


	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		kamera=Camera.open();
		
		try
		
		{
			Camera.Parameters parameter = kamera.getParameters();
			if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
			{
				parameter.set("orientation","portrait");
				kamera.setDisplayOrientation(90);
			}
			
			else
			{
				parameter.set("orientation","landscape");
				kamera.setDisplayOrientation(0);
			}
			
			kamera.setParameters(parameter);
			kamera.setPreviewDisplay(holder);
		}
		
		catch(IOException e)
		{
		kamera.release();
		
	    }
		kamera.startPreview();
	}


	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		kamera.stopPreview();
		kamera.release();
		
	}


	@Override
	public void onPictureTaken(byte[] data, Camera kamera) {
		
		// TODO Auto-generated method stub
				Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,new ContentValues());
				try
				
				{
					/*String sdcardStorage = Environment.getExternalStorageDirectory().toString();
					imageFileUri=Uri.parse(sdcardStorage + "/HeryMD/Test.jpg");*/
					
					 String sdcardStorage = Environment.getExternalStorageDirectory().toString();
					 File f = new File(sdcardStorage + "/HeryMD/Test.jpg");
					 OutputStream os = new FileOutputStream(f);
					 os.write(data);
					 os.flush();
					 os.close();
				} catch(FileNotFoundException e)
				{
					Toast t = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
					t.show();
				}
				
				
				catch(IOException e)
				{
					Toast t = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
					t.show();
				}
				
				kamera.startPreview();
	}
}



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

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IsengActivity"
            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>



please help me.

Thanks

This post has been edited by Atli: 22 March 2012 - 04:52 PM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: How to detect motion on android device

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: How to detect motion on android device

Posted 22 March 2012 - 04:58 PM

We sure can help you, but could you give us some information first, such as the errors that you are getting? Is DDMS giving you any? Or is it simply not detecting?
Was This Post Helpful? 0
  • +
  • -

#3 heryirawan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 11

Re: How to detect motion on android device

Posted 22 March 2012 - 11:06 PM

There is error :

Current file is not a match for the given config. what should I do for it?

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1