heryirawan1 0 Newbie Poster

Hi, I want to create motion detection application on android device. Could you help me?

Here 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();
    }
}

But It doesnt work, please help me.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.