Я хочу создать детектор движения в Android usingCamera.PreviewCallback
и сравнивать два изображения.Не могли бы вы мне помочь?
открытый класс IsengActivity расширяет 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();
}
}