简体   繁体   中英

How to detect motion on android device

I want to create motion detector in android usingCamera.PreviewCallback , and comparing two images . Could you help me please?

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=+5)
    {
        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();
}

}

managing preview is a bit tricky, because you haev to take into accound orientation changes and activity lifecycle. But this is nicely encapsulated in this project ( See android demos in sources, and camera manager ):

http://sourceforge.net/projects/javaocr/

Another issue would be to determine movement. This could be done using image moments - just compute some domains over ixisting image and cmopare them with previous values ( of course, you will have to pick moments where there is not translation of rotation invariance ) (code computing image moments is also in project )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM