I'm making a motion detection app that takes picture on detection of motion. That is working fine. Now I'm trying to implement a movable custom view on top of my surfaceView to define the area of motion detection. I have found a function (AreaDetectorView.java) to do that. But as I'm new to Android programming, it is difficult for me to understand how to implement that function in my code. Please explain what changes are to be made in the layout file and the MotionDetectionActivity, so that I will get a view like this:
Question on StackOverflow
MotionDetectionActivity.java
AreaDetectorView.java
main.xml (Layout)
Question on StackOverflow
MotionDetectionActivity.java
Code:
public class MotionDetectionActivity extends SensorsActivity {
private static final String TAG = "MotionDetectionActivity";
private static SurfaceView preview = null;
private static SurfaceHolder previewHolder = null;
private static Camera camera = null;
private static boolean inPreview = false;
private static long mReferenceTime = 0;
private static IMotionDetection detector = null;
public static MediaPlayer song;
public static Vibrator mVibrator;
private static volatile AtomicBoolean processing = new AtomicBoolean(false);
/**
* {@inheritDoc}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
mVibrator = (Vibrator)this.getSystemService(VIBRATOR_SERVICE);
preview = (SurfaceView) findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
if (Preferences.USE_RGB) {
detector = new RgbMotionDetection();
} else if (Preferences.USE_LUMA) {
detector = new LumaMotionDetection();
} else {
// Using State based (aggregate map)
detector = new AggregateLumaMotionDetection();
}
}
/**
* {@inheritDoc}
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
/**
* {@inheritDoc}
*/
@Override
public void onPause() {
super.onPause();
if(song!=null && song.isPlaying())
{
song.stop();}
camera.setPreviewCallback(null);
if (inPreview) camera.stopPreview();
inPreview = false;
camera.release();
camera = null;
}
/**
* {@inheritDoc}
*/
@Override
public void onResume() {
super.onResume();
camera = Camera.open();
}
private PreviewCallback previewCallback = new PreviewCallback() {
/**
* {@inheritDoc}
*/
@Override
public void onPreviewFrame(byte[] data, Camera cam) {
if (data == null) return;
Camera.Size size = cam.getParameters().getPreviewSize();
if (size == null) return;
if (!GlobalData.isPhoneInMotion()) {
DetectionThread thread = new DetectionThread(data, size.width, size.height);
thread.start();
}
}
};
private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
/**
* {@inheritDoc}
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(previewHolder);
camera.setPreviewCallback(previewCallback);
} catch (Throwable t) {
Log.e("Prek", "Exception in setPreviewDisplay()", t);
}
}
/**
* {@inheritDoc}
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(camera != null) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);
if (size != null) {
parameters.setPreviewSize(size.width, size.height);
Log.d(TAG, "Using width=" + size.width + " height=" + size.height);
}
camera.setParameters(parameters);
camera.startPreview();
inPreview = true;
}
}
/**
* {@inheritDoc}
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Ignore
}
};
private static Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) result = size;
}
}
}
return result;
}
private static final class DetectionThread extends Thread {
private byte[] data;
private int width;
private int height;
public DetectionThread(byte[] data, int width, int height) {
this.data = data;
this.width = width;
this.height = height;
}
/**
* {@inheritDoc}
*/
@Override
public void run() {
if (!processing.compareAndSet(false, true)) return;
// Log.d(TAG, "BEGIN PROCESSING...");
try {
// Previous frame
int[] pre = null;
if (Preferences.SAVE_PREVIOUS) pre = detector.getPrevious();
// Current frame (with changes)
// long bConversion = System.currentTimeMillis();
int[] img = null;
if (Preferences.USE_RGB) {
img = ImageProcessing.decodeYUV420SPtoRGB(data, width, height);
if (img != null && detector.detect(img, width, height))
{
if(song!=null && !song.isPlaying())
{
song.start();
mVibrator.vibrate(50);
}
}
else
{
if(song!=null && song.isPlaying())
{
song.pause();
}
}
}
int[] org = null;
if (Preferences.SAVE_ORIGINAL && img != null) org = img.clone();
if (img != null && detector.detect(img, width, height)) {
// The delay is necessary to avoid taking a picture while in
// the
// middle of taking another. This problem can causes some
// phones
// to reboot.
long now = System.currentTimeMillis();
if (now > (mReferenceTime + Preferences.PICTURE_DELAY)) {
mReferenceTime = now;
Bitmap previous = null;
if (Preferences.SAVE_PREVIOUS && pre != null) {
if (Preferences.USE_RGB) previous = ImageProcessing.rgbToBitmap(pre, width, height);
else previous = ImageProcessing.lumaToGreyscale(pre, width, height);
}
Bitmap original = null;
if (Preferences.SAVE_ORIGINAL && org != null) {
if (Preferences.USE_RGB) original = ImageProcessing.rgbToBitmap(org, width, height);
else original = ImageProcessing.lumaToGreyscale(org, width, height);
}
Bitmap bitmap = null;
if (Preferences.SAVE_CHANGES) {
if (Preferences.USE_RGB) bitmap = ImageProcessing.rgbToBitmap(img, width, height);
else bitmap = ImageProcessing.lumaToGreyscale(img, width, height);
}
Log.i(TAG, "Saving.. previous=" + previous + " original=" + original + " bitmap=" + bitmap);
Looper.prepare();
new SavePhotoTask().execute(previous, original, bitmap);
} else {
Log.i(TAG, "Not taking picture because not enough time has passed since the creation of the Surface");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
processing.set(false);
}
// Log.d(TAG, "END PROCESSING...");
processing.set(false);
}
};
private static final class SavePhotoTask extends AsyncTask<Bitmap, Integer, Integer> {
/**
* {@inheritDoc}
*/
@Override
protected Integer doInBackground(Bitmap... data) {
for (int i = 0; i < data.length; i++) {
Bitmap bitmap = data[i];
String name = String.valueOf(System.currentTimeMillis());
if (bitmap != null) save(name, bitmap);
}
return 1;
}
private void save(String name, Bitmap bitmap) {
File photo = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
if (photo.exists()) photo.delete();
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
}
}
AreaDetectorView.java
Code:
public class AreaDetectorView extends LinearLayout {
public static int Width;
public static int Height;
private static Paint BoxPaint = null;
private static Paint TextPaint = null;
private static Paint ArrowPaint = null;
private static Path mPath = null;
private static Rect mRect = null;
private static int lastX, lastY = 0;
private static boolean mBoxTouched = false;
private static boolean mArrowTouched = false;
private static int ArrowWidth = 0;
private static Paint BoxPaint2 = null;
public AreaDetectorView(Context context) {
super(context);
}
public AreaDetectorView(Context context, AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
if (!this.getRootView().isInEditMode()) {
ArrowWidth =GetDisplayPixel(context, 30);
}
//InitDetectionArea();
InitMemberVariables();
setWillNotDraw(false);
}
public static int GetDisplayPixel(Context paramContext, int paramInt)
{
return (int)(paramInt * paramContext.getResources().getDisplayMetrics().density + 0.5F);
}
public static void InitMemberVariables() {
if (BoxPaint == null) {
BoxPaint = new Paint();
BoxPaint.setAntiAlias(true);
BoxPaint.setStrokeWidth(2.0f);
//BoxPaint.setStyle(Style.STROKE);
BoxPaint.setStyle(Style.FILL_AND_STROKE);
BoxPaint.setColor(AppShared.gResources.getColor(R.color.bwff_60));
}
if (ArrowPaint == null) {
ArrowPaint = new Paint();
ArrowPaint.setAntiAlias(true);
ArrowPaint.setColor(AppShared.gResources.getColor(R.color.redDD));
ArrowPaint.setStyle(Style.FILL_AND_STROKE);
}
if (TextPaint == null) {
TextPaint = new Paint();
TextPaint.setColor(0xFFFFFFFF);
TextPaint.setTextSize(16);
//txtPaint.setTypeface(lcd);
TextPaint.setStyle(Style.FILL_AND_STROKE);
}
if (mPath == null) {
mPath = new Path();
} else {
mPath.reset();
}
if (mRect == null) {
mRect = new Rect();
}
if (BoxPaint2 == null) {
BoxPaint2 = new Paint();
BoxPaint2.setAntiAlias(true);
BoxPaint2.setStrokeWidth(2.0f);
//BoxPaint.setStyle(Style.STROKE);
BoxPaint2.setStyle(Style.STROKE);
BoxPaint2.setColor(AppShared.gResources.getColor(R.color.bwff_9e));
}
}
public static void InitDetectionArea() {
try {
int w = AppShared.DetectionArea.width();
int h = AppShared.DetectionArea.height();
int x = AppShared.DetectionArea.left;
int y = AppShared.DetectionArea.top;
// ver 2.6.0
if (AppShared.DetectionArea.left == 1
&& AppShared.DetectionArea.top == 1
&& AppShared.DetectionArea.right == 1
&& AppShared.DetectionArea.bottom == 1) {
w = AppShared.DisplayWidth / 4;
h = AppShared.DisplayHeight / 3;
// ver 2.5.9
w = Width / 4;
h = Height / 3;
AppShared.DetectorWidth = w;
AppShared.DetectorHeight = h;
x = (AppShared.DisplayWidth / 2) - (w / 2);
y = (AppShared.DisplayHeight / 2) - (h / 2);
// ver 2.5.9
x = (Width / 2) - (w / 2);
y = (Height / 2) - (h / 2);
}
//AppShared.DetectionArea = new Rect(x, x, x + AppShared.DetectorWidth, x + AppShared.DetectorHeight);
AppShared.DetectionArea = new Rect(x, y, x + w, y + h);
AppShared.gDetectionBitmapInt = new int[AppShared.DetectionArea.width() * AppShared.DetectionArea.height()];
AppShared.gDetectionBitmapIntPrev = new int[AppShared.DetectionArea.width() * AppShared.DetectionArea.height()];
} catch (Exception e) {
e.printStackTrace();
}
}
public static void SetDetectionArea(int x, int y, int w, int h) {
try {
AppShared.DetectionArea = new Rect(x, y, w, h);
} catch (Exception e) {
e.printStackTrace();
}
}
private void DrawAreaBox(Canvas canvas) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
try {
if (this.getRootView().isInEditMode()) {
super.dispatchDraw(canvas);
return;
}
//canvas.save(Canvas.MATRIX_SAVE_FLAG);
//AppShared.DetectionAreaOrient = UtilGeneralHelper.GetDetectRectByOrientation();
canvas.drawColor(0);
mPath.reset();
canvas.drawRect(AppShared.DetectionArea, BoxPaint);
mPath.moveTo(AppShared.DetectionArea.right - ArrowWidth, AppShared.DetectionArea.bottom);
mPath.lineTo(AppShared.DetectionArea.right, AppShared.DetectionArea.bottom - ArrowWidth);
mPath.lineTo(AppShared.DetectionArea.right, AppShared.DetectionArea.bottom);
mPath.lineTo(AppShared.DetectionArea.right - ArrowWidth, AppShared.DetectionArea.bottom);
mPath.close();
canvas.drawPath(mPath, ArrowPaint);
mPath.reset();
//canvas.drawRect(AppShared.DetectionAreaOrient, BoxPaint2);
//canvas.drawRect(AppShared.DetectionAreaOrientPort, BoxPaint2);
TextPaint.setTextSize(16);
//TextPaint.setLetterSpacing(2);
TextPaint.setColor(getResources().getColor(R.color.bwff));
TextPaint.getTextBounds(getResources().getString(R.string.str_detectarea), 0, 1, mRect);
canvas.drawText(getResources().getString(R.string.str_detectarea),
AppShared.DetectionArea.left + 4,
AppShared.DetectionArea.top + 4 + mRect.height(),
TextPaint);
int recH = mRect.height();
TextPaint.setStrokeWidth(1.2f);
TextPaint.setTextSize(18);
TextPaint.setColor(getResources().getColor(R.color.redD_9e));
TextPaint.getTextBounds(getResources().getString(R.string.str_dragandmove), 0, 1, mRect);
canvas.drawText(getResources().getString(R.string.str_dragandmove),
AppShared.DetectionArea.left + 4,
AppShared.DetectionArea.top + 20 + mRect.height()*2,
TextPaint);
TextPaint.getTextBounds(getResources().getString(R.string.str_scalearea), 0, 1, mRect);
canvas.drawText(getResources().getString(R.string.str_scalearea),
AppShared.DetectionArea.left + 4,
AppShared.DetectionArea.top + 36 + mRect.height()*3,
TextPaint);
super.dispatchDraw(canvas);
//canvas.restore();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDraw(Canvas canvas) {
try {
super.onDraw(canvas);
invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean retValue = true;
int X = (int)event.getX();
int Y = (int)event.getY();
//AppMain.txtLoc.setText(String.valueOf(X) + ", " + String.valueOf(Y));
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mBoxTouched = TouchedInBoxArea(X, Y);
//AppMain.txtLoc.setText("BoxTouched: " + String.valueOf(mBoxTouched));
if (!mBoxTouched) break;
lastX = X;
lastY = Y;
BoxPaint.setStyle(Style.FILL_AND_STROKE);
BoxPaint.setColor(AppShared.gResources.getColor(R.color.redD_9e));
mArrowTouched = TouchedInArrow(X, Y);
//AppMain.txtLoc.setText("ArrowTouched: " + String.valueOf(mBoxTouched));
if (mArrowTouched) {
ArrowPaint.setColor(AppShared.gResources.getColor(R.color.bwff_9e));
}
break;
case MotionEvent.ACTION_MOVE:
if (!mBoxTouched) break;
int moveX = X - lastX;
int moveY = Y - lastY;
//AppMain.txtLoc.setText("Move X, Y: " + String.valueOf(moveX) + "," + String.valueOf(moveY));
if (!mArrowTouched) {
if (AppShared.DetectionArea.left + moveX < 0) {
break;
}
// if (AppShared.DetectionArea.right + moveX > AppShared.gDisplay.getWidth()) {
// break;
// }
// ver 2.5.9
if (AppShared.DetectionArea.right + moveX > Width) {
break;
}
if (AppShared.DetectionArea.top + moveY < 0) {
break;
}
// if (AppShared.DetectionArea.bottom + moveY > AppShared.gDisplay.getHeight()) {
// break;
// }
// ver 2.5.9
if (AppShared.DetectionArea.bottom + moveY > Height) {
break;
}
}
if (mArrowTouched) {
if ((AppShared.DetectionArea.width() + moveX) < ArrowWidth * 2){
break;
}
if ((AppShared.DetectionArea.height() + moveY) < ArrowWidth * 2) {
break;
}
AppShared.DetectionArea.right += moveX;
AppShared.DetectionArea.bottom += moveY;
//Log.i("DBG", "W,H: " + String.valueOf(AppShared.DetectionArea.width()) + "," + String.valueOf(AppShared.DetectionArea.height()));
} else {
AppShared.DetectionArea.left += moveX;
AppShared.DetectionArea.right += moveX;
AppShared.DetectionArea.top += moveY;
AppShared.DetectionArea.bottom += moveY;
}
lastX = X;
lastY = Y;
//AppMain.txtLoc.setText(String.valueOf(AppShared.DetectionArea.left) + ", " + String.valueOf(AppShared.DetectionArea.top));
break;
case MotionEvent.ACTION_UP:
mBoxTouched = false;
mArrowTouched = false;
//BoxPaint.setStyle(Style.STROKE);
BoxPaint.setStyle(Style.FILL_AND_STROKE);
BoxPaint.setColor(AppShared.gResources.getColor(R.color.bwff_60));
ArrowPaint.setColor(AppShared.gResources.getColor(R.color.redDD));
//AppMain.txtLoc.setText(String.valueOf(AppShared.DetectionArea.left) + ", " + String.valueOf(AppShared.DetectionArea.top));
if (AppShared.DetectionArea.left < 0) {
AppShared.DetectionArea.left = 0;
}
// if (AppShared.DetectionArea.right > AppShared.gDisplay.getWidth()) {
// AppShared.DetectionArea.right = AppShared.gDisplay.getWidth();
// }
// ver 2.5.9
if (AppShared.DetectionArea.right > Width) {
AppShared.DetectionArea.right = Width;
}
if (AppShared.DetectionArea.top < 0) {
AppShared.DetectionArea.top = 0;
}
// if (AppShared.DetectionArea.bottom > AppShared.gDisplay.getHeight()) {
// AppShared.DetectionArea.bottom = AppShared.gDisplay.getHeight();
// }
if (AppShared.DetectionArea.bottom > Height) {
AppShared.DetectionArea.bottom = Height;
}
AppShared.gDetectionBitmapInt = new int[AppShared.DetectionArea.width() * AppShared.DetectionArea.height()];
AppShared.gDetectionBitmapIntPrev = new int[AppShared.DetectionArea.width() * AppShared.DetectionArea.height()];
//AppShared.gDetectionBitmapInt = null;
//AppShared.gDetectionBitmapIntPrev = null;
String area = String.valueOf(AppShared.DetectionArea.left)
+ "," + String.valueOf(AppShared.DetectionArea.top)
+ "," + String.valueOf(AppShared.DetectionArea.right)
+ "," + String.valueOf(AppShared.DetectionArea.bottom);
// UtilGeneralHelper.SavePreferenceSetting(AppShared.gContext, AppShared.PREF_DETECTION_AREA_KEY, area);
break;
}
invalidate();
return retValue;
}
private boolean TouchedInBoxArea(int x, int y) {
boolean retValue = false;
try {
if (x > AppShared.DetectionArea.left && x < AppShared.DetectionArea.right) {
if (y > AppShared.DetectionArea.top && y < AppShared.DetectionArea.bottom) {
retValue = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return retValue;
}
private boolean TouchedInArrow(int x, int y) {
boolean retValue = false;
try {
if (x > AppShared.DetectionArea.right - ArrowWidth && x < AppShared.DetectionArea.right) {
if (y > AppShared.DetectionArea.bottom - ArrowWidth && y < AppShared.DetectionArea.bottom) {
retValue = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return retValue;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
Width = width;
Height = height;
InitDetectionArea();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
for (int i = 0; i < this.getChildCount()-1; i++){
(this.getChildAt(i)).layout(l, t, r, b);
}
if (changed) {
// check width height
if (r != Width || b != Height) {
// size does not match
}
}
}
}
main.xml (Layout)
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/preview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/preview">
</SurfaceView>
</FrameLayout>
<LinearLayout
android:layout_below="@id/preview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
</LinearLayout>
<ImageView android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/wm"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trigcount"
android:layout_alignTop="@+id/myImageView"
android:layout_centerHorizontal="true"
android:textColor="@android:color/white" />
</RelativeLayout>