如何检测Android设备上的动作

时间:2012-03-22 04:23:06

标签: android android-ndk

我想在android usingCamera.PreviewCallback中创建动作检测器,并比较两个图像。 你能帮帮我吗?

public class IsengActivity extends Activity实现了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();
}

}

1 个答案:

答案 0 :(得分:0)

管理预览有点棘手,因为你不得不考虑方向改变 和活动生命周期。但这很好地封装在这个项目中(参见源代码中的android demos和相机管理器):

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

另一个问题是确定运动。这可以使用图像时刻来完成 - 只需在图像上计算一些域,并使用之前的值进行cmop(当然,您必须选择没有旋转不变性平移的时刻) (代码计算图像时刻也在项目中)