Created a Camera Fragment & Activity to start testing computer vision

This commit is contained in:
2012-01-29 15:09:12 -05:00
parent a8bdfa8407
commit 8c04578c21
9 changed files with 448 additions and 1 deletions

View File

@@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:icon="@drawable/icon"
@@ -47,6 +48,9 @@
</activity>
<uses-library android:name="com.google.android.maps" />
<activity android:name="CameraActivity" >
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/camera_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/camera_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.TwentyCodes.android.IOIOTruck.CameraFragment" >
<!-- Preview: layout=@layout/camera_fragment -->
</fragment>
<TextView
android:id="@+id/log_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/camera_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="@+id/cameraSurface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>

View File

@@ -42,6 +42,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/nav_activity" />
<Button
android:id="@+id/camera_activity_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/camera_activity" />
<RelativeLayout
android:id="@+id/relativeLayout1"

View File

@@ -17,7 +17,6 @@
android:layout_weight="2" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"

View File

@@ -26,5 +26,6 @@
<string name="no_gps_signal">No GPS signal at this time, please try agian</string>
<string name="point_selected">Location Selected: </string>
<string name="dest_reached">Destination reached</string>
<string name="camera_activity">Camera Activity</string>
</resources>

View File

@@ -0,0 +1,102 @@
/**
* CameraActivity.java
* @date Jan 29, 2012
* @author ricky barrette
* @author Twenty Codes, LLC
*
* Copyright 2012 Richard Barrette
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.TwentyCodes.android.IOIOTruck;
import com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
/**
* This activity will be used to test the object avoidance algorithms
*
* TODO
* + drive the robot forward
* + steer the robot to avoid object in front of it
* @author ricky barrette
*/
public class CameraActivity extends FragmentActivity implements IOIOTruckThreadListener {
private static final String TAG = "CameraActivity";
private IOIOTruckManager mIOIOManager;
private WakeLock mWakeLock;
private TextView mLogTextView;
/**
* Called when the activity is being created
* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.camera_activity);
mLogTextView = (TextView) findViewById(R.id.log_textView);
}
/**
* Called when the activity is pausing
* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onPause()
*/
@Override
protected void onPause() {
super.onPause();
try {
mIOIOManager.abort();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(mWakeLock.isHeld())
mWakeLock.release();
}
/**
* Called when the activity is resuming
* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onResume()
*/
@Override
protected void onResume() {
mIOIOManager = new IOIOTruckManager(this, this);
mIOIOManager.start();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
mWakeLock.acquire();
super.onResume();
}
/**
* Called when the IOIO Manager wants to log something
* (non-Javadoc)
* @see com.TwentyCodes.android.IOIOTruck.IOIOTruckManager.IOIOTruckThreadListener#onLogUpdate(java.lang.String)
*/
@Override
public void onLogUpdate(String log) {
mLogTextView.setText(log);
}
}

View File

@@ -0,0 +1,290 @@
/**
* CameraFragment.java
* @date Jan 29, 2012
* @author ricky barrette
* @author Twenty Codes, LLC
*
* Copyright 2012 Richard Barrette
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.TwentyCodes.android.IOIOTruck;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.TwentyCodes.android.location.R;
/**
* This camera fragment will be used to maintain the camera, display a preview on the screen, display a vanishing point overlay on the preview
* @author ricky barrette
*/
public class CameraFragment extends Fragment {
/**
* A interface used to receive steering updates generated by the camera
* @author ricky barrette
*/
public interface CameraListener{
/**
* Called when there is new information
* @param error
* @author ricky barrette
*/
public void onCameraUpdate(int steeringModifier);
}
private boolean isDrawingView;
private Camera mCamera;
private Size mPreviewSize;
private byte[] mImageData;
private PreviewOverlay mPreviewOverlay;
private SurfaceView mPreview;
private CameraListener mListener;
/**
* Creates a new VanishingPointCameraFragment
* @author ricky barrette
*/
public CameraFragment() {
super();
}
/**
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
* @author ricky barrette
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_fragment, container, false);
/*
* setup augmented preview overlay
*/
FrameLayout fl = (FrameLayout) view.findViewById(R.id.camera_frame);
mPreviewOverlay = new PreviewOverlay(this.getActivity());
fl.addView(mPreviewOverlay);
/*
* setup camera preview
*/
mPreview = (SurfaceView) view.findViewById(R.id.cameraSurface);
SurfaceHolder cameraHolder = mPreview.getHolder();
cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraHolder.addCallback(new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
});
return view;
}
/**
* Called when the application is resuming
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onResume()
* @author ricky barrette
*/
@Override
public void onResume() {
super.onResume();
/*
* open camera
*/
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
Camera.Parameters params = mCamera.getParameters();
/*
* find smallest preview size
*/
List<Size> sizes = params.getSupportedPreviewSizes();
Iterator<Size> iter = sizes.iterator();
mPreviewSize = sizes.get(0);
int minPixelCount = mPreviewSize.width * mPreviewSize.height;
while (iter.hasNext()) {
Size size = iter.next();
int pixelCount = size.height * size.width;
if (pixelCount < minPixelCount) {
minPixelCount = pixelCount;
mPreviewSize = size;
}
}
params.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mPreviewOverlay.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mImageData = new byte[mPreviewSize.width*mPreviewSize.height];
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (!isDrawingView) {
mImageData = data;
mPreviewOverlay.invalidate();
isDrawingView = true;
}
}
});
params.setRotation(90);
mCamera.setParameters(params);
}
/**
* Called when the application is pausing
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onPause()
* @author ricky barrette
*/
public void onPause() {
super.onPause();
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
}
/**
* @return the mError
*/
public void setListener(CameraListener listener) {
mListener = listener;
}
/**
* This View view will be displayed over the image preview.
* It will be used to display a human readable overlay
* @author ricky barrette
*/
private class PreviewOverlay extends View {
private int imageWidth, imageHeight, mHorizonOffset;
private int[] localData, edgePixels;
private long lastTime;
private int mVerticalOffsetLeft;
private int mVerticalOffsetRight;
/**
* Creates a new PreviewOverlay
* @param context
* @author ricky barrette
*/
public PreviewOverlay(Context context) {
super(context);
lastTime = System.nanoTime();
}
public void setPreviewSize(int width, int height) {
ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
this.setLayoutParams(layoutParams);
imageWidth = width;
imageHeight = height;
localData = new int[width*height];
edgePixels = new int[width*height];
}
/**
* Called by system when view is invalidated
* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
* @author ricky barrette
*/
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
mHorizonOffset = (this.getHeight() / 4) * 3;
mVerticalOffsetLeft = this.getWidth() / 4;
mVerticalOffsetRight = (this.getWidth() /4 )* 3;
if (mImageData == null) return;
Paint paint = new Paint();
paint.setColor(Color.RED);
/*
* draw the vertical & horizon lines that represent the robots path
*/
canvas.drawLine(0, mHorizonOffset, this.getWidth(), mHorizonOffset, paint);
canvas.drawLine(mVerticalOffsetLeft, mHorizonOffset, mVerticalOffsetLeft, this.getHeight(), paint);
canvas.drawLine(mVerticalOffsetRight, mHorizonOffset, mVerticalOffsetRight, this.getHeight(), paint);
/*
* TODO Draw boxes around objects
*/
/*
* TODO some magical computer vision math
* TODO draw path
*/
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
/*
* here is some text information
*/
paint.setColor(Color.RED);
paint.setTextSize(30);
/*
* FPS
*/
long thisTime = System.nanoTime();
double fps = Math.floor(1e12/(thisTime - lastTime))/1000;
lastTime = thisTime;
canvas.drawText(fps + " fps", 5, 30, paint);
isDrawingView = false;
}
}
}

View File

@@ -49,6 +49,7 @@ public class Main extends Activity implements OnClickListener {
findViewById(R.id.test_activity_button).setOnClickListener(this);
findViewById(R.id.nav_activity_button).setOnClickListener(this);
findViewById(R.id.camera_activity_button).setOnClickListener(this);
/*
* Version information textview
@@ -78,9 +79,16 @@ public class Main extends Activity implements OnClickListener {
case R.id.test_activity_button:
startActivity(new Intent(this, TestActivity.class));
break;
case R.id.nav_activity_button:
startActivity(new Intent(this, NavigationActivity.class));
break;
case R.id.camera_activity_button:
startActivity(new Intent(this, CameraActivity.class));
break;
}
}