package com.ford.openxc.nightvision;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;

import com.ford.openxc.webcam.WebcamPreview;

public class NightvisionView extends WebcamPreview {
    private final static String TAG = "NightvisionView";

    private static Paint sOverlayPaint = new Paint();
    private Bitmap mBitmapEdges;
    private Bitmap mBitmapObjectOverlay;
    private boolean mObjectInPreviousFrame = false;
    private MediaPlayer mMediaPlayer;

    public native void detectEdges(Bitmap imageBitmap, Bitmap edgeBitmap);
    public native boolean detectObjects(Bitmap edgeBitmap, Bitmap overlayBitmap);

    static {
        System.loadLibrary("nightvision");
        sOverlayPaint.setAlpha(130);
        sOverlayPaint.setColor(Color.RED);
    }

    public NightvisionView(Context context) {
        super(context);
        init();
    }

    public NightvisionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mMediaPlayer = MediaPlayer.create(getContext(), R.raw.alert);
    }

    void initializeBitmaps(Bitmap videoBitmap) {
        if(mBitmapEdges == null ||
                mBitmapEdges.getWidth() != videoBitmap.getWidth() ||
                mBitmapEdges.getHeight() != videoBitmap.getHeight()) {
            mBitmapEdges = Bitmap.createBitmap(videoBitmap.getWidth(),
                    videoBitmap.getHeight(), Bitmap.Config.ALPHA_8);
        }

        if(mBitmapObjectOverlay == null ||
                mBitmapObjectOverlay.getWidth() != videoBitmap.getWidth() ||
                mBitmapObjectOverlay.getHeight() != videoBitmap.getHeight()) {
            mBitmapObjectOverlay = Bitmap.createBitmap(videoBitmap.getWidth(),
                    videoBitmap.getHeight(), Bitmap.Config.ALPHA_8);
        }
    }

    protected void drawOnCanvas(Canvas canvas, Bitmap videoBitmap) {
        initializeBitmaps(videoBitmap);
        detectEdges(videoBitmap, mBitmapEdges);

        mBitmapObjectOverlay.eraseColor(Color.TRANSPARENT);
        boolean objectDetected = detectObjects(mBitmapEdges,
                mBitmapObjectOverlay);
        if (!mObjectInPreviousFrame && objectDetected) {
            mMediaPlayer.start();
            mObjectInPreviousFrame = true;
            Log.d(TAG, "Object detected");
        } else if(mObjectInPreviousFrame && !objectDetected) {
            mObjectInPreviousFrame = false;
            Log.d(TAG, "Object left frame");
        }

        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(videoBitmap, null, getViewingWindow(), null);
        canvas.drawBitmap(mBitmapObjectOverlay, null, getViewingWindow(),
                sOverlayPaint);
    }
}

@@ -438,16 +438,66 @@
 onView%22;
+%0A    private final int DETECT_FRAME_FREQUENCY = 4;
 %0A%0A    pr
@@ -707,16 +707,45 @@
 aPlayer;
+%0A    private int mFrameCount;
 %0A%0A    pu
@@ -2199,24 +2199,120 @@
 deoBitmap);%0A
+        canvas.drawColor(Color.BLACK);%0A%0A        if(mFrameCount == DETECT_FRAME_FREQUENCY) %7B%0A    
         dete
@@ -2344,24 +2344,28 @@
 mapEdges);%0A%0A
+    
         mBit
@@ -2420,16 +2420,20 @@
         
+    
 boolean 
@@ -2485,32 +2485,36 @@
                 
+   
+ 
 mBitmapObjectOve
@@ -2520,16 +2520,20 @@
 erlay);%0A
+    
         
@@ -2585,32 +2585,36 @@
 ) %7B%0A            
+  
+  
 mMediaPlayer.sta
@@ -2615,24 +2615,28 @@
 er.start();%0A
+    
             
@@ -2670,32 +2670,36 @@
 ue;%0A            
+  
+  
 Log.d(TAG, %22Obje
@@ -2709,24 +2709,28 @@
 detected%22);%0A
+    
         %7D el
@@ -2784,32 +2784,36 @@
 ) %7B%0A            
+ 
+   
 mObjectInPreviou
@@ -2824,24 +2824,28 @@
 me = false;%0A
+    
             
@@ -2889,49 +2889,98 @@
     
-%7D%0A%0A        canvas.drawColor(Color.BLACK);
+    %7D%0A%0A            mFrameCount = 0;%0A        %7D else %7B%0A            mFrameCount++;%0A        %7D%0A
 %0A   

Add an adjustable paramter to control how often obj. detection runs.