git.haldean.org droidcopter / a31cd5a
Created AndroidImage, updated BlobTracker (which does not compile?) Benjamin Bardin 11 years ago
9 changed file(s) with 77 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
00 <?xml version="1.0" encoding="UTF-8"?>
11 <classpath>
2 <classpathentry excluding="org/haldean/chopper/pilot/nav/NavListTest.java|org/haldean/chopper/pilot/nav/NavVelTest.java|org/haldean/blob/" including="org/haldean/chopper/nav/|org/haldean/chopper/pilot/" kind="src" path="src"/>
2 <classpathentry excluding="org/haldean/chopper/pilot/nav/NavListTest.java|org/haldean/chopper/pilot/nav/NavVelTest.java|org/haldean/chopper/server/|org/haldean/blob/JavaImage.java|org/haldean/blob/SegmentTest.java" including="org/haldean/blob/|org/haldean/chopper/nav/|org/haldean/chopper/pilot/" kind="src" path="src"/>
33 <classpathentry kind="src" path="gen"/>
44 <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
55 <classpathentry kind="output" path="bin"/>
0 package org.haldean.blob;
1
2 public class AndroidImage implements Image {
3 private int[][] mData;
4 private int width;
5 private int height;
6
7 public void updateImage(byte[] yuvData, int w, int h) {
8 width = w;
9 height = h;
10
11 if (mData.length != yuvData.length / 2)
12 mData = new int[yuvData.length / 2][3];
13
14 for (int i = 0; i < mData.length; i += 2) {
15 yuvToRgb(mData[i], yuvData[2 * i], yuvData[2 * i + 1], yuvData[2 * i + 3]);
16 yuvToRgb(mData[i + 1], yuvData[2 * i + 2], yuvData[2 * i + 1], yuvData[2 * i + 3]);
17 }
18
19 }
20
21 public int[] getSize() {
22 return new int[] {width, height};
23 }
24
25 public int[] getPixel(int x, int y) {
26 return mData[y * width + x];
27 }
28
29 private void yuvToRgb(int[] target, int y, int u, int v) {
30 target[0] = (int) (y + 1.13983 * v) + 128;
31 target[1] = (int) (y - .39465 * u -.58060 * v) + 128;
32 target[2] = (int) (y + 2.03211 * u) + 128;
33 }
34 }
22 public interface Image {
33 int[] getPixel(int i, int j);
44 int[] getSize();
5 void updateImage(byte[] data, int width, int height);
56 }
1010 public NavList() {
1111 mData = new double[2];
1212 mList = new LinkedList<NavData>();
13 type = "LIST";
1314 }
1415
1516 /**
00 package org.haldean.chopper.nav;
11
2 import java.util.Arrays;
32
43 public class NavTrack extends NavData {
54
00 package org.haldean.chopper.pilot;
11
2 import java.util.Arrays;
3
4 import org.haldean.blob.AndroidImage;
5 import org.haldean.blob.Image;
26 import org.haldean.blob.Segmenter;
3 import org.haldean.blob.Image;
47
58 public final class BlobTracker implements Runnable, Receivable {
69 Image image;
710 Segmenter segmenter;
811 int[] lastLocation;
912 int[] lastVector;
13 MakePicture mPic;
14 byte[] mBuffer;
1015
1116 private static final int TRACKING_PERIOD_MS = 200;
1217 private static final int DISABLED_PERIOD_MS = 1000;
1318
14 public BlobTracker() {
19 public BlobTracker(MakePicture pic) {
1520 lastLocation = new int[2];
1621 lastVector = new int[3];
1722 segmenter = null;
18 image = null;
23 image = new AndroidImage();
24 mPic = pic;
25 mBuffer = new byte[mPic.getBufferLength()];
1926 }
2027
2128 public void receiveMessage(String msg, Receivable source) {
4451 public void run() {
4552 while (true) {
4653 if (enabled) {
47 //image = getImage();
54 if (mBuffer.length != mPic.getBufferLength())
55 mBuffer = new byte[mPic.getBufferLength()];
56 mPic.getBufferCopy(mBuffer);
57 int[] size = mPic.getFrameSize();
58 image.updateImage(mBuffer, size[0], size[1]);
4859 calculateVector();
4960 try {
5061 Thread.sleep(TRACKING_PERIOD_MS);
107107 /**
108108 * Gets the length of the array storing preview frames
109109 */
110 public int getFrameArrayLength() {
110 public int getBufferLength() {
111111 return mXprev.get() * mYprev.get() * ImageFormat.getBitsPerPixel(getPreviewFormat()) / 8;
112112 }
113113
265265 mYprev.set(mNextY.get());
266266 Log.v(TAG, "Init prev array: " + mXprev.get() + ", " + mYprev.get());
267267 synchronized (mStoreFrame) {
268 mStoreFrame = new byte[getFrameArrayLength()];
268 mStoreFrame = new byte[getBufferLength()];
269269 Log.e(TAG, "Array size: " + mStoreFrame.length);
270270 }
271271 initPrevCallback();
404404 mCamera.setPreviewCallbackWithBuffer(precall);
405405
406406 synchronized (mStoreFrame) {
407 mStoreFrame = new byte[getFrameArrayLength()];
407 mStoreFrame = new byte[getBufferLength()];
408408 mCamera.addCallbackBuffer(mStoreFrame);
409409 }
410410 //Inner class defs done
4040 return (long) vel.getTime();
4141 else
4242 return (long) Math.max(NAVPAUSE, vel.getTime() - (System.currentTimeMillis() - vel.getFirstCall()));
43 }
44 else if (nav instanceof NavTrack) {
45 NavTrack track = (NavTrack) nav;
46 if (track.getStartTime() <= 0)
47 return (long) track.getTrackTime();
48 else
49 return (long) Math.max(NAVPAUSE, track.getTrackTime() - (System.currentTimeMillis() - track.getStartTime()));
4350 }
4451 return NAVPAUSE;
4552 }
105112 while ((curTask != null) && (isComplete(curTask))) {
106113 curTask = lastList.nextTask();
107114 }
108 if (curTask == null) {
115 if (curTask == null)
109116 return true;
110 }
111 else {
117 else
112118 return false;
113 }
119 }
120 else if (nav instanceof NavTrack) {
121 NavTrack track = (NavTrack) nav;
122 if (track.started() && (System.currentTimeMillis() - track.getStartTime() >= track.getTrackTime()))
123 return true;
124 else
125 return false;
114126 }
115127 return true;
116128 }
117129
118130 private void getTrackTarg(NavTrack nav, double[] target) {
119 if (!nav.started()) {
120 nav.start();
121 }
131 if (!nav.started()) {
132 nav.start();
133 }
122134 }
123135
124136
218218 return;
219219 }
220220 int[] frameSize = myMakePic.getFrameSize();
221 if ((mPicFrame == null) || (mPicFrame.length != myMakePic.getFrameArrayLength()))
222 mPicFrame = new byte[myMakePic.getFrameArrayLength()];
221 if ((mPicFrame == null) || (mPicFrame.length != myMakePic.getBufferLength()))
222 mPicFrame = new byte[myMakePic.getBufferLength()];
223223 myMakePic.getBufferCopy(mPicFrame); //create a new copy.
224224 byte[] temppic = encodePic(mPicFrame, frameSize);
225225 myMakePic.setFrameNewnessTo(false);